Class: Membrane::Schema::Record
- Defined in:
- lib/membrane/schema/record.rb
Instance Attribute Summary collapse
-
#optional_keys ⇒ Object
readonly
Returns the value of attribute optional_keys.
-
#schemas ⇒ Object
readonly
Returns the value of attribute schemas.
Instance Method Summary collapse
-
#initialize(schemas, optional_keys = []) ⇒ Record
constructor
A new instance of Record.
- #validate(object) ⇒ Object
Methods inherited from Base
Constructor Details
#initialize(schemas, optional_keys = []) ⇒ Record
Returns a new instance of Record.
15 16 17 18 |
# File 'lib/membrane/schema/record.rb', line 15 def initialize(schemas, optional_keys = []) @optional_keys = Set.new(optional_keys) @schemas = schemas end |
Instance Attribute Details
#optional_keys ⇒ Object (readonly)
Returns the value of attribute optional_keys.
13 14 15 |
# File 'lib/membrane/schema/record.rb', line 13 def optional_keys @optional_keys end |
#schemas ⇒ Object (readonly)
Returns the value of attribute schemas.
12 13 14 |
# File 'lib/membrane/schema/record.rb', line 12 def schemas @schemas end |
Instance Method Details
#validate(object) ⇒ Object
20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 |
# File 'lib/membrane/schema/record.rb', line 20 def validate(object) unless object.kind_of?(Hash) emsg = "Expected instance of Hash, given instance of #{object.class}" raise Membrane::SchemaValidationError.new(emsg) end key_errors = {} @schemas.each do |k, schema| if object.has_key?(k) begin schema.validate(object[k]) rescue Membrane::SchemaValidationError => e key_errors[k] = e.to_s end elsif !@optional_keys.include?(k) key_errors[k] = "Missing key" end end if key_errors.size > 0 emsg = "{ " + key_errors.map { |k, e| "#{k} => #{e}" }.join(", ") + " }" raise Membrane::SchemaValidationError.new(emsg) end end |