Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Swapped out class variables to scale the ancestors tree. This allows …

…for the super class to not receive properties set on subclasses.
  • Loading branch information...
commit 81ec5006cfe2d4c0adc7cecf9940eb6dbb8cbe39 1 parent a2ecc49
@benschwarz benschwarz authored Michael Bleigh committed
Showing with 26 additions and 6 deletions.
  1. +18 −4 lib/hashie/dash.rb
  2. +8 −2 spec/hashie/dash_spec.rb
View
22 lib/hashie/dash.rb
@@ -26,8 +26,8 @@ class Dash < Hashie::Hash
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 @@ def #{property_name}=(val)
# 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 @@ def self.property?(prop)
# 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
View
10 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 @@ class Subclassed < DashTest
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
Please sign in to comment.
Something went wrong with that request. Please try again.