Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

Already on GitHub? Sign in to your account

Add `#inspect` and `.accessors` #1

Merged
merged 2 commits into from Apr 18, 2012
Jump to file or symbol
Failed to load files and symbols.
+23 −0
Split
View
@@ -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
@@ -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 }