summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-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