Skip to content

Commit

Permalink
Swapped out class variables to scale the ancestors tree. This allows …
Browse files Browse the repository at this point in the history
…for the super class to not receive properties set on subclasses.
  • Loading branch information
benschwarz authored and Michael Bleigh committed Jan 14, 2010
1 parent a2ecc49 commit 81ec500
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 6 deletions.
22 changes: 18 additions & 4 deletions lib/hashie/dash.rb
Expand Up @@ -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}
Expand All @@ -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
Expand All @@ -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
Expand Down
10 changes: 8 additions & 2 deletions spec/hashie/dash_spec.rb
Expand Up @@ -7,6 +7,7 @@ class DashTest < Hashie::Dash
end

class Subclassed < DashTest
property :last_name
end

describe Hashie::Dash do
Expand Down Expand Up @@ -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

0 comments on commit 81ec500

Please sign in to comment.