Module: Compel::Coercion

Extended by:
Coercion
Included in:
Coercion
Defined in:
lib/compel/coercion/result.rb,
lib/compel/coercion/coercion.rb,
lib/compel/coercion/types/any.rb,
lib/compel/coercion/nil_result.rb,
lib/compel/coercion/types/date.rb,
lib/compel/coercion/types/hash.rb,
lib/compel/coercion/types/json.rb,
lib/compel/coercion/types/time.rb,
lib/compel/coercion/types/type.rb,
lib/compel/coercion/types/array.rb,
lib/compel/coercion/types/float.rb,
lib/compel/coercion/types/regexp.rb,
lib/compel/coercion/types/string.rb,
lib/compel/coercion/types/boolean.rb,
lib/compel/coercion/types/integer.rb,
lib/compel/coercion/types/datetime.rb,
lib/compel/coercion/types/date_type.rb

Defined Under Namespace

Classes: Any, Array, Boolean, Date, DateTime, DateType, Float, Hash, Integer, JSON, NilResult, Regexp, Result, String, Time, Type

Instance Method Summary collapse

Instance Method Details

#coerce(value, type, options = {}) ⇒ Object



33
34
35
36
37
# File 'lib/compel/coercion/coercion.rb', line 33

def coerce(value, type, options = {})
  return Coercion::NilResult.new if value.nil?

  type.coerce(value, options)
end

#coerce!(value, type, options = {}) ⇒ Object



23
24
25
26
27
28
29
30
31
# File 'lib/compel/coercion/coercion.rb', line 23

def coerce!(value, type, options = {})
  result = coerce(value, type, options)

  unless result.valid?
    raise Compel::TypeError, result.error
  end

  result.coerced
end