lib/gorillib/factories.rb in gorillib-0.4.2 vs lib/gorillib/factories.rb in gorillib-0.5.0

- old
+ new

@@ -30,23 +30,10 @@ return find(type) if options.compact.blank? klass = factory_klasses[type] or raise "You can only supply options #{options} to a Factory-mapped class" klass.new(options) end - # Manufactures objects from their raw attributes hash - # - # A hash with a value for `:_type` is dispatched to the corresponding factory - # Everything else is returned directly - def self.make(obj) - if obj.respond_to?(:has_key?) && (obj.has_key?(:_type) || obj.has_key?('_type')) - factory = Gorillib::Factory(attrs[:_type]) - factory.receive(obj) - else - obj - end - end - def self.register_factory(factory, typenames) typenames.each{|typename| factories[typename] = factory } end def self.register_factory_klass(factory_klass, typenames) @@ -183,10 +170,18 @@ rescue NoMethodError => err mismatched!(obj, err.message, err.backtrace) end end + # __________________________________________________________________________ + # + # Generic Factories + # __________________________________________________________________________ + + # + # Factory that accepts whatever given and uses it directly -- no nothin' + # class ::Whatever < BaseFactory def initialize(options={}) options.slice!(:convert, :blankish) super(options) end @@ -197,9 +192,27 @@ obj end Gorillib::Factory.register_factory(self, [self, :identical, :whatever]) end IdenticalFactory = ::Whatever unless defined?(IdenticalFactory) + + + # Manufactures objects from their raw attributes hash + # + # The hash must have a value for `:_type`, used to retrieve the actual factory + # + class ::GenericModel < BaseFactory + def blankish?(obj) obj.nil? ; end + def native?(obj) false ; end + def receive(attrs, &block) + Gorillib::Model::Validate.hashlike!(attrs){ "attributes for typed object" } + klass = Gorillib::Factory(attrs.fetch(:_type){ attrs.fetch("_type") }) + # + klass.new(attrs, &block) + end + def self.receive(obj) allocate.receive(obj) end + register_factory!(GenericModel, :generic) + end # __________________________________________________________________________ # # Concrete Factories # __________________________________________________________________________