Project

fabrik

0.01
No commit activity in last 3 years
No release in over 3 years
Traits for Ruby 2, as described in the paper "Traits: A Mechanism for Fine-grained Reuse" by Ducasse, Nierstrasz, Schärli, Wuyts and Black.
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
 Dependencies

Development

~> 3.0
 Project Readme

Fabrik

Build Status

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

Alternatively, a trait can be created with Fabrik::Trait.build.

Lion = Fabrik::Trait.build do
  def mother; :lioness end
  def father; :lion 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[exclude: :mother],
          Lion[exclude: :father]
end
tigon = Tigon.new
tigon.roar    # => :roar!
tigon.mother  # => :lioness
tigon.father  # => :tiger

Traits can be composed from other traits in the same way:

class TigonTrait
  extend Fabrik::Trait
  compose Panthera,
          Tiger[exclude: :mother],
          Lion[exclude: :father]
end

Or by using .build:

TigonTrait = Fabrik::Trait.build(Panthera, Tiger[exclude: :mother], Lion[exclude: :father]) do
  def scratch; :scratched end
end

View the specs for further examples, including composable traits and traits that provide methods from shared modules.

Credits

Built by Joe Corcoran.

License

MIT.