Skip to content

Instantly share code, notes, and snippets.

@acuppy
Created December 11, 2014 05:26
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save acuppy/56890ef8de40c855bdfe to your computer and use it in GitHub Desktop.
Save acuppy/56890ef8de40c855bdfe to your computer and use it in GitHub Desktop.
Arguments wrapper
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
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