class Gorillib::Factory::BaseFactory
A gorillib Factory
should answer to the following:
-
‘typename` – a handle (symbol, lowercased-underscored) naming this type
-
‘native?` – native objects do not need type-conversion
-
‘blankish?` – blankish objects are type-converted to a `nil` value
-
‘product` – the class of objects produced when non-blank
-
‘receive` – performs the actual conversion
Public Class Methods
# File lib/gorillib/factories.rb, line 90 def self.blankish?(obj) obj.nil? || (obj == "") end
# File lib/gorillib/factories.rb, line 81 def self.native?(obj) self.new.native?(obj) ; end
# File lib/gorillib/factories.rb, line 61 def initialize(options={}) @product = options.delete(:product){ self.class.product } define_blankish_method(options.delete(:blankish)) if options.has_key?(:blankish) redefine(:convert, options.delete(:convert)) if options.has_key?(:convert) warn "Unknown options #{options.keys}" unless options.empty? end
# File lib/gorillib/factories.rb, line 68 def self.typename @typename ||= Gorillib::Inflector.underscore(product.name).to_sym end
Protected Class Methods
# File lib/gorillib/factories.rb, line 128 def self.register_factory!(*typenames) typenames = [typename, product] if typenames.empty? Gorillib::Factory.register_factory_klass(self, typenames) Gorillib::Factory.register_factory( self.new, typenames) end
Public Instance Methods
A ‘blankish` object should be converted to `nil`, not a value
@param [Object] obj the object to convert and receive @return [true, false] true if the item is equivalent to a nil value
# File lib/gorillib/factories.rb, line 87 def blankish?(obj) obj.nil? || (obj == "") end
A ‘native` object does not need any transformation; it is accepted directly. By default, an object is native if it `is_a?(product)`
@param obj [Object] the object that will be received @return [true, false] true if the item does not need conversion
# File lib/gorillib/factories.rb, line 78 def native?(obj) obj.is_a?(@product) end
performs the actual conversion
# File lib/gorillib/factories.rb, line 95 def receive(*args) NoMethodError.abstract_method(self) end
# File lib/gorillib/factories.rb, line 71 def typename ; self.class.typename ; end
Protected Instance Methods
# File lib/gorillib/factories.rb, line 101 def define_blankish_method(blankish) FactoryMismatchError.check_type!(blankish, [Proc, Method, :include?]) if blankish.respond_to?(:include?) then meth = ->(val){ blankish.include?(val) } else meth = blankish ; end define_singleton_method(:blankish?, meth) end
Raises a FactoryMismatchError
.
# File lib/gorillib/factories.rb, line 123 def mismatched!(obj, message=nil, *args) message ||= "item cannot be converted to #{product}" FactoryMismatchError.mismatched!(obj, product, message, *args) end
# File lib/gorillib/factories.rb, line 109 def redefine(meth, *args, &block) if args.present? val = args.first case when block_given? then raise ArgumentError, "Pass a block or a value, not both" when val.is_a?(Proc) || val.is_a?(Method) then block = val else block = ->(*){ val.try_dup } end end self.define_singleton_method(meth, &block) self end