Class: Membrane::Schema::Tuple

Inherits:
Base
  • Object
show all
Defined in:
lib/membrane/schema/tuple.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods inherited from Base

#deparse

Constructor Details

#initialize(*elem_schemas) ⇒ Tuple

Returns a new instance of Tuple.



12
13
14
# File 'lib/membrane/schema/tuple.rb', line 12

def initialize(*elem_schemas)
  @elem_schemas = elem_schemas
end

Instance Attribute Details

#elem_schemasObject (readonly)

Returns the value of attribute elem_schemas.



10
11
12
# File 'lib/membrane/schema/tuple.rb', line 10

def elem_schemas
  @elem_schemas
end

Instance Method Details

#validate(object) ⇒ Object



16
17
18
19
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
45
46
47
# File 'lib/membrane/schema/tuple.rb', line 16

def validate(object)
  if !object.kind_of?(Array)
    emsg = "Expected instance of Array, given instance of #{object.class}"
    raise Membrane::SchemaValidationError.new(emsg)
  end

  expected = @elem_schemas.length
  actual = object.length

  if actual != expected
    emsg = "Expected #{expected} element(s), given #{actual}"
    raise Membrane::SchemaValidationError.new(emsg)
  end

  errors = {}

  @elem_schemas.each_with_index do |schema, ii|
    begin
      schema.validate(object[ii])
    rescue Membrane::SchemaValidationError => e
      errors[ii] = e
    end
  end

  if errors.size > 0
    emsg = "There were errors at the following indices: " \
           + errors.map { |ii, err| "#{ii} => #{err}" }.join(", ")
    raise Membrane::SchemaValidationError.new(emsg)
  end

  nil
end