class RSchema::CoercionWrapper
Builds coercing schemas, by wrapping coercers around an existing schema.
Constants
- RACK_PARAMS
Public Class Methods
new(&initializer)
click to toggle source
# File lib/rschema/coercion_wrapper.rb, line 8 def initialize(&initializer) @builder_by_schema = {} @builder_by_type = {} instance_eval(&initializer) if initializer end
Public Instance Methods
coerce(schema_type, with:)
click to toggle source
# File lib/rschema/coercion_wrapper.rb, line 14 def coerce(schema_type, with:) @builder_by_schema[schema_type] = with end
coerce_type(type, with:)
click to toggle source
# File lib/rschema/coercion_wrapper.rb, line 18 def coerce_type(type, with:) @builder_by_type[type] = with end
wrap(schema)
click to toggle source
# File lib/rschema/coercion_wrapper.rb, line 22 def wrap(schema) wrapped_schema = schema.with_wrapped_subschemas(self) wrap_with_coercer(wrapped_schema) end
Private Instance Methods
builder_for_schema(schema)
click to toggle source
# File lib/rschema/coercion_wrapper.rb, line 29 def builder_for_schema(schema) @builder_by_schema.fetch(schema.class) do builder_for_type(schema.type) if schema.is_a?(Schemas::Type) end end
builder_for_type(type)
click to toggle source
# File lib/rschema/coercion_wrapper.rb, line 35 def builder_for_type(type) # polymorphic lookup type.ancestors.each do |ancestor| builder = @builder_by_type[ancestor] return builder if builder end nil end
wrap_with_coercer(schema)
click to toggle source
# File lib/rschema/coercion_wrapper.rb, line 45 def wrap_with_coercer(schema) builder = builder_for_schema(schema) if builder coercer = builder.build(schema) Schemas::Coercer.new(coercer, schema) else schema end end