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

- old
+ new

@@ -56,58 +56,18 @@ # specifies the currently active choice. # <tt>:copy_on_change</tt>:: If set to true, copy the value of the previous # selection to the current selection whenever the # selection changes. Default is false. class Choice < BinData::Base - include DSLMixin + extend DSLMixin - dsl_parser :choice + dsl_parser :choice + arg_processor :choice mandatory_parameters :choices, :selection optional_parameter :copy_on_change - class << self - - def sanitize_parameters!(params) #:nodoc: - params.merge!(dsl_params) - - if params.needs_sanitizing?(:choices) - choices = choices_as_hash(params[:choices]) - ensure_valid_keys(choices) - params[:choices] = params.create_sanitized_choices(choices) - end - end - - #------------- - private - - def choices_as_hash(choices) - if choices.respond_to?(:to_ary) - key_array_by_index(choices.to_ary) - else - choices - end - end - - def key_array_by_index(array) - result = {} - array.each_with_index do |el, i| - result[i] = el unless el.nil? - end - result - end - - def ensure_valid_keys(choices) - if choices.has_key?(nil) - raise ArgumentError, ":choices hash may not have nil key" - end - if choices.keys.detect { |key| key.is_a?(Symbol) and key != :default } - raise ArgumentError, ":choices hash may not have symbols for keys" - end - end - end - def initialize_shared_instance extend CopyOnChangePlugin if eval_parameter(:copy_on_change) == true super end @@ -123,46 +83,30 @@ raise IndexError, ":selection returned nil for #{debug_name}" end selection end - def clear? #:nodoc: - current_choice.clear? + def safe_respond_to?(symbol, include_private = false) #:nodoc: + base_respond_to?(symbol, include_private) end - def assign(val) - current_choice.assign(val) - end - - def snapshot - current_choice.snapshot - end - def respond_to?(symbol, include_private = false) #:nodoc: current_choice.respond_to?(symbol, include_private) || super end - def safe_respond_to?(symbol, include_private = false) #:nodoc: - base_respond_to?(symbol, include_private) - end - def method_missing(symbol, *args, &block) #:nodoc: current_choice.__send__(symbol, *args, &block) end - def do_read(io) #:nodoc: - current_choice.do_read(io) + %w(clear? assign snapshot do_read do_write do_num_bytes).each do |m| + self.module_eval <<-END + def #{m}(*args) + current_choice.#{m}(*args) + end + END end - def do_write(io) #:nodoc: - current_choice.do_write(io) - end - - def do_num_bytes #:nodoc: - current_choice.do_num_bytes - end - #--------------- private def current_choice current_selection = selection @@ -173,9 +117,49 @@ prototype = get_parameter(:choices)[selection] if prototype.nil? raise IndexError, "selection '#{selection}' does not exist in :choices for #{debug_name}" end prototype.instantiate(nil, self) + end + end + + class ChoiceArgProcessor < BaseArgProcessor + def sanitize_parameters!(obj_class, params) #:nodoc: + params.merge!(obj_class.dsl_params) + + if params.needs_sanitizing?(:choices) + choices = choices_as_hash(params[:choices]) + ensure_valid_keys(choices) + params[:choices] = params.create_sanitized_choices(choices) + end + end + + #------------- + private + + def choices_as_hash(choices) + if choices.respond_to?(:to_ary) + key_array_by_index(choices.to_ary) + else + choices + end + end + + def key_array_by_index(array) + result = {} + array.each_with_index do |el, i| + result[i] = el unless el.nil? + end + result + end + + def ensure_valid_keys(choices) + if choices.has_key?(nil) + raise ArgumentError, ":choices hash may not have nil key" + end + if choices.keys.detect { |key| key.is_a?(Symbol) and key != :default } + raise ArgumentError, ":choices hash may not have symbols for keys" + end end end # Logic for the :copy_on_change parameter module CopyOnChangePlugin