lib/bindata/choice.rb in bindata-0.9.1 vs lib/bindata/choice.rb in bindata-0.9.2
- old
+ new
@@ -58,11 +58,11 @@
class << self
# Returns a sanitized +params+ that is of the form expected
# by #initialize.
- def sanitize_parameters(params, endian = nil)
+ def sanitize_parameters(sanitizer, params)
params = params.dup
if params.has_key?(:choices)
choices = params[:choices]
@@ -78,77 +78,42 @@
raise ArgumentError, ":choices hash may not have nil key"
end
# collect sanitized choice values
type, param = choices[key]
- klass = lookup(type, endian)
- if klass.nil?
- raise TypeError, "unknown type '#{type}' for #{self}"
- end
- val = [klass, SanitizedParameters.new(klass, param, endian)]
- new_choices[key] = val
+ new_choices[key] = sanitizer.sanitize(type, param)
end
params[:choices] = new_choices
when ::Array
choices.collect! do |type, param|
if type.nil?
# allow sparse arrays
nil
else
- klass = lookup(type, endian)
- if klass.nil?
- raise TypeError, "unknown type '#{type}' for #{self}"
- end
- [klass, SanitizedParameters.new(klass, param, endian)]
+ sanitizer.sanitize(type, param)
end
end
params[:choices] = choices
else
raise ArgumentError, "unknown type for :choices (#{choices.class})"
end
end
- super(params, endian)
+ super(sanitizer, params)
end
-
- # Returns all the possible field names a :choice may have.
- def all_possible_field_names(sanitized_params)
- unless SanitizedParameters === sanitized_params
- raise ArgumentError, "parameters aren't sanitized"
- end
-
- choices = sanitized_params[:choices]
-
- names = []
- if ::Array === choices
- choices.each do |cklass, cparams|
- names.concat(cklass.all_possible_field_names(cparams))
- end
- elsif ::Hash === choices
- choices.values.each do |cklass, cparams|
- names.concat(cklass.all_possible_field_names(cparams))
- end
- end
- names
- end
end
def initialize(params = {}, env = nil)
super(params, env)
# prepare collection of instantiated choice objects
@choices = (param(:choices) === ::Array) ? [] : {}
@last_key = nil
end
- def_delegators :the_choice, :clear, :clear?, :single_value?, :field_names
- def_delegators :the_choice, :snapshot, :done_read
+ def_delegators :the_choice, :clear, :clear?, :single_value?
+ def_delegators :the_choice, :done_read, :_snapshot
def_delegators :the_choice, :_do_read, :_do_write, :_do_num_bytes
-
- # Returns the data object that stores values for +name+.
- def find_obj_for_name(name)
- field_names.include?(name) ? the_choice.find_obj_for_name(name) : nil
- end
# Override to include selected data object.
def respond_to?(symbol, include_private = false)
super || the_choice.respond_to?(symbol, include_private)
end