lib/bindata/sanitize.rb in bindata-2.0.0 vs lib/bindata/sanitize.rb in bindata-2.1.0

- old
+ new

@@ -22,10 +22,18 @@ @obj_class = obj_class @obj_params = SanitizedParameters.new(obj_params, @obj_class, endian) end end + def has_parameter?(param) + if @factory + @factory.has_parameter?(param) + else + @obj_params.has_parameter?(param) + end + end + def instantiate(value = nil, parent = nil) @factory ||= @obj_class.new(@obj_params) @factory.new(value, parent) end @@ -46,17 +54,23 @@ def name @name end + def has_parameter?(param) + @prototype.has_parameter?(param) + end + def instantiate(value = nil, parent = nil) @prototype.instantiate(value, parent) end end #---------------------------------------------------------------------------- class SanitizedFields < SanitizedParameter + include Enumerable + def initialize(endian) @fields = [] @endian = endian end attr_reader :fields @@ -81,14 +95,10 @@ def each(&block) @fields.each(&block) end - def collect(&block) - @fields.collect(&block) - end - def field_names @fields.collect { |field| field.name_as_sym } end def has_field_name?(name) @@ -101,10 +111,14 @@ def no_field_names_blank? @fields.all? { |f| f.name != nil } end + def any_field_has_parameter?(parameter) + @fields.any? { |f| f.has_parameter?(parameter) } + end + def copy_fields(other) @fields.concat(other.fields) end end #---------------------------------------------------------------------------- @@ -205,20 +219,36 @@ warn ":#{old_key} has been renamed to :#{new_key} in #{@the_class}. " + "Using :#{old_key} is now deprecated and will be removed in the future" end end + def must_be_integer(*keys) + keys.each do |key| + if has_parameter?(key) + parameter = self[key] + unless Symbol === parameter or + parameter.respond_to? :arity or + parameter.respond_to? :to_int + raise ArgumentError, "parameter '#{key}' in #{@the_class} must " + + "evaluate to an integer, got #{parameter.class}" + end + end + end + end + def endian @endian || self[:endian] end attr_writer :endian def create_sanitized_endian(endian) if endian == :big BIG_ENDIAN elsif endian == :little LITTLE_ENDIAN + elsif endian == :big_and_little + raise ArgumentError, ":endian => :big or :endian => :little is required" else raise ArgumentError, "unknown value for endian '#{endian}'" end end @@ -243,10 +273,10 @@ def sanitize! ensure_no_nil_values merge_default_parameters! - @the_class.sanitize_parameters!(self) + @the_class.arg_processor.sanitize_parameters!(@the_class, self) ensure_mandatory_parameters_exist ensure_mutual_exclusion_of_parameters end