Skip to content
Browse files

add #inspect to instances with a hash of accessors and values

  • Loading branch information...
1 parent 6d134e5 commit ebba4274682d4a017328321beb522e822e8c6114 @justinfrench justinfrench committed
Showing with 14 additions and 0 deletions.
  1. +8 −0 lib/modelling.rb
  2. +6 −0 spec/modelling_spec.rb
View
8 lib/modelling.rb
@@ -79,5 +79,13 @@ def initialize(args = {})
end
args.each { |name, value| send "#{name}=", value }
end
+
+ def inspect
+ hash = {}
+ self.class.accessors.each do |method_name|
+ hash[method_name] = send(method_name)
+ end
+ hash
+ end
end
View
6 spec/modelling_spec.rb
@@ -130,6 +130,12 @@ class LambdaTest
User.accessors.should include :name, :age
end
+ specify 'provides a Hash of attributes and values through inspect' do
+ User.new.inspect.key?(:name).should be_true
+ User.new.inspect.key?(:age).should be_true
+ User.new(:name => "Joe").inspect[:name].should eq "Joe"
+ end
+
context 'inheritence' do
let(:car) { Car.new }
let(:super_car) { SuperCar.new }

0 comments on commit ebba427

Please sign in to comment.
Something went wrong with that request. Please try again.