Innate::Traited

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 {Traited#ancestral_trait} you will get nicely inherited configuration, where keys later in the ancestors will take precedence.

@example usage

  class Foo
    include Innate::Traited
    trait :hello => 'Hello'

    def initialize
      trait :hello => 'World!'
    end

    def show
      [class_trait[:hello], trait[:hello], ancestral_trait[:hello]]
    end
  end

  Foo.trait[:hello] # => "Hello"
  foo = Foo.new
  foo.trait[:hello] # => "World!"
  foo.show          # => ["Hello", "World!", "World!"]

Constants

ANCESTRAL_VALUES

Public Class Methods

included(into) click to toggle source
    # File lib/innate/traited.rb, line 31
31:     def self.included(into)
32:       into.extend(self)
33:     end

Public Instance Methods

ancestral_trait() click to toggle source

Builds a trait from all the ancestors, closer ancestors overwrite distant ancestors

@example

 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 # => {
   :three => :drei, :two => :zwei, :one => :eins, :first => :overwritten
 }
    # File lib/innate/traited.rb, line 67
67:     def ancestral_trait
68:       klass = self.kind_of?(Module) ? self : self.class
69:       ANCESTRAL_TRAITS[klass] ||=
70:         each_ancestral_trait({}){|hash, trait| hash.update(trait) }
71:     end
ancestral_trait_values(key) click to toggle source
    # File lib/innate/traited.rb, line 73
73:     def ancestral_trait_values(key)
74:       klass = self.kind_of?(Module) ? self : self.class
75:       cache = ANCESTRAL_VALUES[klass] ||= {}
76:       cache[key] ||= each_ancestral_trait([]){|array, trait|
77:         array << trait[key] if trait.key?(key) }
78:     end
class_trait() click to toggle source

trait for self.class if we are an instance

    # File lib/innate/traited.rb, line 88
88:     def class_trait
89:       respond_to?(:ancestors) ? trait : self.class.trait
90:     end
each_ancestral_trait(obj) click to toggle source
    # File lib/innate/traited.rb, line 80
80:     def each_ancestral_trait(obj)
81:       ancs = respond_to?(:ancestors) ? ancestors : self.class.ancestors
82:       ancs.unshift(self)
83:       ancs.reverse_each{|anc| yield(obj, TRAITS[anc]) if TRAITS.key?(anc) }
84:       obj
85:     end
trait(hash = nil) click to toggle source
    # File lib/innate/traited.rb, line 35
35:     def trait(hash = nil)
36:       if hash
37:         TRAITS[self] ||= {}
38:         result = TRAITS[self].merge!(hash)
39:         ANCESTRAL_VALUES.clear
40:         ANCESTRAL_TRAITS.clear
41:         result
42:       else
43:         TRAITS[self] || {}
44:       end
45:     end

Disabled; run with --debug to generate this.

[Validate]

Generated with the Darkfish Rdoc Generator 1.1.6.