Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Merge pull request #1 from justinfrench/add_inspect_to_instances

Add `#inspect` and `.accessors`
  • Loading branch information...
commit d1bff1fc38dccb06bf08ea502219517228f9550e 2 parents 825c689 + ebba427
@stevehodgkiss stevehodgkiss authored
Showing with 23 additions and 0 deletions.
  1. +13 −0 lib/modelling.rb
  2. +10 −0 spec/modelling_spec.rb
View
13 lib/modelling.rb
@@ -32,6 +32,10 @@ def structs(*args)
def members
@members ||= {}
end
+
+ def accessors
+ @accessors ||= []
+ end
private
@@ -55,6 +59,7 @@ def generate_accessors(names_to_initializer)
end
def create_accessor(name)
+ accessors << name
instance_eval { attr_accessor name }
end
@@ -74,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
10 spec/modelling_spec.rb
@@ -125,6 +125,16 @@ class LambdaTest
it 'doesnt fail when lambdas with no args are used' do
LambdaTest.new.lambda.should eq 'boo'
end
+
+ specify 'tracks list of accessors' do
+ 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 }
Please sign in to comment.
Something went wrong with that request. Please try again.