Module: Compel::Coercion

Extended by:
Coercion
Included in:
Coercion
Defined in:
lib/compel/coercion/result.rb,
lib/compel/coercion/coercion.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

Defined Under Namespace

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

Instance Method Summary collapse

Instance Method Details

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



35
36
37
38
39
40
41
# File 'lib/compel/coercion/coercion.rb', line 35

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

  coercion_klass = Compel::Coercion.const_get("#{type}")

  coercion_klass.new(value, options).coerce
end

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



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

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

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

  result.coerced
end

#valid?(value, type, options = {}) ⇒ Boolean

Returns:



21
22
23
# File 'lib/compel/coercion/coercion.rb', line 21

def valid?(value, type, options = {})
  coerce(value, type, options).valid?
end