Class: Sinatra::Kagero::Props::Schema

Inherits:
Object
  • Object
show all
Defined in:
lib/sinatra/kagero/props.rb

Instance Method Summary collapse

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

Raises:



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

#dupObject



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_hObject



109
110
111
# File 'lib/sinatra/kagero/props.rb', line 109

def to_h
  @fields.map { |field| [field.name, field.type] }.to_h
end