class LolSoap::Builder

Used to build XML, with namespaces automatically added.

@example General

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

@example Explicitly specifying a namespace prefix

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

Constants

RESERVED_METHODS

Public Class Methods

new(node, type = WSDL::NullType.new) click to toggle source
# File lib/lolsoap/builder.rb, line 47
def initialize(node, type = WSDL::NullType.new)
  @node = node
  @type = type || WSDL::NullType.new
end

Public Instance Methods

[](prefix) click to toggle source

Specify a namespace prefix explicitly

# File lib/lolsoap/builder.rb, line 103
def [](prefix)
  Prefix.new(self, prefix)
end
__attribute__(name, value) click to toggle source
# File lib/lolsoap/builder.rb, line 58
def __attribute__(name, value)
  @node[name.to_s] = value.to_s
end
__content__(value) click to toggle source
# File lib/lolsoap/builder.rb, line 62
def __content__(value)
  @node.content = value
end
__node__() click to toggle source

Node accessor. Named to prevent method_missing conflict.

# File lib/lolsoap/builder.rb, line 93
def __node__
  @node
end
__prefixed_tag__(prefix, sub_type, name, *args) { |builder| ... } click to toggle source

@private

# File lib/lolsoap/builder.rb, line 67
def __prefixed_tag__(prefix, sub_type, name, *args)
  sub_node = @node.document.create_element(name.to_s, *args)
  sub_node.namespace = @node.namespace_scopes.find { |n| n.prefix == prefix }

  # Nokogiri doesn't currently allow to add a child element without a
  # namespace to a parent with a namespace: the child inherits the parent's
  # namespace.  It's a known issue:
  # https://github.com/sparklemotion/nokogiri/issues/1469 Until it's fixed,
  # we'll use this workaround: store the parent's namespace, set it to nil
  # temporarily, add the child and re-add the original namespace to the
  # parent.
  if sub_node.namespace.nil?
    parent_namespace = @node.namespace
    @node.namespace = nil
    @node << sub_node
    @node.namespace = parent_namespace
  else
    @node << sub_node
  end

  builder = __class__.new(sub_node, sub_type)
  yield builder if block_given?
  builder
end
__tag__(name, *args, &block) click to toggle source

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.

# 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__() click to toggle source

Type accessor. Named to prevent method_missing conflict.

# File lib/lolsoap/builder.rb, line 98
def __type__
  @type
end
pretty_print(pp) click to toggle source
# File lib/lolsoap/builder.rb, line 111
def pretty_print(pp)
  pp.group(2, "#(LolSoap::Builder #{sprintf("0x%x", object_id)} {", "})") do
    pp.pp @node
  end
end
respond_to?(name) click to toggle source
# File lib/lolsoap/builder.rb, line 107
def respond_to?(name)
  true
end

Private Instance Methods

method_missing(name, *args, &block) click to toggle source

alias method_missing __tag__

# File lib/lolsoap/builder.rb, line 120
def method_missing(name, *args, &block)
  if @type.has_attribute?(name.to_s)
    __attribute__(name, *args)
  else
    __tag__(name, *args, &block)
  end
end