summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAndré Luis Leal Cardoso Junior <andrehjr@gmail.com>2009-11-16 00:53:33 -0200
committerAndré Luis Leal Cardoso Junior <andrehjr@gmail.com>2009-11-16 00:53:33 -0200
commitbb4feb07579ad86d2892fe0a6f5bc4dfa7b19303 (patch)
treeb75ba86a5323dca07578a16b8749dfb6ec3b049d
parentf20d0ac3969a5679c57434b734e4725490ce249a (diff)
downloadhashie-bb4feb07579ad86d2892fe0a6f5bc4dfa7b19303.tar.gz
Dashes now raises NoMethodError when reading a non-existent property
-rw-r--r--lib/hashie/dash.rb20
-rw-r--r--spec/hashie/dash_spec.rb6
2 files changed, 19 insertions, 7 deletions
diff --git a/lib/hashie/dash.rb b/lib/hashie/dash.rb
index eac2293..47b0afe 100644
--- a/lib/hashie/dash.rb
+++ b/lib/hashie/dash.rb
@@ -71,18 +71,24 @@ module Hashie
# Retrieve a value from the Dash (will return the
# property's default value if it hasn't been set).
- def [](property_name)
- super(property_name.to_sym)
+ def [](property)
+ super(property.to_sym) if property_exists? property
end
# Set a value on the Dash in a Hash-like way. Only works
# on pre-existing properties.
def []=(property, value)
- if self.class.property?(property.to_sym)
- super
- else
- raise NoMethodError, "The property '#{property}' is not defined for this Dash."
- end
+ super if property_exists? property
end
+
+ private
+ # Raises an NoMethodError if the property doesn't exist
+ #
+ def property_exists?(property)
+ unless self.class.property?(property.to_sym)
+ raise NoMethodError, "The property '#{property}' is not defined for this Dash."
+ end
+ true
+ end
end
end
diff --git a/spec/hashie/dash_spec.rb b/spec/hashie/dash_spec.rb
index 3a71c59..9443ef6 100644
--- a/spec/hashie/dash_spec.rb
+++ b/spec/hashie/dash_spec.rb
@@ -32,6 +32,12 @@ describe Hashie::Dash do
end
end
+ describe 'reading properties' do
+ it 'should raise an error when reading a non-existent property' do
+ lambda{@dash['abc']}.should raise_error(NoMethodError)
+ end
+ end
+
describe ' writing to properties' do
before do
@dash = DashTest.new