lib/wash_out/param.rb in wash_out-0.3.6 vs lib/wash_out/param.rb in wash_out-0.4.0

- old
+ new

@@ -1,24 +1,37 @@ module WashOut class Param + attr_accessor :raw_name attr_accessor :name attr_accessor :map attr_accessor :type attr_accessor :multiplied attr_accessor :value + attr_accessor :source_class # 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 ||= {} @name = name.to_s + @raw_name = name.to_s @map = {} @multiplied = multiplied + if WashOut::Engine.camelize_wsdl.to_s == 'lower' + @name = @name.camelize(:lower) + elsif WashOut::Engine.camelize_wsdl + @name = @name.camelize + end + if type.is_a?(Symbol) @type = type.to_s + elsif type.is_a?(Class) + @type = 'struct' + @map = self.class.parse_def(type.wash_out_param_map) + @source_class = type else @type = 'struct' @map = self.class.parse_def(type) end end @@ -45,14 +58,17 @@ param.load(data, elem) end end else operation = case type - when 'string'; :to_s - when 'integer'; :to_i - when 'double'; :to_f - when 'boolean'; nil # Nori handles that for us + when 'string'; :to_s + when 'integer'; :to_i + when 'double'; :to_f + when 'boolean'; nil # Nori handles that for us + when 'date'; :to_date + when 'datetime'; :to_datetime + when 'time'; :to_time else raise RuntimeError, "Invalid WashOut simple type: #{type}" end if operation.nil? data @@ -67,13 +83,27 @@ # Checks if this Param defines a complex type. def struct? type == 'struct' end + def classified? + !source_class.nil? + end + + def basic_type + return name unless classified? + return source_class.wash_out_param_name + end + + def xsd_type + return 'dateTime' if type.to_s == 'datetime' + return type + end + # Returns a WSDL namespaced identifier for this type. def namespaced_type - struct? ? "tns:#{name}" : "xsd:#{type}" + struct? ? "tns:#{basic_type}" : "xsd:#{xsd_type}" end # Parses a +definition+. The format of the definition is best described # by the following BNF-like grammar. # @@ -92,11 +122,11 @@ # This function returns an array of WashOut::Param objects. def self.parse_def(definition) 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) + if [Array, Symbol, Class].include?(definition.class) definition = { :value => definition } end if definition.is_a? Hash definition.map do |name, opt| @@ -107,16 +137,18 @@ else WashOut::Param.new(name, opt) end end else - raise RuntimeError, "Wrong definition: #{type.inspect}" + raise RuntimeError, "Wrong definition: #{definition.inspect}" end end def flat_copy copy = self.class.new(@name, @type.to_sym, @multiplied) + copy.raw_name = raw_name + copy end private # Used to load an entire structure. @@ -124,11 +156,11 @@ data = data.with_indifferent_access struct = {}.with_indifferent_access # RUBY18 Enumerable#each_with_object is better, but 1.9 only. @map.map do |param| - if data.has_key? param.name - struct[param.name] = yield param, data, param.name + if data.has_key? param.raw_name + struct[param.raw_name] = yield param, data, param.raw_name end end struct end