lib/wash_out/param.rb in wash_out-0.2.6 vs lib/wash_out/param.rb in wash_out-0.3.0

- old
+ new

@@ -2,10 +2,11 @@ class Param attr_accessor :name attr_accessor :map attr_accessor :type attr_accessor :multiplied + attr_accessor :value # Defines a WSDL parameter with name +name+ and type specifier +type+. # The type specifier format is described in #parse_def. def initialize(name, type, multiplied = false) type ||= {} @@ -25,34 +26,33 @@ # Converts a generic externally derived Ruby value, such as String or # Hash, to a native Ruby object according to the definition of this type. def load(data) check_if_missing(data) + data = Array(data) if @multiplied + if struct? map_struct(data) { |param, elem| param.load(elem) } else - case type - when 'string'; data.to_s - when 'integer'; data.to_i - when 'double'; data.to_f - when 'boolean'; data # Nori handles that for us + operation = case type + when 'string'; :to_s + when 'integer'; :to_i + when 'double'; :to_f + when 'boolean'; nil # Nori handles that for us else raise RuntimeError, "Invalid WashOut simple type: #{type}" end + + if operation.nil? + data + elsif data.is_a? Array + data.map{|x| x.send(operation)} + else + data.send(operation) + end end end - # The opposite of #load. - def store(data) - check_if_missing(data) - - if struct? - map_struct(data) { |param, elem| param.store(elem) } - else - data.to_s - end - end - # Checks if this Param defines a complex type. def struct? type == 'struct' end @@ -76,27 +76,40 @@ # If a WashOut::Param instance is passed as a +nested_type+, the corresponding # +:parameter_name+ is ignored. # # This function returns an array of WashOut::Param objects. def self.parse_def(definition) - definition = { :value => definition } if definition.is_a? Symbol + raise RuntimeError, "[] should not be used in your params. Use nil if you want to mark empty set." if definition == [] + return [] if definition == nil + + if [Array, Symbol].include?(definition.class) + definition = { :value => definition } + end if definition.is_a? Hash definition.map do |name, opt| if opt.is_a? WashOut::Param opt + elsif opt.is_a? Array + WashOut::Param.new(name, opt[0], true) else WashOut::Param.new(name, opt) end end else - definition.to_a + raise RuntimeError, "Wrong definition: #{type.inspect}" end end + + def clone + copy = self.class.new(@name, @type.to_sym, @multiplied) + copy.map = @map.map{|x| x.clone} + copy + end private - # Used to load or store an entire structure. + # Used to load an entire structure. def map_struct(data) data = data.with_indifferent_access struct = {}.with_indifferent_access # RUBY18 Enumerable#each_with_object is better, but 1.9 only.