Last active
December 28, 2015 13:29
-
-
Save b-murphy/7507729 to your computer and use it in GitHub Desktop.
Need help with this.... there must be a better way.....
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'pry' | |
RSpec.describe " example iterator " do | |
class ATest | |
def call(args = {}) | |
args[:a] = "this was at A" | |
args[:b] = "this should not happen" | |
args[:c] = "this should not happen" | |
args | |
end | |
end | |
class BTest | |
def call(args = {}) | |
args[:b] = "this was at B" | |
if args.fetch(:a) == "this was at A" | |
args[:a] = "this changes the A when it hits B" | |
end | |
args | |
end | |
end | |
class CTest | |
def call(args = {}) | |
args[:c] = "this was at C" | |
if args.fetch(:b) == "this was at B" | |
args[:b] = "this changes B when it hits C" | |
end | |
args | |
end | |
end | |
def brendan_iterator(parameters, args) | |
first_object = nil | |
second_object = nil | |
args_length = args.length | |
until args.length == 0 | |
if args.length == args_length | |
first_object = args.shift.call(parameters) | |
else | |
second_object = args.shift.call(first_object) | |
first_object = second_object | |
end | |
end | |
first_object | |
end | |
def alex_iterator(parameters, args) | |
# apply each operator in args to the parameters | |
args.inject(parameters){|params, operator| operator.call(params)} | |
end | |
let(:expected_return) do | |
{ | |
a: "this changes the A when it hits B", | |
b: "this changes B when it hits C", | |
c: "this was at C", | |
unchanged_value: "Hello this should remain untouched" | |
} | |
end | |
describe " Brendan original format" do | |
it 'the last object return value includes the changes the previous objects provided' do | |
list = [ATest.new, BTest.new, CTest.new] | |
parameters = { unchanged_value: "Hello this should remain untouched" } | |
expect( | |
brendan_iterator(parameters, list) | |
).to eq expected_return | |
end | |
end | |
describe "Alex's format" do | |
it 'the last object return value includes the changes the previous objects provided' do | |
list = [ATest.new, BTest.new, CTest.new] | |
parameters = { unchanged_value: "Hello this should remain untouched" } | |
expect( | |
alex_iterator(parameters, list) | |
).to eq expected_return | |
end | |
end | |
end |
alexslade
commented
Nov 17, 2013
@heeton thanks for this :-) there is alittle more i want to do with the looping and returning the hash, but this is close enough :-)
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment