Class: Gorillib::Factory::NonConvertingFactory

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

Overview

A NonConvertingFactory accepts objects that are already native, and throws a mismatch error for anything else.

Examples:

ff = Gorillib::Factory::NonConvertingFactory.new(:product => String, :blankish => ->(obj){ obj.nil? })
ff.receive(nil)    #=> nil
ff.receive("bob")  #=> "bob"
ff.receive(:bob)   #=> Gorillib::Factory::FactoryMismatchError: must be an instance of String, got 3

Instance Method Summary collapse

Methods inherited from BaseFactory

blankish?, #initialize, native?, #native?, typename, #typename

Constructor Details

This class inherits a constructor from Gorillib::Factory::BaseFactory

Instance Method Details

#blankish?(obj) ⇒ Boolean

Returns:

  • (Boolean)


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

def blankish?(obj) obj.nil? ; end

#receive(obj) ⇒ Object



166
167
168
169
170
171
172
# File 'lib/gorillib/factories.rb', line 166

def receive(obj)
  return nil  if blankish?(obj)
  return obj  if native?(obj)
  mismatched!(obj, "must be an instance of #{product},")
rescue NoMethodError => err
  mismatched!(obj, err.message, err.backtrace)
end