lib/bindata/sanitize.rb in bindata-2.1.0 vs lib/bindata/sanitize.rb in bindata-2.2.0
- old
+ new
@@ -4,25 +4,28 @@
# Subclasses of this are sanitized
class SanitizedParameter; end
class SanitizedPrototype < SanitizedParameter
- def initialize(obj_type, obj_params, endian)
- endian = endian.endian if endian.respond_to? :endian
+ def initialize(obj_type, obj_params, hints)
+ raw_hints = hints.dup
+ if raw_hints[:endian].respond_to?(:endian)
+ raw_hints[:endian] = raw_hints[:endian].endian
+ end
obj_params ||= {}
if BinData::Base === obj_type
obj_class = obj_type
else
- obj_class = RegisteredClasses.lookup(obj_type, endian)
+ obj_class = RegisteredClasses.lookup(obj_type, raw_hints)
end
if BinData::Base === obj_class
@factory = obj_class
else
@obj_class = obj_class
- @obj_params = SanitizedParameters.new(obj_params, @obj_class, endian)
+ @obj_params = SanitizedParameters.new(obj_params, @obj_class, hints)
end
end
def has_parameter?(param)
if @factory
@@ -39,13 +42,13 @@
end
end
#----------------------------------------------------------------------------
class SanitizedField < SanitizedParameter
- def initialize(name, field_type, field_params, endian)
+ def initialize(name, field_type, field_params, hints)
@name = name
- @prototype = SanitizedPrototype.new(field_type, field_params, endian)
+ @prototype = SanitizedPrototype.new(field_type, field_params, hints)
end
attr_reader :prototype
def name_as_sym
@@ -67,20 +70,20 @@
#----------------------------------------------------------------------------
class SanitizedFields < SanitizedParameter
include Enumerable
- def initialize(endian)
+ def initialize(hints)
@fields = []
- @endian = endian
+ @hints = hints
end
attr_reader :fields
def add_field(type, name, params)
name = nil if name == ""
- @fields << SanitizedField.new(name, type, params, @endian)
+ @fields << SanitizedField.new(name, type, params, @hints)
end
def [](idx)
@fields[idx]
end
@@ -122,18 +125,18 @@
end
end
#----------------------------------------------------------------------------
class SanitizedChoices < SanitizedParameter
- def initialize(choices, endian)
+ def initialize(choices, hints)
@choices = {}
choices.each_pair do |key, val|
if SanitizedParameter === val
prototype = val
else
type, param = val
- prototype = SanitizedPrototype.new(type, param, endian)
+ prototype = SanitizedPrototype.new(type, param, hints)
end
if key == :default
@choices.default = prototype
else
@@ -181,21 +184,28 @@
class << self
def sanitize(parameters, the_class)
if SanitizedParameters === parameters
parameters
else
- SanitizedParameters.new(parameters, the_class, nil)
+ SanitizedParameters.new(parameters, the_class, {})
end
end
end
- def initialize(parameters, the_class, endian)
+ def initialize(parameters, the_class, hints)
parameters.each_pair { |key, value| self[key.to_sym] = value }
@the_class = the_class
- @endian = endian
+ if hints[:endian]
+ self[:endian] ||= hints[:endian]
+ end
+
+ if hints[:search_prefix]
+ self[:search_prefix] = Array(self[:search_prefix]).concat(Array(hints[:search_prefix]))
+ end
+
sanitize!
end
alias_method :has_parameter?, :has_key?
@@ -233,14 +243,16 @@
end
end
end
end
- def endian
- @endian || self[:endian]
+ def hints
+ {
+ :endian => self[:endian],
+ :search_prefix => self[:search_prefix],
+ }
end
- attr_writer :endian
def create_sanitized_endian(endian)
if endian == :big
BIG_ENDIAN
elsif endian == :little
@@ -251,22 +263,22 @@
raise ArgumentError, "unknown value for endian '#{endian}'"
end
end
def create_sanitized_params(params, the_class)
- SanitizedParameters.new(params, the_class, self.endian)
+ SanitizedParameters.new(params, the_class, hints)
end
def create_sanitized_choices(choices)
- SanitizedChoices.new(choices, self.endian)
+ SanitizedChoices.new(choices, hints)
end
def create_sanitized_fields
- SanitizedFields.new(self.endian)
+ SanitizedFields.new(hints)
end
def create_sanitized_object_prototype(obj_type, obj_params)
- SanitizedPrototype.new(obj_type, obj_params, self.endian)
+ SanitizedPrototype.new(obj_type, obj_params, hints)
end
#---------------
private