module PatronusFati::FactoryBase

This module provides the basis for an automatic Factory registration and generation system. Other modules that wish to make use of this functionality should extend this module. Those modules should then in turn be included by their respective generators.

Public Instance Methods

class_to_name(klass) click to toggle source

Turns the name of a class into it's snake cased equivalent.

@param [Object] klass @return [Symbol]

# File lib/patronus_fati/factory_base.rb, line 11
def class_to_name(klass)
  klass.to_s.split('::').last.scan(/[A-Z][a-z]*/).map(&:downcase)
    .join('_').to_sym
end
factory(type, opts = {}) click to toggle source

Factory method for triggering the lookup and return of the specific requested type of factory.

@param [Symbol] type Type of generator to create @param [Hash<Symbol=>String>] opts

# File lib/patronus_fati/factory_base.rb, line 21
def factory(type, opts = {})
  return if ignored_types.include?(type)
  if registered_factories[type].nil?
    PatronusFati.logger.warn("Unknown factory #{type} (Available: #{registered_factories.keys})")
    return
  end
  registered_factories[type].process(opts)
end
ignored_types() click to toggle source

Placeholder mechanism to allow sub-generators to not generate any warnings for specific types.

@return [Array<Symbol>]

# File lib/patronus_fati/factory_base.rb, line 34
def ignored_types
  []
end
included(klass) click to toggle source

Trigger for when this module gets included to register it with the factory.

@param [Object#create] klass @return [Object]

# File lib/patronus_fati/factory_base.rb, line 43
def included(klass)
  registered_factories[class_to_name(klass)] = klass
end
registered_factories() click to toggle source

Returns the hash containing the set of registered factories or initializes it if one doesn't exist.

@return [Hash<Symbol=>Object>]

# File lib/patronus_fati/factory_base.rb, line 51
def registered_factories
  @registered_factories ||= {}
end