Class: Deimos::SchemaCoercer
- Inherits:
-
Object
- Object
- Deimos::SchemaCoercer
- Defined in:
- lib/deimos/schema_coercer.rb
Overview
Class to coerce values in a payload to match a schema.
Instance Method Summary collapse
- #coerce(payload) ⇒ HashWithIndifferentAccess
-
#initialize(schema) ⇒ SchemaCoercer
constructor
A new instance of SchemaCoercer.
Constructor Details
#initialize(schema) ⇒ SchemaCoercer
Returns a new instance of SchemaCoercer.
7 8 9 |
# File 'lib/deimos/schema_coercer.rb', line 7 def initialize(schema) @schema = schema end |
Instance Method Details
#coerce(payload) ⇒ HashWithIndifferentAccess
13 14 15 16 17 18 19 20 21 22 23 |
# File 'lib/deimos/schema_coercer.rb', line 13 def coerce(payload) result = {} @schema.fields.each do |field| name = field.name next unless payload.key?(name) val = payload[name] result[name] = _coerce_type(field.type, val) end result.with_indifferent_access end |