Fabrik
Traits for Ruby 2, as described in Traits: A Mechanism for Fine-grained Reuse by Ducasse, Nierstrasz, Schärli, Wuyts and Black.
Usage
A class becomes a trait when it extends Fabrik::Trait
.
class Tiger
extend Fabrik::Trait
end
Traits provide methods within a provides
block.
class Panthera
extend Fabrik::Trait
provides do
def roar; :roar! end
end
end
class Tiger
extend Fabrik::Trait
provides do
def mother; :tigress end
def father; :tiger end
end
end
class Lion
extend Fabrik::Trait
provides do
def mother; :lioness end
def father; :lion end
end
end
A class uses traits by extending Fabrik::Composer
and composing the traits.
class Tigon
extend Fabrik::Composer
compose Panthera, Tiger, Lion
end
tigon = Tigon.new
tigon.roar # => :roar!
tigon.mother # => raises ConflictingMethods error
Conflicting methods must be resolved, by exclusion or aliasing during composition, or by overriding in the composing class.
class Tigon
extend Fabrik::Composer
compose Panthera,
Tiger.methods(exclude: :mother),
Lion.methods(exclude: :father)
end
tigon = Tigon.new
tigon.roar # => :roar!
tigon.mother # => :lioness
tigon.father # => :tiger
View the specs for further examples, including composable traits and traits that provide methods from shared modules.
Credits
Built by Joe Corcoran.
License
MIT.