Sha256: 6c6de7064fc8ab78db632b73a03d1e19de0f996c8fbf0a18e9143f623f3c2b20

Contents?: true

Size: 1.7 KB

Versions: 15

Compression:

Stored size: 1.7 KB

Contents

module PatronusFati
  # 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.
  module FactoryBase
    # Turns the name of a class into it's snake cased equivalent.
    #
    # @param [Object] klass
    # @return [Symbol]
    def class_to_name(klass)
      klass.to_s.split('::').last.scan(/[A-Z][a-z]*/).map(&:downcase)
        .join('_').to_sym
    end

    # 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
    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

    # Placeholder mechanism to allow sub-generators to not generate any
    # warnings for specific types.
    #
    # @return [Array<Symbol>]
    def ignored_types
      []
    end

    # Trigger for when this module gets included to register it with the
    # factory.
    #
    # @param [Object#create] klass
    # @return [Object]
    def included(klass)
      registered_factories[class_to_name(klass)] = klass
    end

    # Returns the hash containing the set of registered factories or
    # initializes it if one doesn't exist.
    #
    # @return [Hash<Symbol=>Object>]
    def registered_factories
      @registered_factories ||= {}
    end
  end
end

Version data entries

15 entries across 15 versions & 1 rubygems

Version Path
patronus_fati-1.3.8 lib/patronus_fati/factory_base.rb
patronus_fati-1.3.7 lib/patronus_fati/factory_base.rb
patronus_fati-1.3.6 lib/patronus_fati/factory_base.rb
patronus_fati-1.3.5 lib/patronus_fati/factory_base.rb
patronus_fati-1.3.4 lib/patronus_fati/factory_base.rb
patronus_fati-1.3.3 lib/patronus_fati/factory_base.rb
patronus_fati-1.3.2 lib/patronus_fati/factory_base.rb
patronus_fati-1.3.1 lib/patronus_fati/factory_base.rb
patronus_fati-1.3.0 lib/patronus_fati/factory_base.rb
patronus_fati-1.2.2 lib/patronus_fati/factory_base.rb
patronus_fati-1.2.1 lib/patronus_fati/factory_base.rb
patronus_fati-1.2.0 lib/patronus_fati/factory_base.rb
patronus_fati-1.1.2 lib/patronus_fati/factory_base.rb
patronus_fati-1.1.1 lib/patronus_fati/factory_base.rb
patronus_fati-1.1.0 lib/patronus_fati/factory_base.rb