Module: Extant::Coercers

Defined in:
lib/extant/coercers.rb,
lib/extant/coercers/base.rb,
lib/extant/coercers/hash.rb,
lib/extant/coercers/time.rb,
lib/extant/coercers/uuid.rb,
lib/extant/coercers/array.rb,
lib/extant/coercers/float.rb,
lib/extant/coercers/fixnum.rb,
lib/extant/coercers/string.rb,
lib/extant/coercers/symbol.rb,
lib/extant/coercers/boolean.rb,
lib/extant/coercers/integer.rb,
lib/extant/coercers/date_time.rb

Defined Under Namespace

Classes: Array, ArrayCoercerBuilder, Base, Boolean, DateTime, Fixnum, Float, Hash, HashCoercerBuilder, Integer, String, Symbol, Time, Uuid

Class Method Summary collapse

Class Method Details

.find(type) ⇒ Object



3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/extant/coercers.rb', line 3

def self.find(type)
  if type.is_a?(::Hash)
    coercer_options = {
      hash_format: type
    }

    type = HashCoercerBuilder.build(coercer_options)
  end

  if type.is_a?(::Array)
    coercer_options = {
      array_format: type
    }

    type = ArrayCoercerBuilder.build(coercer_options)
  end

  if type.is_a?(Class) && type.ancestors.include?(Extant::Coercers::Base)
    coercer_class = type
  else
    coercer_class = Object.const_get("Extant::Coercers::#{type}")

    unless coercer_class.method_defined?(:coerce)
      raise "#{type} must respond to #coerce"
    end
  end

  coercer_class
end