lib/bindata/base.rb in bindata-0.10.0 vs lib/bindata/base.rb in bindata-0.11.0

- old
+ new

@@ -37,32 +37,49 @@ data = self.new data.read(io) data end - def recursive? - # data objects do not self reference by default - false + def mandatory_parameters(*args) + accepted_parameters.mandatory(*args) end - AcceptedParameters.define_all_accessors(self, :internal) + def optional_parameters(*args) + accepted_parameters.optional(*args) + end - def accepted_internal_parameters - internal = AcceptedParameters.get(self, :internal) - internal.all + def default_parameters(*args) + accepted_parameters.default(*args) end - def sanitize_parameters!(sanitizer, params) - internal = AcceptedParameters.get(self, :internal) - internal.sanitize_parameters!(sanitizer, params) + def mutually_exclusive_parameters(*args) + accepted_parameters.mutually_exclusive(*args) end + alias_method :mandatory_parameter, :mandatory_parameters + alias_method :optional_parameter, :optional_parameters + alias_method :default_parameter, :default_parameters + + def accepted_parameters + unless defined? @accepted_parameters + ancestor = ancestors[1..-1].find { |cls| + cls.respond_to?(:accepted_parameters) + } + ancestor_params = ancestor.nil? ? nil : ancestor.accepted_parameters + @accepted_parameters = AcceptedParameters.new(ancestor_params) + end + @accepted_parameters + end + + def sanitize_parameters!(params, sanitizer) + end + #------------- private def warn_replacement_parameter(params, bad_key, suggested_key) - if params.has_key?(bad_key) + if params.has_parameter?(bad_key) warn ":#{bad_key} is not used with #{self}. " + "You probably want to change this to :#{suggested_key}" end end @@ -79,11 +96,11 @@ # +params+ is a hash containing symbol keys. Some params may # reference callable objects (methods or procs). +parent+ is the # parent data object (e.g. struct, array, choice) this object resides # under. def initialize(params = {}, parent = nil) - @params = Sanitizer.sanitize(self.class, params) + @params = Sanitizer.sanitize(params, self.class) @parent = parent end attr_reader :parent @@ -102,11 +119,11 @@ @params[key] end # Returns whether +key+ exists in the +parameters+ hash. def has_parameter?(key) - @params.has_key?(key) + @params.has_parameter?(key) end # Reads data into this data object. def read(io) io = BinData::IO.new(io) unless BinData::IO === io @@ -140,17 +157,17 @@ _do_write(io) end protected :do_write # Returns the number of bytes it will take to write this data. - def num_bytes(what = nil) - num = do_num_bytes(what) + def num_bytes(deprecated = nil) + num = do_num_bytes(deprecated) num.ceil end - def do_num_bytes(what = nil) - _do_num_bytes(what) + def do_num_bytes(deprecated = nil) + _do_num_bytes(deprecated) end protected :do_num_bytes # Assigns the value of +val+ to this data object. Note that +val+ will # always be deep copied to ensure no aliasing problems can occur. @@ -251,11 +268,11 @@ def clear raise NotImplementedError end # Returns true if the object has not been changed since creation. - def clear?(*args) + def clear? raise NotImplementedError end # Returns the debug name of +child+. This only needs to be implemented # by objects that contain child objects. @@ -283,11 +300,11 @@ def _do_write(io) raise NotImplementedError end # Returns the number of bytes it will take to write this data. - def _do_num_bytes(what) + def _do_num_bytes(deprecated) raise NotImplementedError end # Assigns the value of +val+ to this data object. Note that +val+ will # always be deep copied to ensure no aliasing problems can occur. @@ -301,15 +318,9 @@ end # Set visibility requirements of methods to implement public :clear, :clear?, :debug_name_of, :offset_of private :_do_read, :_done_read, :_do_write, :_do_num_bytes, :_assign, :_snapshot - -def single_value? - warn "#single_value? is deprecated. It should no longer be needed" - false -end -public :single_value? # End To be implemented by subclasses ########################################################################### end end