Class: EbayTrader::XMLBuilder

Inherits:
Object
  • Object
show all
Defined in:
lib/ebay_trader/xml_builder.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(args = {}) ⇒ XMLBuilder

Returns a new instance of XMLBuilder.



8
9
10
11
12
# File 'lib/ebay_trader/xml_builder.rb', line 8

def initialize(args = {})
  @xml = ''
  @depth = 0
  @tab_width = (args[:tab_width] || 0).to_i
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method_name, *args, &block) ⇒ Object

Catch-all method to avoid having to create individual methods for each XML tag name.



15
16
17
18
19
20
21
# File 'lib/ebay_trader/xml_builder.rb', line 15

def method_missing(method_name, *args, &block)
  if @context && @context.respond_to?(method_name)
    @context.send(method_name, *args, &block)
  else
    node(method_name, args, &block)
  end
end

Instance Attribute Details

#contextObject (readonly)

Returns the value of attribute context.



6
7
8
# File 'lib/ebay_trader/xml_builder.rb', line 6

def context
  @context
end

#depthObject (readonly)

Returns the value of attribute depth.



6
7
8
# File 'lib/ebay_trader/xml_builder.rb', line 6

def depth
  @depth
end

#tab_widthObject (readonly)

Returns the value of attribute tab_width.



6
7
8
# File 'lib/ebay_trader/xml_builder.rb', line 6

def tab_width
  @tab_width
end

#xmlObject (readonly)

Returns the value of attribute xml.



6
7
8
# File 'lib/ebay_trader/xml_builder.rb', line 6

def xml
  @xml
end

Instance Method Details

#respond_to_missing?(method_name, include_private = false) ⇒ Boolean

Only respond to method names with only numbers and letters. Do not respond to names with underscores.

Returns:

  • (Boolean)


25
26
27
# File 'lib/ebay_trader/xml_builder.rb', line 25

def respond_to_missing?(method_name, include_private = false)
  super || method_name.to_s =~ /^[a-z0-9]+$/i
end

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

Begin creating an XML string by specifying the root node. This also sets the context scope, allowing methods and variables outside the block to be accessed.

Parameters:

  • name (String)

    the name of the root node element.

  • args (Array)

    the data for this element.

  • block (Block)

    an optional block of sub-elements to be nested within the root node.



36
37
38
39
# File 'lib/ebay_trader/xml_builder.rb', line 36

def root(name, *args, &block)
  set_context(&block)
  node(name, args, &block)
end