Sha256: 2737a606f2bda15040b865e15657a94502728b47f89b18d4cb1dad5eed3edf0b

Contents?: true

Size: 1.71 KB

Versions: 44

Compression:

Stored size: 1.71 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>] options
    def factory(type, opts = {})
      return if ignored_types.include?(type)
      if registered_factories[type].nil?
        warn("Unknown factory #{type} (Available: #{registered_factories.keys})")
        #puts opts.inspect
        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

44 entries across 44 versions & 1 rubygems

Version Path
patronus_fati-0.9.24 lib/patronus_fati/factory_base.rb
patronus_fati-0.9.23 lib/patronus_fati/factory_base.rb
patronus_fati-0.9.22 lib/patronus_fati/factory_base.rb
patronus_fati-0.9.21 lib/patronus_fati/factory_base.rb
patronus_fati-0.9.20 lib/patronus_fati/factory_base.rb
patronus_fati-0.9.19 lib/patronus_fati/factory_base.rb
patronus_fati-0.9.18 lib/patronus_fati/factory_base.rb
patronus_fati-0.9.17 lib/patronus_fati/factory_base.rb
patronus_fati-0.9.16 lib/patronus_fati/factory_base.rb
patronus_fati-0.9.15 lib/patronus_fati/factory_base.rb
patronus_fati-0.9.14 lib/patronus_fati/factory_base.rb
patronus_fati-0.9.13 lib/patronus_fati/factory_base.rb
patronus_fati-0.9.12 lib/patronus_fati/factory_base.rb
patronus_fati-0.9.11 lib/patronus_fati/factory_base.rb
patronus_fati-0.9.10 lib/patronus_fati/factory_base.rb
patronus_fati-0.9.9 lib/patronus_fati/factory_base.rb
patronus_fati-0.9.8 lib/patronus_fati/factory_base.rb
patronus_fati-0.9.7 lib/patronus_fati/factory_base.rb
patronus_fati-0.9.6 lib/patronus_fati/factory_base.rb
patronus_fati-0.9.5 lib/patronus_fati/factory_base.rb