Module: Schemata::Dsl

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

Instance Method Summary collapse

Instance Method Details

#define_aux_schema(&blk) ⇒ Object



183
184
185
186
187
188
189
190
# File 'lib/schemata/common/msgbase.rb', line 183

def define_aux_schema(&blk)
  aux_schema = Membrane::SchemaParser.parse(&blk)
  unless aux_schema.kind_of? Membrane::Schema::Record
    Schemata::SchemaDefinitionError.new("Schema must be a hash")
  end

  self::const_set(:AUX_SCHEMA, aux_schema)
end

#define_constant(constant_name, constant_value) ⇒ Object



251
252
253
# File 'lib/schemata/common/msgbase.rb', line 251

def define_constant(constant_name, constant_value)
  self.const_set(constant_name, constant_value)
end

#define_generate_old_fields(&blk) ⇒ Object



213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
# File 'lib/schemata/common/msgbase.rb', line 213

def define_generate_old_fields(&blk)
  self.send(:define_method, :generate_old_fields) do
    if self.class.aux_schema && aux_data.empty?
      raise Schemata::DecodeError.new("Necessary aux_data missing")
    end
    old_fields = blk.call(self)

    msg_contents = contents
    msg_contents.update(old_fields)
    msg_obj = self.class.previous_version.new(msg_contents)

    msg_obj.validate_contents
    return msg_obj, old_fields
  end
end

#define_min_version(min_version) ⇒ Object



192
193
194
195
196
197
# File 'lib/schemata/common/msgbase.rb', line 192

def define_min_version(min_version)
  unless min_version.is_a? Integer
    raise SchemaDefinitionError.new("Min version must be an integer")
  end
  const_set(:MIN_VERSION_ALLOWED, min_version)
end

#define_mock_values(hash = nil, &blk) ⇒ Object



229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
# File 'lib/schemata/common/msgbase.rb', line 229

def define_mock_values(hash=nil, &blk)
  if (hash && blk) ||  (!hash && !blk)
    # value defined twice or not at all
    raise SchemaDefinitionError.new("Mock values incorrectly defined")
  end

  hash = blk.call if blk

  # Validate a sample of the mock values.
  mock = {}
  hash.each do |key, value|
    mock[key] = value.respond_to?("call") ? value.call : value
  end

  begin
    self.schema.validate(mock)
    define_constant(:MOCK_VALUES, hash)
  rescue Membrane::SchemaValidationError => e
    raise SchemaDefinitionError.new("Sample mock values do not match schema: #{e}")
  end
end

#define_schema(&blk) ⇒ Object



175
176
177
178
179
180
181
# File 'lib/schemata/common/msgbase.rb', line 175

def define_schema(&blk)
  schema = Membrane::SchemaParser.parse(&blk)
  unless schema.kind_of? Membrane::Schema::Record
    Schemata::SchemaDefinitionError.new("Schema must be a hash")
  end
  self::const_set(:SCHEMA, schema)
end

#define_upvert(&blk) ⇒ Object



199
200
201
202
203
204
205
206
207
208
209
210
211
# File 'lib/schemata/common/msgbase.rb', line 199

def define_upvert(&blk)
  eigenclass.send(:define_method, :upvert) do |old_data|
    # No need to validate aux_data because upvert is only called during
    # decode, when aux_data is irrelevant
    begin
      previous_version::SCHEMA.validate(old_data)
    rescue Membrane::SchemaValidationError => e
      raise Schemata::DecodeError.new(e.message)
    end

    blk.call(old_data)
  end
end

#include_preschemataObject



255
256
257
# File 'lib/schemata/common/msgbase.rb', line 255

def include_preschemata
  define_constant(:INCLUDE_PRESCHEMATA, true)
end