Created
December 1, 2016 03:52
-
-
Save counterbeing/1e27bbb970cab85289f82c89fc2374a6 to your computer and use it in GitHub Desktop.
Trying to make these methods chain! I'd expect these to work... kinda. It gets confusing fast. The expectation should stay mostly intact, but needs to accept an initial argument. The `run_chain` method is where most changes should be made.
This file contains 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 'rspec' | |
action = Struct.new(:action, :args) | |
def run_chain(action_chain, anchor) | |
action_chain.inject(anchor) do |chain, link| | |
lambda do | |
link.action.call(chain.action.call) | |
end | |
end.call | |
end | |
describe('chaining methods') do | |
let(:action_chain) do | |
lambda1 = -> (var) { var } | |
lambda2 = -> (var) { var + 1 } | |
lambda3 = -> (var) { "I have #{var} pickles" } | |
[ | |
action.new(lambda1), | |
action.new(lambda2), | |
action.new(lambda3) | |
] | |
end | |
it 'chains and calls the methods' do | |
anchor = -> { action.new(action: -> { 1 }) } | |
expect(run_chain(action_chain, anchor)) | |
.to eq('I have 2 pickles') | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment