Module: Schemata::MessageBase

Defined in:
lib/schemata/common/msgbase.rb

Defined Under Namespace

Classes: ValidatingContainer

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.included(klass) ⇒ Object



133
134
135
136
# File 'lib/schemata/common/msgbase.rb', line 133

def self.included(klass)
  klass.extend(Schemata::ClassMethods)
  klass.extend(Dsl)
end

Instance Method Details

#aux_dataObject



124
125
126
# File 'lib/schemata/common/msgbase.rb', line 124

def aux_data
  @aux_contents
end

#aux_vc_klassObject



68
69
70
# File 'lib/schemata/common/msgbase.rb', line 68

def aux_vc_klass
  return self.class.const_get(:AUX_VC_KLASS) if self.class.aux_schema
end

#contentsObject



120
121
122
# File 'lib/schemata/common/msgbase.rb', line 120

def contents
  @contents.contents
end

#encodeObject



79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
# File 'lib/schemata/common/msgbase.rb', line 79

def encode
  begin
    validate_contents
    validate_aux_data
  rescue Membrane::SchemaValidationError => e
    raise Schemata::EncodeError.new(e.message)
  end

  msg_type = message_type
  curr_version = self.class.version
  min_version = self.class::MIN_VERSION_ALLOWED

  msg = { "V#{curr_version}" => contents }
  curr_msg_obj = self
  (min_version...curr_version).reverse_each do |v|
    curr_msg_obj, old_fields =
      curr_msg_obj.generate_old_fields
     msg["V#{v}"] = old_fields
  end
  msg["min_version"] = min_version

  if include_preschemata?
    msg["V#{curr_version}"].each do |k, v|
      msg[k] = v
    end
  end
  Yajl::Encoder.encode(msg)
end

#include_preschemata?Boolean

Returns:

  • (Boolean)


108
109
110
# File 'lib/schemata/common/msgbase.rb', line 108

def include_preschemata?
  self.class.const_get(:INCLUDE_PRESCHEMATA)
end

#initialize(msg_data_hash = nil, aux_data_hash = nil) ⇒ Object



72
73
74
75
76
77
# File 'lib/schemata/common/msgbase.rb', line 72

def initialize(msg_data_hash = nil, aux_data_hash = nil)
  @contents = vc_klass.new(msg_data_hash)
  if self.class.aux_schema
    @aux_contents = aux_vc_klass.new(aux_data_hash)
  end
end

#message_typeObject



128
129
130
131
# File 'lib/schemata/common/msgbase.rb', line 128

def message_type
  _, component, msg_type, version = self.class.name.split("::")
  Schemata::const_get(component)::const_get(msg_type)
end

#validate_aux_dataObject



116
117
118
# File 'lib/schemata/common/msgbase.rb', line 116

def validate_aux_data
  @aux_contents.validate if self.class.aux_schema
end

#validate_contentsObject



112
113
114
# File 'lib/schemata/common/msgbase.rb', line 112

def validate_contents
  @contents.validate
end

#vc_klassObject



64
65
66
# File 'lib/schemata/common/msgbase.rb', line 64

def vc_klass
  self.class.const_get(:VC_KLASS)
end