lib/bindata/base.rb in bindata-0.6.0 vs lib/bindata/base.rb in bindata-0.7.0

- old
+ new

@@ -12,15 +12,17 @@ # Parameters may be provided at initialisation to control the behaviour of # an object. These params are: # # [<tt>:readwrite</tt>] If false, calls to #read or #write will # not perform any I/O. Default is true. - # [<tt>:check_offset</tt>] Raise an error if the current IO offest doesn't + # [<tt>:check_offset</tt>] Raise an error if the current IO offset doesn't # meet this criteria. A boolean return indicates # success or failure. Any other return is compared - # to the current offset. This parameter is - # only checked before reading. + # to the current offset. The variable +offset+ + # is made available to any lambda assigned to + # this parameter. This parameter is only checked + # before reading. class Base class << self # Returns the mandatory parameters used by this class. Any given args # are appended to the parameters list. The parameters for a class will # include the parameters of its ancestors. @@ -31,11 +33,11 @@ if parent.respond_to?(:mandatory_parameters) @mandatory_parameters.concat(parent.mandatory_parameters) end end end - unless (args.empty?) + if not args.empty? args.each { |arg| @mandatory_parameters << arg.to_sym } @mandatory_parameters.uniq! end @mandatory_parameters end @@ -51,11 +53,11 @@ if parent.respond_to?(:optional_parameters) @optional_parameters.concat(parent.optional_parameters) end end end - unless (args.empty?) + if not args.empty? args.each { |arg| @optional_parameters << arg.to_sym } @optional_parameters.uniq! end @optional_parameters end @@ -109,18 +111,18 @@ # +params+ is a hash containing symbol keys. Some params may # reference callable objects (methods or procs). +env+ is the # environment that these callable objects are evaluated in. def initialize(params = {}, env = nil) # default :readwrite param to true if unspecified - unless params.has_key?(:readwrite) + if not params.has_key?(:readwrite) params = params.dup params[:readwrite] = true end # ensure mandatory parameters exist self.class.mandatory_parameters.each do |prm| - unless params.has_key?(prm) + if not params.has_key?(prm) raise ArgumentError, "parameter ':#{prm}' must be specified " + "in #{self}" end end @@ -195,13 +197,14 @@ LazyEvalEnv.new(@env) end # Returns the value of the evaluated parameter. +key+ references a # parameter from the +params+ hash used when creating the data object. + # +values+ contains data that may be accessed when evaluating +key+. # Returns nil if +key+ does not refer to any parameter. - def eval_param(key) - @env.lazy_eval(@params[key]) + def eval_param(key, values = {}) + @env.lazy_eval(@params[key], values) end # Returns the parameter from the +params+ hash referenced by +key+. # Use this method if you are sure the parameter is not to be evaluated. # You most likely want #eval_param. @@ -225,16 +228,16 @@ # Checks that the current offset of +io+ is as expected. This should # be called from #do_read before performing the reading. def check_offset(io) if has_param?(:check_offset) - @env.offset = io.pos - io.mark - expected = eval_param(:check_offset) + actual_offset = io.pos - io.mark + expected = eval_param(:check_offset, :offset => actual_offset) if not expected raise ValidityError, "offset not as expected" - elsif @env.offset != expected and expected != true - raise ValidityError, "offset is '#{@env.offset}' but " + + elsif actual_offset != expected and expected != true + raise ValidityError, "offset is '#{actual_offset}' but " + "expected '#{expected}'" end end end