class Gorillib::Factory::BaseFactory

A gorillib Factory should answer to the following:

Public Class Methods

blankish?(obj) click to toggle source
# File lib/gorillib/model/factories.rb, line 82
def self.blankish?(obj)
  obj.nil? || (obj == "")
end
native?(obj) click to toggle source
# File lib/gorillib/model/factories.rb, line 73
def self.native?(obj) self.new.native?(obj) ; end
new(options={}) click to toggle source
# File lib/gorillib/model/factories.rb, line 53
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
typename() click to toggle source
# File lib/gorillib/model/factories.rb, line 60
def self.typename
  @typename ||= ActiveSupport::Inflector.underscore(product.name).to_sym
end

Protected Class Methods

register_factory!(*typenames) click to toggle source
# File lib/gorillib/model/factories.rb, line 120
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

blankish?(obj) click to toggle source

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/model/factories.rb, line 79
def blankish?(obj)
  obj.nil? || (obj == "")
end
native?(obj) click to toggle source

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/model/factories.rb, line 70
def native?(obj)
  obj.is_a?(@product)
end
receive(*args) click to toggle source

performs the actual conversion

# File lib/gorillib/model/factories.rb, line 87
def receive(*args)
  NoMethodError.abstract_method(self)
end
typename() click to toggle source
# File lib/gorillib/model/factories.rb, line 63
def typename ; self.class.typename ; end

Protected Instance Methods

define_blankish_method(blankish) click to toggle source
# File lib/gorillib/model/factories.rb, line 93
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
mismatched!(obj, message=nil, *args) click to toggle source

Raises a FactoryMismatchError.

# File lib/gorillib/model/factories.rb, line 115
def mismatched!(obj, message=nil, *args)
  message ||= "item cannot be converted to #{product}"
  FactoryMismatchError.mismatched!(obj, product, message, *args)
end
redefine(meth, *args, &block) click to toggle source
# File lib/gorillib/model/factories.rb, line 101
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