Skip to content

Commit

Permalink
customer attributes uses method missing to get attribute readers
Browse files Browse the repository at this point in the history
  • Loading branch information
ryanmoran committed Dec 22, 2011
1 parent 2815e8b commit fae799a
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 11 deletions.
7 changes: 3 additions & 4 deletions .autotest
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
Autotest.add_hook :initialize do |at|
at.clear_mappings

at.add_mapping %r%/^lib/(.*)\.rb$% do |_, m|
possible = File.basename(m[1])
files_matching %r%^spec/.*(#{possible}_spec)\.rb$%
at.add_mapping(%r%^lib/(.*)\.rb$%) do |_, m|
at.files_matching %r%^spec/#{m[1]}_spec\.rb$%
end

at.add_mapping(%r%^spec/.*\_spec.rb$%) {|filename, _| filename }
at.add_mapping(%r%^spec/.*\_spec\.rb$%) { |filename, _| filename }
end
8 changes: 6 additions & 2 deletions lib/netsuite/models/customer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,12 @@ def self.get(id)
end
end

def is_person
@attributes[:is_person]
def method_missing(m, *args, &block)
if @attributes.keys.include?(m.to_sym)
@attributes[m.to_sym]
else
super
end
end

end
Expand Down
18 changes: 13 additions & 5 deletions spec/netsuite/models/customer_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,19 @@
end
end

describe '#is_person' do
context 'when the customer is a person' do
it 'returns true' do
customer = NetSuite::Customer.new(:is_person => true)
customer.is_person.should be_true
describe '#method_missing' do
context 'when calling a method that exists as an attribute' do
it 'returns the right value from the attributes hash' do
customer = NetSuite::Customer.new(:name => 'Mr. Banana')
customer.name.should eql('Mr. Banana')
end
end

context 'when calling a method that does not exist in the attributes hash' do
it 'raises a NoMethodError' do
lambda {
customer.banana
}.should raise_error(NoMethodError)
end
end
end
Expand Down

0 comments on commit fae799a

Please sign in to comment.