Class: RVGP::Application::DescendantRegistry::ClassRegistry

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
lib/rvgp/application/descendant_registry.rb

Overview

This basic class resembles an array, and is used to house a regsitry of children classes. Typically, this class is instantiated inside of RVGP, at the time a child inherits from a parent.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(opts = {}) ⇒ ClassRegistry

Declare the registry, and initialize with the relevant options

Parameters:

  • opts (Hash) (defaults to: {})

    what options to configure this registry with

Options Hash (opts):

  • :accessors (Hash<String, Proc>)

    what methods to dispatch to the instances of this collection



31
32
33
34
# File 'lib/rvgp/application/descendant_registry.rb', line 31

def initialize(opts = {})
  @classes = []
  @accessors = opts[:accessors] || {}
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(name) ⇒ Object

In the case that a method is called on this registry, that isn’t explicitly defined, this method checks the accessors provided in #initialize to see if there’s a matching block, indexing to the name of the missing method. And calls that.

Parameters:

  • name (Symbol)

    The method attempting to be called



65
66
67
# File 'lib/rvgp/application/descendant_registry.rb', line 65

def method_missing(name)
  @accessors.key?(name) ? @accessors[name].call(self) : super(name)
end

Instance Attribute Details

#classesArray<Object> (readonly)

The undecorated classes that are contained in this object

Returns:

  • (Array<Object>)

    the current value of classes



23
24
25
# File 'lib/rvgp/application/descendant_registry.rb', line 23

def classes
  @classes
end

Instance Method Details

#add(klass) ⇒ void

This method returns an undefined value.

Add the provided object to the #classes collection

Parameters:

  • klass (Object)

    The object class, you wish to add



46
47
48
# File 'lib/rvgp/application/descendant_registry.rb', line 46

def add(klass)
  @classes << klass
end

#each {|obj| ... } ⇒ void

This method returns an undefined value.

Call the provided block, for each element of the registry

Yields:

  • (obj)

    The block you wish to call, once per element of the registry



39
40
41
# File 'lib/rvgp/application/descendant_registry.rb', line 39

def each(&block)
  classes.each(&block)
end

#namesArray<String>

The names of all the classes that are defined in this registry

Returns:



52
53
54
# File 'lib/rvgp/application/descendant_registry.rb', line 52

def names
  classes.collect(&:name)
end