Class: Sinatra::Kagero::Props::Schema
- Inherits:
-
Object
- Object
- Sinatra::Kagero::Props::Schema
- Defined in:
- lib/sinatra/kagero/props.rb
Instance Method Summary collapse
- #coerce(input) ⇒ Object
- #dup ⇒ Object
-
#initialize(fields = []) ⇒ Schema
constructor
A new instance of Schema.
- #prop(name, type = nil, required: true, default: MISSING) ⇒ Object
- #to_h ⇒ Object
Constructor Details
#initialize(fields = []) ⇒ Schema
Returns a new instance of Schema.
85 86 87 |
# File 'lib/sinatra/kagero/props.rb', line 85 def initialize(fields = []) @fields = fields end |
Instance Method Details
#coerce(input) ⇒ Object
97 98 99 100 101 102 103 104 105 106 107 |
# File 'lib/sinatra/kagero/props.rb', line 97 def coerce(input) errors = {} output = {} @fields.each do |field| output[field.name] = field.coerce(input, errors) end raise ValidationError, errors unless errors.empty? output end |
#dup ⇒ Object
89 90 91 |
# File 'lib/sinatra/kagero/props.rb', line 89 def dup self.class.new(@fields.dup) end |
#prop(name, type = nil, required: true, default: MISSING) ⇒ Object
93 94 95 |
# File 'lib/sinatra/kagero/props.rb', line 93 def prop(name, type = nil, required: true, default: MISSING) @fields << Field.new(name, type, required: required, default: default) end |
#to_h ⇒ Object
109 110 111 |
# File 'lib/sinatra/kagero/props.rb', line 109 def to_h @fields.map { |field| [field.name, field.type] }.to_h end |