Class: RSchema::CoercionWrapper

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

Overview

Builds coercing schemas, by wrapping coercers around an existing schema.

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::Maybe, with: Coercers::NilEmptyStrings
  coerce Schemas::Boolean, with: Coercers::Boolean
  coerce Schemas::FixedHash, with: Coercers::Chain[
    Coercers::FixedHash::SymbolizeKeys,
    Coercers::FixedHash::RemoveExtraneousAttributes,
    Coercers::FixedHash::DefaultBooleansToFalse,
    Coercers::FixedHash::DefaultArraysToEmpty,
  ]
end

Instance Method Summary collapse

Constructor Details

#initialize(&initializer) ⇒ CoercionWrapper

Returns a new instance of CoercionWrapper.



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

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

Instance Method Details

#coerce(schema_type, with:) ⇒ Object



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

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

#coerce_type(type, with:) ⇒ Object



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

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

#wrap(schema) ⇒ Object



22
23
24
25
# File 'lib/rschema/coercion_wrapper.rb', line 22

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