Module: BinData::BasePrimitive::AssertedValuePlugin

Defined in:
lib/bindata/base_primitive.rb

Overview

Logic for the :asserted_value parameter

Instance Method Summary collapse

Instance Method Details

#_valueObject



199
200
201
# File 'lib/bindata/base_primitive.rb', line 199

def _value
  reading? ? @value : eval_parameter(:asserted_value)
end

#assert!Object



217
218
219
# File 'lib/bindata/base_primitive.rb', line 217

def assert!
  assert_value(snapshot)
end

#assert_value(current_value) ⇒ Object



221
222
223
224
225
226
227
228
# File 'lib/bindata/base_primitive.rb', line 221

def assert_value(current_value)
  expected = eval_parameter(:asserted_value, value: current_value)
  if current_value != expected
    raise ValidityError,
          "value is '#{current_value}' but " \
          "expected '#{expected}' for #{debug_name}"
  end
end

#asserted_binary_sObject

The asserted value as a binary string.

Rationale: while reading, #to_binary_s will use the value read in, rather than the :asserted_value. This feature is used by Skip.



208
209
210
# File 'lib/bindata/base_primitive.rb', line 208

def asserted_binary_s
  value_to_binary_string(eval_parameter(:asserted_value))
end

#assign(val) ⇒ Object



194
195
196
197
# File 'lib/bindata/base_primitive.rb', line 194

def assign(val)
  assert_value(val)
  super(val)
end

#do_read(io) ⇒ Object

:nodoc:



212
213
214
215
# File 'lib/bindata/base_primitive.rb', line 212

def do_read(io) # :nodoc:
  super(io)
  assert!
end