1

Is there a way to validate the fields being set on a BinData::Record during initialization? The check_value for each parameter appears to only be evaluated after reading from an input stream

class Foo < BinData::Record
   uint8 :bar,:check_value=>lambda{raise 'Here is an error'}
end

Foo.new(:bar=>5)  #Does not raise validation exception

I've tried adding an 'initialize_instance' where it does a read on 'to_binary_s' but it throws some other errors, presumably because the object isn't fully instantiated.

the Tin Man
  • 158,662
  • 42
  • 215
  • 303
FooManChew
  • 21
  • 2

1 Answers1

0

Write a def initialize method. Then put the validation inside it. The initialize is run on object instantiation, so the check must occur.

the Tin Man
  • 158,662
  • 42
  • 215
  • 303
MurifoX
  • 14,991
  • 3
  • 36
  • 60
  • Sorry, its not as simple as that. The library checks to see if an initialize method has been written and takes other actions – FooManChew Jun 04 '12 at 10:58
  • you can't override initialize with bindata records, they recommend creating a method `initialize_instance` instead. – amenthes Dec 09 '14 at 15:44