diff options
author | gregory <greg2502@gmail.com> | 2014-06-20 08:38:00 -0400 |
---|---|---|
committer | dblock <dblock@dblock.org> | 2014-06-20 08:38:00 -0400 |
commit | f3fd2e51da1e76e6495a04ab44cb8915df59bbaa (patch) | |
tree | 735654521c072b9ed5da8f66b63650fe65556b5b | |
parent | 3ab15f24beba02b20f2edb69ddc861c86d3abe3c (diff) | |
download | hashie-f3fd2e51da1e76e6495a04ab44cb8915df59bbaa.tar.gz |
Include Trash and Dash class name when raising NoMethodError.
-rw-r--r-- | CHANGELOG.md | 2 | ||||
-rw-r--r-- | lib/hashie/dash.rb | 20 | ||||
-rw-r--r-- | lib/hashie/trash.rb | 4 | ||||
-rw-r--r-- | spec/hashie/dash_spec.rb | 30 |
4 files changed, 32 insertions, 24 deletions
diff --git a/CHANGELOG.md b/CHANGELOG.md index 49fccf4..8b359ba 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -6,6 +6,7 @@ **Note:** This version introduces several backward incompatible API changes. See [UPGRADING](UPGRADING.md) for details. +* [#171](https://github.com/intridea/hashie/pull/171): Include Trash and Dash class name when raising `NoMethodError` - [@gregory](https://github.com/gregory). * [#172](https://github.com/intridea/hashie/pull/172): Added Dash and Trash#update_attributes! - [@gregory](https://github.com/gregory). * [#173](https://github.com/intridea/hashie/pull/173): Auto include Dash::IndifferentAccess when IndiferentAccess is included in Dash - [@gregory](https://github.com/gregory). * [#174](https://github.com/intridea/hashie/pull/174): Fixed `from` and `transform_with` Trash features when IndifferentAccess is included - [@gregory](https://github.com/gregory). @@ -18,7 +19,6 @@ * [#149](https://github.com/intridea/hashie/issues/149): Allow IgnoreUndeclared and DeepMerge to be used with undeclared properties - [@jhaesus](https://github.com/jhaesus). ## 2.1.1 (4/12/2014) - * [#144](https://github.com/intridea/hashie/issues/144): Fixed regression invoking `to_hash` with no parameters - [@mbleigh](https://github.com/mbleigh). ## 2.1.0 (4/6/2014) diff --git a/lib/hashie/dash.rb b/lib/hashie/dash.rb index 5a26861..b685cf6 100644 --- a/lib/hashie/dash.rb +++ b/lib/hashie/dash.rb @@ -164,9 +164,7 @@ module Hashie end def assert_property_exists!(property) - unless self.class.property?(property) - fail NoMethodError, "The property '#{property}' is not defined for this Dash." - end + fail_no_property_error!(property) unless self.class.property?(property) end def assert_required_attributes_set! @@ -176,15 +174,19 @@ module Hashie end def assert_property_set!(property) - if send(property).nil? - fail ArgumentError, "The property '#{property}' is required for this Dash." - end + fail_property_required_error!(property) if send(property).nil? end def assert_property_required!(property, value) - if self.class.required?(property) && value.nil? - fail ArgumentError, "The property '#{property}' is required for this Dash." - end + fail_property_required_error!(property) if self.class.required?(property) && value.nil? + end + + def fail_property_required_error!(property) + fail ArgumentError, "The property '#{property}' is required for #{self.class.name}." + end + + def fail_no_property_error!(property) + fail NoMethodError, "The property '#{property}' is not defined for #{self.class.name}." end end end diff --git a/lib/hashie/trash.rb b/lib/hashie/trash.rb index fbad3a6..6179dc2 100644 --- a/lib/hashie/trash.rb +++ b/lib/hashie/trash.rb @@ -88,9 +88,7 @@ module Hashie # Raises an NoMethodError if the property doesn't exist # def property_exists?(property) - unless self.class.property?(property) - fail NoMethodError, "The property '#{property}' is not defined for this Trash." - end + fail_no_property_error!(property) unless self.class.property?(property) true end diff --git a/spec/hashie/dash_spec.rb b/spec/hashie/dash_spec.rb index 6b10a52..59f03c3 100644 --- a/spec/hashie/dash_spec.rb +++ b/spec/hashie/dash_spec.rb @@ -43,6 +43,14 @@ class DeferredTest < Hashie::Dash end describe DashTest do + def property_required_error(property) + [ArgumentError, "The property '#{property}' is required for #{subject.class.name}."] + end + + def no_property_error(property) + [NoMethodError, "The property '#{property}' is not defined for #{subject.class.name}."] + end + subject { DashTest.new(first_name: 'Bob', email: 'bob@example.com') } it('subclasses Hashie::Hash') { should respond_to(:to_mash) } @@ -68,30 +76,30 @@ describe DashTest do it { should_not respond_to(:nonexistent) } it 'errors out for a non-existent property' do - expect { subject['nonexistent'] }.to raise_error(NoMethodError) + expect { subject['nonexistent'] }.to raise_error(*no_property_error('nonexistent')) end it 'errors out when attempting to set a required property to nil' do - expect { subject.first_name = nil }.to raise_error(ArgumentError) + expect { subject.first_name = nil }.to raise_error(*property_required_error('first_name')) end context 'writing to properties' do it 'fails writing a required property to nil' do - expect { subject.first_name = nil }.to raise_error(ArgumentError) + expect { subject.first_name = nil }.to raise_error(*property_required_error('first_name')) end it 'fails writing a required property to nil using []=' do - expect { subject[:first_name] = nil }.to raise_error(ArgumentError) + expect { subject[:first_name] = nil }.to raise_error(*property_required_error('first_name')) end it 'fails writing to a non-existent property using []=' do - expect { subject['nonexistent'] = 123 }.to raise_error(NoMethodError) + expect { subject['nonexistent'] = 123 }.to raise_error(*no_property_error('nonexistent')) end it 'works for an existing property using []=' do subject[:first_name] = 'Bob' expect(subject[:first_name]).to eq 'Bob' - expect { subject['first_name'] }.to raise_error(NoMethodError) + expect { subject['first_name'] }.to raise_error(*no_property_error('first_name')) end it 'works for an existing property using a method call' do @@ -102,7 +110,7 @@ describe DashTest do context 'reading from properties' do it 'fails reading from a non-existent property using []' do - expect { subject['nonexistent'] }.to raise_error(NoMethodError) + expect { subject['nonexistent'] }.to raise_error(*no_property_error('nonexistent')) end it 'is able to retrieve properties through blocks' do @@ -133,7 +141,7 @@ describe DashTest do describe '#new' do it 'fails with non-existent properties' do - expect { described_class.new(bork: '') }.to raise_error(NoMethodError) + expect { described_class.new(bork: '') }.to raise_error(*no_property_error('bork')) end it 'sets properties that it is able to' do @@ -152,7 +160,7 @@ describe DashTest do end it 'fails when required values are missing' do - expect { DashTest.new }.to raise_error(ArgumentError) + expect { DashTest.new }.to raise_error(*property_required_error('first_name')) end it 'does not overwrite default values' do @@ -176,11 +184,11 @@ describe DashTest do end it 'fails with non-existent properties' do - expect { subject.merge(middle_name: 'James') }.to raise_error(NoMethodError) + expect { subject.merge(middle_name: 'James') }.to raise_error(*no_property_error('middle_name')) end it 'errors out when attempting to set a required property to nil' do - expect { subject.merge(first_name: nil) }.to raise_error(ArgumentError) + expect { subject.merge(first_name: nil) }.to raise_error(*property_required_error('first_name')) end context 'given a block' do |