Last active
March 30, 2016 21:32
-
-
Save pje/b5ea445dc239dec17266 to your computer and use it in GitHub Desktop.
microrefinements to facilitate method chaining in ruby
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
module Chain | |
refine Object do | |
def and(obj) | |
self && obj | |
end | |
def or(obj) | |
self || obj | |
end | |
def receive(obj) | |
obj.send(self) | |
end | |
def self_if(pred) | |
send(pred) ? self : nil | |
end | |
def self_unless(pred) | |
send(pred) ? nil : self | |
end | |
def yield_to | |
yield(self) | |
end | |
def yield_if(pred) | |
if (pred.respond_to?(:call) ? pred.call(self) : send(pred)) | |
yield(self) | |
end | |
end | |
def yield_unless(pred) | |
unless (pred.respond_to?(:call) ? pred.call(self) : send(pred)) | |
yield(self) | |
end | |
end | |
end | |
end |
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 'minitest/autorun' | |
require_relative 'chain' | |
describe Chain do | |
using Chain | |
it :and do | |
nil.and(2).must_equal nil | |
false.and(2).must_equal false | |
true.and(2).must_equal 2 | |
end | |
it :or do | |
nil.or(2).must_equal 2 | |
false.or(2).must_equal 2 | |
true.or(2).must_equal true | |
end | |
it :receive do | |
:odd?.receive(3).must_equal true | |
:nil?.receive(nil).must_equal true | |
end | |
it :self_if do | |
2.self_if(:even?).must_equal 2 | |
2.self_if(:odd?).must_equal nil | |
nil.self_if(:nil?).must_equal nil | |
end | |
it :self_unless do | |
2.self_unless(:even?).must_equal nil | |
2.self_unless(:odd?).must_equal 2 | |
nil.self_unless(:nil?).must_equal nil | |
end | |
it :yield_to do | |
nil.yield_to { |x| x }.must_equal nil | |
2.yield_to { |x| x * x }.must_equal 4 | |
end | |
it :yield_if do | |
3.yield_if(:nil?) { |x| x > 2 }.must_equal(nil) | |
nil.yield_if(:nil?) { |x| 4 }.must_equal(4) | |
3.yield_if(->(x){ x == 3 }) { |x| x > 2 }.must_equal(true) | |
nil.yield_if(:nil?, &:object_id).must_equal(8) | |
end | |
it :yield_unless do | |
3.yield_unless(:nil?) { |x| x > 2 }.must_equal(true) | |
nil.yield_unless(:nil?) { |x| 4 }.must_equal(nil) | |
3.yield_unless(->(x){ x == 3 }) { |x| x > 2 }.must_equal(nil) | |
nil.yield_unless(:tainted?, &:object_id).must_equal(8) | |
end | |
end |
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
test: | |
ruby *_spec.rb | |
.PHONY: test | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment