lib/innate/traited.rb in manveru-innate-2009.02.25 vs lib/innate/traited.rb in manveru-innate-2009.03.24
- old
+ new
@@ -4,11 +4,12 @@
# It's built on a simple Hash, where keys are objects and the values the
# configuration.
# By using {Traited#ancestral_trait} you will get nicely inherited
# configuration, where keys later in the ancestors will take precedence.
#
- # @usage
+ # @example usage
+ #
# class Foo
# include Innate::Traited
# trait :hello => 'Hello'
#
# def initialize
@@ -58,16 +59,23 @@
#
# Foobar.ancestral_trait
# # => {:three => :drei, :two => :zwei, :one => :eins, :first => :overwritten}
def ancestral_trait
result = {}
+ each_ancestral_trait{|trait| result.merge!(trait) }
+ result
+ end
- ancs = respond_to?(:ancestors) ? ancestors : self.class.ancestors
- ancs.reverse_each do |anc|
- result.update(anc.trait) if anc.respond_to?(:trait)
- end
+ def ancestral_trait_values(key)
+ result = []
+ each_ancestral_trait{|trait| result << trait[key] if trait.key?(key) }
+ result
+ end
- result.merge!(trait)
+ def each_ancestral_trait
+ ancs = respond_to?(:ancestors) ? ancestors : self.class.ancestors
+ ancs.unshift(self)
+ ancs.reverse_each{|anc| yield(TRAITS[anc]) if TRAITS.key?(anc) }
end
# trait for self.class if we are an instance
def class_trait
respond_to?(:ancestors) ? trait : self.class.trait