Created
December 11, 2014 05:26
-
-
Save acuppy/56890ef8de40c855bdfe to your computer and use it in GitHub Desktop.
Arguments wrapper
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 "ostruct" | |
class Arguments | |
def initialize(arguments) | |
@whiny = arguments.delete(:whiny) || false | |
@arguments = arguments | |
@delegee = OpenStruct.new(@arguments) | |
end | |
def method_missing(method, *args) | |
@delegee.send(method, *args) | |
rescue NoMethodError | |
if whiny? | |
raise ArgumentError.new("No argument matches '#{method}': available arguments '#{@arguments.keys.join(', ')}'") | |
end | |
end | |
private | |
def whiny? | |
!!@whiny | |
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 "rubygems" | |
require "rspec" | |
require "arguments" | |
describe Arguments do | |
let(:args) do | |
{ foo: "bar" } | |
end | |
subject(:arguments) { Arguments.new(args) } | |
it { expect(arguments.foo).to eq "bar" } | |
it { expect(arguments.foo_b).to be_nil } | |
context "when whiny" do | |
subject(:arguments) { Arguments.new(args.merge(whiny: true)) } | |
it { expect { arguments.foo_b }.to raise_error } | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment