summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMichael Bleigh <michael@intridea.com>2010-08-10 10:05:52 -0500
committerMichael Bleigh <michael@intridea.com>2010-08-10 10:05:52 -0500
commit5a53de54c1b97c8f16203ad0fef6e95ef4c2a161 (patch)
tree51728c42cbaf5a18e8c516cb5541581b765ed0e9
parent714dad5ccd277f47d753b258311f0cc5ff961491 (diff)
downloadhashie-5a53de54c1b97c8f16203ad0fef6e95ef4c2a161.tar.gz
Query methods now return false if the key has been set to nil or false. Closes #4
-rw-r--r--README.rdoc4
-rw-r--r--lib/hashie/mash.rb2
-rw-r--r--spec/hashie/mash_spec.rb7
3 files changed, 12 insertions, 1 deletions
diff --git a/README.rdoc b/README.rdoc
index a8b8d10..7346d9a 100644
--- a/README.rdoc
+++ b/README.rdoc
@@ -30,6 +30,10 @@ to JSON and XML parsed hashes.
# use bang methods for multi-level assignment
mash.author!.name = "Michael Bleigh"
mash.author # => <Hashie::Mash name="Michael Bleigh">
+
+<b>Note:</b> The <tt>?</tt> method will return false if a key has been set
+to false or nil. In order to check if a key has been set at all, use the
+<tt>mash.key?('some_key')</tt> method instead.
== Dash
diff --git a/lib/hashie/mash.rb b/lib/hashie/mash.rb
index 7904202..bcc976b 100644
--- a/lib/hashie/mash.rb
+++ b/lib/hashie/mash.rb
@@ -125,7 +125,7 @@ module Hashie
when "="
self[match[1]] = args.first
when "?"
- key?(match[1])
+ !!self[match[1]]
when "!"
initializing_reader(match[1])
else
diff --git a/spec/hashie/mash_spec.rb b/spec/hashie/mash_spec.rb
index 64b30ef..c927a87 100644
--- a/spec/hashie/mash_spec.rb
+++ b/spec/hashie/mash_spec.rb
@@ -24,6 +24,13 @@ describe Hashie::Mash do
@mash.test = "abc"
@mash.test?.should be_true
end
+
+ it "should return false on a ? method if a value has been set to nil or false" do
+ @mash.test = nil
+ @mash.should_not be_test
+ @mash.test = false
+ @mash.should_not be_test
+ end
it "should make all [] and []= into strings for consistency" do
@mash["abc"] = 123