Class: Gorillib::Factory::BaseFactory

Inherits:
Object
  • Object
show all
Defined in:
lib/gorillib/factories.rb

Overview

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

Direct Known Subclasses

ConvertingFactory, NonConvertingFactory

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ BaseFactory

Returns a new instance of BaseFactory.



61
62
63
64
65
66
# 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

Class Method Details

.blankish?(obj) ⇒ Boolean

Returns:

  • (Boolean)


90
91
92
# File 'lib/gorillib/factories.rb', line 90

def self.blankish?(obj)
  obj.nil? || (obj == "")
end

.native?(obj) ⇒ Boolean

Returns:

  • (Boolean)


81
# File 'lib/gorillib/factories.rb', line 81

def self.native?(obj) self.new.native?(obj) ; end

.typenameObject



68
69
70
# File 'lib/gorillib/factories.rb', line 68

def self.typename
  @typename ||= Gorillib::Inflector.underscore(product.name).to_sym
end

Instance Method Details

#blankish?(obj) ⇒ true, false

A blankish object should be converted to nil, not a value

Parameters:

  • obj (Object)

    the object to convert and receive

Returns:

  • (true, false)

    true if the item is equivalent to a nil value



87
88
89
# File 'lib/gorillib/factories.rb', line 87

def blankish?(obj)
  obj.nil? || (obj == "")
end

#native?(obj) ⇒ true, false

A native object does not need any transformation; it is accepted directly. By default, an object is native if it is_a?(product)

Parameters:

  • obj (Object)

    the object that will be received

Returns:

  • (true, false)

    true if the item does not need conversion



78
79
80
# File 'lib/gorillib/factories.rb', line 78

def native?(obj)
  obj.is_a?(@product)
end

#receive(*args) ⇒ Object

performs the actual conversion



95
96
97
# File 'lib/gorillib/factories.rb', line 95

def receive(*args)
  NoMethodError.abstract_method(self)
end

#typenameObject



71
# File 'lib/gorillib/factories.rb', line 71

def typename ; self.class.typename ; end