Module: Releasy::Mixins::Register

Extended by:
Forwardable
Includes:
Enumerable
Included in:
Builders, Deployers, Packagers
Defined in:
lib/releasy/mixins/register.rb

Overview

Maintains a registry of classes within a module.

Examples:

module Frogs
  extend Releasy::Mixins::Register
end

class Frogs::BlueFrog
  TYPE = :blue # Type must be defined before registering.
  Frogs.register self
end

class Frogs::RedFrog
  TYPE = :red # Type must be defined before registering.
  Frogs.register self
end

Frogs[:blue]          #=> Frogs::BlueFrog
Frogs.has_type? :blue #=> true
Frogs.types           #=> [:blue, :red]
Frogs.values          #=> [Frogs::BlueFrog, Frogs::RedFrog]

Instance Method Summary (collapse)

Instance Method Details

- (Object) register(klass)

Register a class with this register of classes of that type.

Parameters:

  • klass (Object)

    Object, which is defined within the namespace being registered with.

Raises:

  • (TypeError)


36
37
38
39
40
# File 'lib/releasy/mixins/register.rb', line 36

def register(klass)
  raise TypeError, "Can only register classes" unless klass.is_a? Class
  raise ArgumentError, "To register, a class must have TYPE defined" unless klass.const_defined? :TYPE
  registered[klass::TYPE] = klass
end