Created
May 28, 2015 16:59
-
-
Save mcmire/16021ade611b37c3e6ae to your computer and use it in GitHub Desktop.
SingleOrMultiple functor (Ruby)
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 "attr_extras" | |
class SingleOrMultiple | |
rattr_initialize :value | |
def map(&block) | |
result = | |
if multiple_values? | |
value.map(&block) | |
else | |
block.call(value) | |
end | |
self.class.new(result) | |
end | |
def to_a | |
if multiple_values? | |
value | |
else | |
[value] | |
end | |
end | |
def multiple_values? | |
value.is_a?(Array) | |
end | |
def single_value? | |
!multiple_values? | |
end | |
end |
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 "spec_helper" | |
require_relative "single_or_multiple" | |
describe SingleOrMultiple do | |
describe "#map" do | |
context "working with a single value" do | |
it "calls the given block with the value and returns the result, wrapped" do | |
f = described_class.new("single value") | |
f2 = f.map(&:upcase) | |
expect(f2.value).to eq "SINGLE VALUE" | |
end | |
end | |
context "working with multiple values" do | |
it "calls the given block for each value and returns the results, wrapped" do | |
f = described_class.new(["one fish", "two fish"]) | |
f2 = f.map(&:upcase) | |
expect(f2.value).to eq ["ONE FISH", "TWO FISH"] | |
end | |
end | |
end | |
describe "#to_a" do | |
context "working with a single value" do | |
it "wraps it in an array" do | |
f = described_class.new("a value") | |
expect(f.to_a).to eq ["a value"] | |
end | |
end | |
context "working with multiple values" do | |
it "returns the values" do | |
values = ["first value", "second value"] | |
f = described_class.new(values) | |
expect(f.to_a).to eq values | |
end | |
end | |
end | |
describe "#multiple_values?" do | |
context "working with a single value" do | |
it "returns false" do | |
f = described_class.new("single value") | |
expect(f).not_to be_multiple_values | |
end | |
end | |
context "working with multiple values" do | |
it "returns true" do | |
f = described_class.new(["multiple", "values"]) | |
expect(f).to be_multiple_values | |
end | |
end | |
end | |
describe "#single_value?" do | |
context "working with a single value" do | |
it "returns true" do | |
f = described_class.new("single value") | |
expect(f).to be_single_value | |
end | |
end | |
context "working with multiple values" do | |
it "returns false" do | |
f = described_class.new(["multiple", "values"]) | |
expect(f).not_to be_single_value | |
end | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment