Class: RSchema::CoercionWrapper

Inherits:
Object
  • Object
show all
Defined in:
lib/rschema/coercion_wrapper.rb,
lib/rschema/coercion_wrapper/rack_params.rb

Constant Summary collapse

RACK_PARAMS =
CoercionWrapper.new do
  coerce_type Symbol, with: Coercers::Symbol
  coerce_type Integer, with: Coercers::Integer
  coerce_type Float, with: Coercers::Float
  coerce_type Time, with: Coercers::Time
  coerce_type Date, with: Coercers::Date

  coerce Schemas::Boolean, with: Coercers::Boolean
  coerce Schemas::FixedHash, with: Coercers::Chain[
    Coercers::FixedHash::SymbolizeKeys,
    Coercers::FixedHash::RemoveExtraneousAttributes,
    Coercers::FixedHash::DefaultBooleansToFalse,
  ]
end

Instance Method Summary collapse

Constructor Details

#initialize(&initializer) ⇒ CoercionWrapper


3
4
5
6
7
# File 'lib/rschema/coercion_wrapper.rb', line 3

def initialize(&initializer)
  @builder_by_schema = {}
  @builder_by_type = {}
  instance_eval(&initializer) if initializer
end

Instance Method Details

#coerce(schema_type, with:) ⇒ Object


9
10
11
# File 'lib/rschema/coercion_wrapper.rb', line 9

def coerce(schema_type, with:)
  @builder_by_schema[schema_type] = with
end

#coerce_type(type, with:) ⇒ Object


13
14
15
# File 'lib/rschema/coercion_wrapper.rb', line 13

def coerce_type(type, with:)
  @builder_by_type[type] = with
end

#wrap(schema) ⇒ Object


17
18
19
20
# File 'lib/rschema/coercion_wrapper.rb', line 17

def wrap(schema)
  wrapped_schema = schema.with_wrapped_subschemas(self)
  wrap_with_coercer(wrapped_schema)
end