Class: Object

Inherits:
BasicObject
Defined in:
lib/m4dbi/traits.rb

Overview

Extensions for Object

Instance Method Summary collapse

Instance Method Details

#ancestral_traitObject

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

class Foo

trait :one => :eins
trait :first => :erstes

end

class Bar < Foo

trait :two => :zwei

end

class Foobar < Bar

trait :three => :drei
trait :first => :overwritten

end

Foobar.ancestral_trait :two=>:zwei, :one=>:eins, :first=>:overwritten



60
61
62
63
64
65
66
67
# File 'lib/m4dbi/traits.rb', line 60

def ancestral_trait
  if respond_to?(:ancestors)
    ancs = ancestors
  else
    ancs = self.class.ancestors
  end
  ancs.reverse.inject({}){|s,v| s.merge(v.trait)}.merge(trait)
end

#ancestral_trait_class_reader(*names) ⇒ Object



85
86
87
88
89
90
# File 'lib/m4dbi/traits.rb', line 85

def ancestral_trait_class_reader( *names )
  names.each do |name|
    name = name.to_sym
    meta_def( name ) { ancestral_trait[ name ] }
  end
end

#ancestral_trait_reader(*names) ⇒ Object

end



79
80
81
82
83
84
# File 'lib/m4dbi/traits.rb', line 79

def ancestral_trait_reader( *names )
  names.each do |name|
    name = name.to_sym
    define_method( name ) { ancestral_trait[ name ] }
  end
end

#trait(hash = nil) ⇒ Object

Adds a method to Object to annotate your objects with certain traits. It's basically a simple Hash that takes the current object as key

Example:

class Foo
  trait :instance => false

  def initialize
    trait :instance => true
  end
end

Foo.trait[:instance]
# false

foo = Foo.new
foo.trait[:instance]
# true


32
33
34
35
36
37
38
# File 'lib/m4dbi/traits.rb', line 32

def trait hash = nil
  if hash
    Traits[self].merge! hash
  else
    Traits[self]
  end
end