Module: Innate::Traited
- Included in:
- Helper::Aspect::SingletonMethods, Node
- Defined in:
- lib/innate/traited.rb
Overview
Traited helps you doing configuration similar to class variables.
It’s built on a simple Hash, where keys are objects and the values the configuration. By using #ancestral_trait you will get nicely inherited configuration, where keys later in the ancestors will take precedence.
Constant Summary collapse
- TRAITS =
{}
Class Method Summary collapse
Instance Method Summary collapse
-
#ancestral_trait ⇒ Object
Builds a trait from all the ancestors, closer ancestors overwrite distant ancestors.
- #ancestral_trait_values(key) ⇒ Object
-
#class_trait ⇒ Object
trait for self.class if we are an instance.
- #each_ancestral_trait ⇒ Object
- #trait(hash = nil) ⇒ Object
Class Method Details
.included(into) ⇒ Object
31 32 33 |
# File 'lib/innate/traited.rb', line 31 def self.included(into) into.extend(self) end |
Instance Method Details
#ancestral_trait ⇒ Object
Builds a trait from all the ancestors, closer ancestors overwrite distant ancestors
class Foo
include Innate::Traited
trait :one => :eins, :first => :erstes
end
class Bar < Foo
trait :two => :zwei
end
class Foobar < Bar
trait :three => :drei, :first => :overwritten
end
Foobar.ancestral_trait # => => :drei, :two => :zwei, :one => :eins, :first => :overwritten
62 63 64 65 66 |
# File 'lib/innate/traited.rb', line 62 def ancestral_trait result = {} each_ancestral_trait{|trait| result.merge!(trait) } result end |
#ancestral_trait_values(key) ⇒ Object
68 69 70 71 72 |
# File 'lib/innate/traited.rb', line 68 def ancestral_trait_values(key) result = [] each_ancestral_trait{|trait| result << trait[key] if trait.key?(key) } result end |
#class_trait ⇒ Object
trait for self.class if we are an instance
81 82 83 |
# File 'lib/innate/traited.rb', line 81 def class_trait respond_to?(:ancestors) ? trait : self.class.trait end |