Class: Object

Inherits:
BasicObject
Defined in:
lib/weewar-ai/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



58
59
60
61
62
63
64
65
# File 'lib/weewar-ai/traits.rb', line 58

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

#class_traitObject

trait for self.class



69
70
71
72
73
74
75
# File 'lib/weewar-ai/traits.rb', line 69

def class_trait
  if respond_to?(:ancestors)
    trait
  else
    self.class.trait
  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


30
31
32
33
34
35
36
# File 'lib/weewar-ai/traits.rb', line 30

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