lib/bindata/multi_value.rb in bindata-0.9.2 vs lib/bindata/multi_value.rb in bindata-0.9.3
- old
+ new
@@ -1,5 +1,6 @@
+require 'bindata/params'
require 'bindata/struct'
module BinData
# A MultiValue is a declarative wrapper around Struct.
#
@@ -42,15 +43,22 @@
# endian of any numerics in this struct, or in any
# nested data objects.
class MultiValue < BinData::Struct
class << self
+ extend Parameters
+
# Register the names of all subclasses of this class.
def inherited(subclass) #:nodoc:
register(subclass.name, subclass)
end
+ # Can this data object self reference itself?
+ def recursive?
+ true
+ end
+
# Returns or sets the endianess of numerics used in this stucture.
# Endianess is applied to the fields of this structure.
# Valid values are :little and :big.
def endian(endian = nil)
@endian ||= nil
@@ -115,23 +123,49 @@
# remember this field. These fields will be recalled upon creating
# an instance of this class
@fields.push([type, name, params])
end
- # Returns a sanitized +params+ that is of the form expected
- # by #initialize.
- def sanitize_parameters(sanitizer, params)
- params = params.dup
-
+ # Ensures that +params+ is of the form expected by #initialize.
+ def sanitize_parameters!(sanitizer, params)
endian = params[:endian] || self.endian
fields = params[:fields] || self.fields
hide = params[:hide] || self.hide
params[:endian] = endian unless endian.nil?
params[:fields] = fields
params[:hide] = hide
+ # add default parameters
+ default_parameters.each do |k,v|
+ params[k] = v unless params.has_key?(k)
+ end
+
+ # ensure mandatory parameters exist
+ mandatory_parameters.each do |prm|
+ if not params.has_key?(prm)
+ raise ArgumentError, "parameter ':#{prm}' must be specified " +
+ "in #{self}"
+ end
+ end
+
super(sanitizer, params)
+ end
+
+ # Sets the mandatory parameters used by this class.
+ def mandatory_parameters(*args) ; end
+
+ define_x_parameters(:mandatory, []) do |array, args|
+ args.each { |arg| array << arg.to_sym }
+ array.uniq!
+ end
+
+ # Sets the default parameters used by this class.
+ def default_parameters(params = {}); end
+
+ define_x_parameters(:default, {}) do |hash, args|
+ params = args.length > 0 ? args[0] : {}
+ hash.merge!(params)
end
end
end
end