Class: Schemata::MessageBase::ValidatingContainer

Inherits:
Object
  • Object
show all
Defined in:
lib/schemata/common/msgbase.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(data = {}) ⇒ ValidatingContainer

Returns a new instance of ValidatingContainer.



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/schemata/common/msgbase.rb', line 8

def initialize(data = {})
  data ||= {}
  @schema = self.class.const_get(:SCHEMA)
  @contents = {}

  data.each do |key, field_value|
    field_schema = @schema.schemas[key]
    next unless field_schema

    begin
      field_schema.validate(field_value)
    rescue Membrane::SchemaValidationError => e
      raise Schemata::UpdateAttributeError.new(e.message)
    end

    @contents[key] = Schemata::HashCopyHelpers.deep_copy(field_value)
  end
end

Class Method Details

.define(schema) ⇒ Object



27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/schemata/common/msgbase.rb', line 27

def self.define(schema)
  vc_klass = Class.new(self)
  vc_klass.const_set(:SCHEMA, schema)
  schema.schemas.each do |key, field_schema|
    vc_klass.send(:define_method, key) do
      if @contents[key]
        return Schemata::HashCopyHelpers.deep_copy(@contents[key])
      end
      nil
    end

    vc_klass.send(:define_method, "#{key}=") do |field_value|
      begin
        field_schema.validate(field_value)
      rescue Membrane::SchemaValidationError => e
        raise Schemata::UpdateAttributeError.new(e.message)
      end
      @contents[key] = Schemata::HashCopyHelpers.deep_copy(field_value)
      field_value
    end
  end
  vc_klass
end

Instance Method Details

#contentsObject



51
52
53
# File 'lib/schemata/common/msgbase.rb', line 51

def contents
  Schemata::HashCopyHelpers.deep_copy(@contents)
end

#empty?Boolean

Returns:

  • (Boolean)


55
56
57
# File 'lib/schemata/common/msgbase.rb', line 55

def empty?
  @contents.empty?
end

#validateObject



59
60
61
# File 'lib/schemata/common/msgbase.rb', line 59

def validate
  @schema.validate(@contents)
end