Traited

Traited allows you to add traits to your classes allowing you to create a configuration similar to using class variables. Traited is based on a simple Hash where keys are objects and values are whatever you set.

Traited also allows you to get all traits across an ancestral chain, where keys later in the ancestors will take precedence.

This class was taken directly out of the core of the Innate, but I found it to be useful so I'm extracting it and maintaining it myself. It was written by Michael Fellinger.

Example

class Foo
  include 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!"]

Installation

gem install traited