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