summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorBen Schwarz <ben.schwarz@gmail.com>2010-01-14 10:24:58 +0800
committerMichael Bleigh <michael@intridea.com>2010-01-15 01:13:53 +0800
commit81ec5006cfe2d4c0adc7cecf9940eb6dbb8cbe39 (patch)
treebe237f4f01eb89f2d0c88b218154591e21e790f9
parenta2ecc494e21934af553a5141be1c89e1e9e5558f (diff)
downloadhashie-81ec5006cfe2d4c0adc7cecf9940eb6dbb8cbe39.tar.gz
Swapped out class variables to scale the ancestors tree. This allows for the super class to not receive properties set on subclasses.
-rw-r--r--lib/hashie/dash.rb22
-rw-r--r--spec/hashie/dash_spec.rb10
2 files changed, 26 insertions, 6 deletions
diff --git a/lib/hashie/dash.rb b/lib/hashie/dash.rb
index 3d73090..6d4e6b5 100644
--- a/lib/hashie/dash.rb
+++ b/lib/hashie/dash.rb
@@ -26,8 +26,8 @@ module Hashie
def self.property(property_name, options = {})
property_name = property_name.to_sym
- (@@properties ||= []) << property_name
- (@@defaults ||= {})[property_name] = options.delete(:default)
+ (@properties ||= []) << property_name
+ (@defaults ||= {})[property_name] = options.delete(:default)
class_eval <<-RUBY
def #{property_name}
@@ -43,7 +43,14 @@ module Hashie
# Get a String array of the currently defined
# properties on this Dash.
def self.properties
- @@properties.collect{|p| p.to_s}
+ properties = []
+ ancestors.each do |elder|
+ if elder.instance_variable_defined?("@properties")
+ properties << elder.instance_variable_get("@properties")
+ end
+ end
+
+ properties.flatten.map{|p| p.to_s}
end
# Check to see if the specified property has already been
@@ -54,7 +61,14 @@ module Hashie
# The default values that have been set for this Dash
def self.defaults
- @@defaults
+ properties = {}
+ ancestors.each do |elder|
+ if elder.instance_variable_defined?("@defaults")
+ properties.merge! elder.instance_variable_get("@defaults")
+ end
+ end
+
+ properties
end
# You may initialize a Dash with an attributes hash
diff --git a/spec/hashie/dash_spec.rb b/spec/hashie/dash_spec.rb
index c800681..b7971f9 100644
--- a/spec/hashie/dash_spec.rb
+++ b/spec/hashie/dash_spec.rb
@@ -7,6 +7,7 @@ class DashTest < Hashie::Dash
end
class Subclassed < DashTest
+ property :last_name
end
describe Hashie::Dash do
@@ -84,14 +85,19 @@ end
describe Subclassed do
it "should inherit all properties from DashTest" do
- Subclassed.properties.size.should == 5
+ Subclassed.properties.size.should == 6
end
it "should inherit all defaults from DashTest" do
- Subclassed.defaults.size.should == 5
+ Subclassed.defaults.size.should == 6
end
it "should init without raising" do
lambda { Subclassed.new }.should_not raise_error
+ lambda { Subclassed.new(:first_name => 'Michael') }.should_not raise_error
+ end
+
+ it "should share defaults from DashTest" do
+ Subclassed.new.count.should == 0
end
end \ No newline at end of file