Skip to content

Commit

Permalink
Extend magic predicate operators to work with non-boolean opts also. …
Browse files Browse the repository at this point in the history
…Option hash can now be treated like an OpenStruct
  • Loading branch information
Charles Jolley committed Jan 12, 2009
1 parent 90bac99 commit 4df7322
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 1 deletion.
9 changes: 8 additions & 1 deletion lib/thor/options.rb
Expand Up @@ -28,7 +28,14 @@ def convert_key(key)
# Magic predicates. For instance:
# options.force? # => !!options['force']
def method_missing(method, *args, &block)
method.to_s =~ /^(\w+)\?$/ ? !!self[$1] : super
method = method.to_s
if method =~ /^(\w+)=$/
self[$1] = args.first
elsif method =~ /^(\w+)\?$/
!!self[$1]
else
self[method]
end
end
end

Expand Down
10 changes: 10 additions & 0 deletions spec/options_spec.rb
Expand Up @@ -207,6 +207,16 @@ def parse(*args)
@hash.foo?.must be_true
@hash.nothing?.must be_false
end

it "should map methods to keys: hash.foo => hash[:foo]" do
@hash.foo.must == @hash['foo']
end

it "should map setters to keys: hash.foo=bar => hash[:foo] => bar" do
@hash.foo = :bar2
@hash.foo.must == :bar2
end

end

describe ":numeric type" do
Expand Down

0 comments on commit 4df7322

Please sign in to comment.