Class: LolSoap::Builder

Inherits:
Object
  • Object
show all
Defined in:
lib/lolsoap/builder.rb

Overview

Used to build XML, with namespaces automatically added.

Examples:

General

builder = Builder.new(node, type)
builder.someTag do |t|
  t.foo 'bar'
end
# => <ns1:someTag><ns1:foo>bar</ns1:foo></ns1:someTag>

Explicitly specifying a namespace prefix

builder = Builder.new(node, type)
builder['ns2'].someTag
# => <ns2:someTag/>

Constant Summary collapse

RESERVED_METHODS =
%w(object_id respond_to_missing? inspect === to_s)

Instance Method Summary collapse

Constructor Details

#initialize(node, type = WSDL::NullType.new) ⇒ Builder

Returns a new instance of Builder.



47
48
49
50
# File 'lib/lolsoap/builder.rb', line 47

def initialize(node, type = WSDL::NullType.new)
  @node = node
  @type = type || WSDL::NullType.new
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(name, *args, &block) ⇒ Object (private)

alias method_missing __tag__



106
107
108
109
110
111
112
# File 'lib/lolsoap/builder.rb', line 106

def method_missing(name, *args, &block)
  if @type.has_attribute?(name.to_s)
    __attribute__(name, *args)
  else
    __tag__(name, *args, &block)
  end
end

Instance Method Details

#[](prefix) ⇒ Object

Specify a namespace prefix explicitly



89
90
91
# File 'lib/lolsoap/builder.rb', line 89

def [](prefix)
  Prefix.new(self, prefix)
end

#__attribute__(name, value) ⇒ Object



58
59
60
# File 'lib/lolsoap/builder.rb', line 58

def __attribute__(name, value)
  @node[name.to_s] = value.to_s
end

#__content__(value) ⇒ Object



62
63
64
# File 'lib/lolsoap/builder.rb', line 62

def __content__(value)
  @node.content = value
end

#__node__Object

Node accessor. Named to prevent method_missing conflict.



79
80
81
# File 'lib/lolsoap/builder.rb', line 79

def __node__
  @node
end

#__tag__(name, *args, &block) ⇒ Object

Add a tag manually, rather than through method_missing. This is so you can still add tags for the very small number of tags that are also existing methods.



54
55
56
# File 'lib/lolsoap/builder.rb', line 54

def __tag__(name, *args, &block)
  __prefixed_tag__(@type.element_prefix(name.to_s), @type.sub_type(name.to_s), name, *args, &block)
end

#__type__Object

Type accessor. Named to prevent method_missing conflict.



84
85
86
# File 'lib/lolsoap/builder.rb', line 84

def __type__
  @type
end

#pretty_print(pp) ⇒ Object



97
98
99
100
101
# File 'lib/lolsoap/builder.rb', line 97

def pretty_print(pp)
  pp.group(2, "#(LolSoap::Builder #{sprintf("0x%x", object_id)} {", "})") do
    pp.pp @node
  end
end

#respond_to?(name) ⇒ Boolean

Returns:

  • (Boolean)


93
94
95
# File 'lib/lolsoap/builder.rb', line 93

def respond_to?(name)
  true
end