Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

More documentation and specs on hash with magic predicates.

  • Loading branch information...
commit 71444f6dcf92f3680777f60d0788fbe02749cab0 1 parent ce674f0
@josevalim josevalim authored
View
5 CHANGELOG.rdoc
@@ -1,15 +1,12 @@
== TODO
-* Change Thor.start to parse ARGV in a single pass
* Improve spec coverage for Thor::Runner
* Improve help output to list shorthand switches, too
* Investigate and fix deep namespacing ("foo:bar:baz") issues
== Current
-* Created a Thor::DSL. If you plan to create a Thor task, this is where you
- should look
-* Added some documentation and code refactoring
+* Added some documentation and code refactoring.
== 0.9.8, released 2008-10-20
View
8 lib/thor/base.rb
@@ -336,18 +336,18 @@ def from_superclass(method, default=nil)
self == baseclass ? default : superclass.send(method).dup
end
- # SIGNATURE: Sets the baseclass to Thor. This is where the superclass
- # lookup finishes.
+ # SIGNATURE: Sets the baseclass. This is where the superclass lookup
+ # finishes.
def baseclass #:nodoc:
end
# SIGNATURE: Defines if a given method is a valid_task?. This method is
- # called everytime a new method is added to the class.
+ # called before a new method is added to the class.
def valid_task?(meth) #:nodoc:
end
# SIGNATURE: Creates a new task if valid_task? is true. This method is
- # called everytime a new method is added to the class.
+ # called when a new method is added to the class.
def create_task(meth) #:nodoc:
end
View
34 lib/thor/core_ext/hash_with_indifferent_access.rb
@@ -1,36 +1,52 @@
class Thor
module CoreExt
+
+ # A hash with indifferent access and magic predicates.
+ #
+ # hash = Thor::CoreExt::HashWithIndifferentAccess.new 'foo' => 'bar', 'baz' => 'bee', 'force' => true
+ #
+ # hash[:foo] #=> 'bar'
+ # hash['foo'] #=> 'bar'
+ # hash.foo? #=> true
+ #
class HashWithIndifferentAccess < ::Hash
+
def initialize(hash)
super()
- update hash
+ update(hash)
end
-
+
def [](key)
- super convert_key(key)
+ super(convert_key(key))
end
-
+
+ def delete(key)
+ super(convert_key(key))
+ end
+
def values_at(*indices)
indices.collect { |key| self[convert_key(key)] }
end
-
+
protected
+
def convert_key(key)
key.kind_of?(Symbol) ? key.to_s : key
end
-
+
# Magic predicates. For instance:
+ #
# options.force? # => !!options['force']
+ #
def method_missing(method, *args, &block)
method = method.to_s
- if method =~ /^(\w+)=$/
- self[$1] = args.first
- elsif method =~ /^(\w+)\?$/
+ if method =~ /^(\w+)\?$/
!!self[$1]
else
self[method]
end
end
+
end
end
end
View
18 spec/core_ext/hash_with_indifferent_access_spec.rb
@@ -5,25 +5,23 @@
before(:each) do
@hash = Thor::CoreExt::HashWithIndifferentAccess.new 'foo' => 'bar', 'baz' => 'bee', 'force' => true
end
-
+
it "has values accessible by either strings or symbols" do
@hash['foo'].must == 'bar'
@hash[:foo].must == 'bar'
+
@hash.values_at(:foo, :baz).must == ['bar', 'bee']
+ @hash.delete(:foo).must == 'bar'
end
-
- it "should handles magic boolean predicates" do
+
+ it "handles magic boolean predicates" do
@hash.force?.must be_true
@hash.foo?.must be_true
@hash.nothing?.must be_false
end
-
- it "should map methods to keys: hash.foo => hash[:foo]" do
+
+ it "maps methods to keys" 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
Please sign in to comment.
Something went wrong with that request. Please try again.