Class: WashOut::Param

Inherits:
Object
  • Object
show all
Defined in:
lib/wash_out/param.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(soap_config, name, type, multiplied = false) ⇒ Param

Defines a WSDL parameter with name name and type specifier type. The type specifier format is described in #parse_def.



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/wash_out/param.rb', line 14

def initialize(soap_config, name, type, multiplied = false)
  type ||= {}
  @soap_config = soap_config
  @name       = name.to_s
  @raw_name   = name.to_s
  @map        = {}
  @multiplied = multiplied

  if soap_config.camelize_wsdl.to_s == 'lower'
    @name = @name.camelize(:lower)
  elsif soap_config.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(soap_config, type.wash_out_param_map)
    @source_class = type
  else
    @type = 'struct'
    @map  = self.class.parse_def(soap_config, type)
  end
end

Instance Attribute Details

#mapObject

Returns the value of attribute map.



5
6
7
# File 'lib/wash_out/param.rb', line 5

def map
  @map
end

#multipliedObject

Returns the value of attribute multiplied.



7
8
9
# File 'lib/wash_out/param.rb', line 7

def multiplied
  @multiplied
end

#nameObject

Returns the value of attribute name.



4
5
6
# File 'lib/wash_out/param.rb', line 4

def name
  @name
end

#raw_nameObject

Returns the value of attribute raw_name.



3
4
5
# File 'lib/wash_out/param.rb', line 3

def raw_name
  @raw_name
end

#soap_configObject

Returns the value of attribute soap_config.



10
11
12
# File 'lib/wash_out/param.rb', line 10

def soap_config
  @soap_config
end

#source_classObject

Returns the value of attribute source_class.



9
10
11
# File 'lib/wash_out/param.rb', line 9

def source_class
  @source_class
end

#typeObject

Returns the value of attribute type.



6
7
8
# File 'lib/wash_out/param.rb', line 6

def type
  @type
end

#valueObject

Returns the value of attribute value.



8
9
10
# File 'lib/wash_out/param.rb', line 8

def value
  @value
end

Class Method Details

.parse_def(soap_config, definition) ⇒ Object

Parses a definition. The format of the definition is best described by the following BNF-like grammar.

simple_type := :string | :integer | :double | :boolean
nested_type := type_hash | simple_type | WashOut::Param instance
type_hash   := { :parameter_name => nested_type, ... }
definition  := [ WashOut::Param, ... ] |
               type_hash |
               simple_type

If a simple type is passed as the definition, a single Param is returned with the name set to “value”. 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.

Raises:

  • (RuntimeError)


135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
# File 'lib/wash_out/param.rb', line 135

def self.parse_def(soap_config, 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 definition.is_a?(Class) && definition.ancestors.include?(WashOut::Type)
    definition = definition.wash_out_param_map
  end

  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(soap_config, name, opt[0], true)
      else
        WashOut::Param.new(soap_config, name, opt)
      end
    end
  else
    raise RuntimeError, "Wrong definition: #{definition.inspect}"
  end
end

Instance Method Details

#attr_nameObject



173
174
175
176
# File 'lib/wash_out/param.rb', line 173

def attr_name
  raise 'Not attribute' unless attribute?
  name[1..-1]
end

#attribute?Boolean

Returns:

  • (Boolean)


169
170
171
# File 'lib/wash_out/param.rb', line 169

def attribute?
  name[0] == "@"
end

#basic_typeObject



103
104
105
106
# File 'lib/wash_out/param.rb', line 103

def basic_type
  return name unless classified?
  return source_class.wash_out_param_name(@soap_config)
end

#classified?Boolean

Returns:

  • (Boolean)


99
100
101
# File 'lib/wash_out/param.rb', line 99

def classified?
  !source_class.nil?
end

#flat_copyObject



162
163
164
165
166
167
# File 'lib/wash_out/param.rb', line 162

def flat_copy
  copy = self.class.new(@soap_config, @name, @type.to_sym, @multiplied)
  copy.raw_name = raw_name
  copy.source_class = source_class
  copy
end

#load(data, key) ⇒ Object

Converts a generic externally derived Ruby value, such as String or Hash, to a native Ruby object according to the definition of this type.



42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
# File 'lib/wash_out/param.rb', line 42

def load(data, key)
  if !data.has_key? key
    raise WashOut::Dispatcher::SOAPError, "Required SOAP parameter '#{key}' is missing"
  end

  data = data[key]
  data = [data] if @multiplied && !data.is_a?(Array)

  if struct?
    data ||= {}
    if @multiplied
      data.map do |x|
        map_struct x do |param, dat, elem|
          param.load(dat, elem)
        end
      end
    else
      map_struct data do |param, dat, elem|
        param.load(dat, elem)
      end
    end
  else
    operation = case type
      when 'string';       :to_s
      when 'integer';      :to_i
      when 'long';         :to_i
      when 'double';       :to_f
      when 'boolean';      lambda{|dat| dat === "0" ? false : !!dat}
      when 'date';         :to_date
      when 'datetime';     :to_datetime
      when 'time';         :to_time
      when 'base64Binary'; lambda{|dat| Base64.decode64(dat)}
      else raise RuntimeError, "Invalid WashOut simple type: #{type}"
    end

    begin
      if data.nil?
        data
      elsif @multiplied
        return data.map{|x| x.send(operation)} if operation.is_a?(Symbol)
        return data.map{|x| operation.call(x)} if operation.is_a?(Proc)
      elsif operation.is_a? Symbol
        data.send(operation)
      else
        operation.call(data)
      end
    rescue
      raise WashOut::Dispatcher::SOAPError, "Invalid SOAP parameter '#{key}' format"
    end
  end
end

#namespaced_typeObject

Returns a WSDL namespaced identifier for this type.



115
116
117
# File 'lib/wash_out/param.rb', line 115

def namespaced_type
  struct? ? "tns:#{basic_type}" : "xsd:#{xsd_type}"
end

#struct?Boolean

Checks if this Param defines a complex type.

Returns:

  • (Boolean)


95
96
97
# File 'lib/wash_out/param.rb', line 95

def struct?
  type == 'struct'
end

#xsd_typeObject



108
109
110
111
112
# File 'lib/wash_out/param.rb', line 108

def xsd_type
  return 'int' if type.to_s == 'integer'
  return 'dateTime' if type.to_s == 'datetime'
  return type
end