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(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.



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

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

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

#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(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)


126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
# File 'lib/wash_out/param.rb', line 126

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, Class].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
    raise RuntimeError, "Wrong definition: #{definition.inspect}"
  end
end

Instance Method Details

#basic_typeObject



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

def basic_type
  return name unless classified?
  return source_class.wash_out_param_name
end

#classified?Boolean

Returns:

  • (Boolean)


90
91
92
# File 'lib/wash_out/param.rb', line 90

def classified?
  !source_class.nil?
end

#flat_copyObject



149
150
151
152
153
# File 'lib/wash_out/param.rb', line 149

def flat_copy
  copy = self.class.new(@name, @type.to_sym, @multiplied)
  copy.raw_name = raw_name
  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.



41
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
# File 'lib/wash_out/param.rb', line 41

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?
    if @multiplied
      data.map do |x|
        map_struct x do |param, data, elem|
          param.load(data, elem)
        end
      end
    else
      map_struct data do |param, data, elem|
        param.load(data, elem)
      end
    end
  else
    operation = case type
      when 'string';    :to_s
      when 'integer';   :to_i
      when 'double';    :to_f
      when 'boolean';   lambda { |v| v == 'true' }
      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.nil?
      data
    elsif @multiplied
      data.map{|x| x.send(operation)}
    elsif operation.is_a? Symbol
      data.send(operation)
    else
      operation.call(data)
    end
  end
end

#namespaced_typeObject

Returns a WSDL namespaced identifier for this type.



106
107
108
# File 'lib/wash_out/param.rb', line 106

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

#struct?Boolean

Checks if this Param defines a complex type.

Returns:

  • (Boolean)


86
87
88
# File 'lib/wash_out/param.rb', line 86

def struct?
  type == 'struct'
end

#xsd_typeObject



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

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