Class: SXRB::Proxy

Inherits:
Object
  • Object
show all
Defined in:
lib/sxrb/proxy.rb

Overview

Objects of Proxy class are part of SXRB DSL and their methods are meant to be used to define set of rules needed to parse XML document. They have no other meaning and should not be used outside of that scope.

Instance Method Summary collapse

Constructor Details

#initialize(callback_tree, current_path = '') ⇒ Proxy

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Proxy objects are generated by the SXRB itself, and are passed to blocks to be used as DSL. They should not be created in any other context. Implementation might change without prior notice.



12
13
14
15
# File 'lib/sxrb/proxy.rb', line 12

def initialize(callback_tree, current_path = '')
  @callback_tree = callback_tree
  @current_path = current_path
end

Instance Method Details

#child(*tags) {|Proxy| ... } ⇒ nil

TODO:

Add Regexp and other selectors support in addition to Strings.

Defines child (a direct descendant) of an element defined in current block.

Parameters:

  • tags (String)

    names of tags that should be matched

Yields:

  • (Proxy)

    block receives Proxy element representing matched elements.

Returns:

  • (nil)


29
30
31
32
33
34
35
# File 'lib/sxrb/proxy.rb', line 29

def child(*tags, &block)
  tags.each do |tag|
    @callback_tree.add_rule(tag, @current_path, :recursive => false).tap do |new_path|
      block.call(Proxy.new(@callback_tree, new_path))
    end
  end
end

#descendant(*tags) {|Proxy| ... } ⇒ nil

TODO:

Add Regexp and other selectors support in addition to strings.

Defines descendant of an element defined in current block.

Parameters:

  • tags (String)

    names of tags that should be matched

Yields:

  • (Proxy)

    block receives Proxy element representing matched elements.

Returns:

  • (nil)


49
50
51
52
53
54
55
# File 'lib/sxrb/proxy.rb', line 49

def descendant(*tags, &block)
  tags.each do |tag|
    @callback_tree.add_rule(tag, @current_path, :recursive => true).tap do |new_path|
      block.call(Proxy.new(@callback_tree, new_path))
    end
  end
end

#on_characters {|String| ... } ⇒ nil

Defines a callback that is invoked whenever anonymous text node within self is encountered.

Yields:

  • (String)

    block receives raw content of parsed data in form of string object.

Returns:

  • (nil)


95
96
97
# File 'lib/sxrb/proxy.rb', line 95

def on_characters(&block)
  @callback_tree.add_callback(:characters, @current_path, &block)
end

#on_element {|Node| ... } ⇒ nil

Note:

‘on_element` should not be used for items that are expected to have large node subtrees.

Defines callback method invoked when matching element is completely parsed.

Yields:

  • (Node)

    block receives whole parsed element with children.

Returns:

  • (nil)


70
71
72
# File 'lib/sxrb/proxy.rb', line 70

def on_element(&block)
  @callback_tree.add_callback(:element, @current_path, &block)
end

#on_end {|Node| ... } ⇒ nil

Defines a callback that is invoked whenever end tag is encountered.

Yields:

  • (Node)

    block receives parsed node without any nested elements. All inline attributes are available though.

Returns:

  • (nil)


107
108
109
# File 'lib/sxrb/proxy.rb', line 107

def on_end(&block)
  @callback_tree.add_callback(:end, @current_path, &block)
end

#on_start {|Node| ... } ⇒ nil

Defines a callback that is invoked whenever start tag is encountered.

Yields:

  • (Node)

    block receives parsed node without any nested elements. All inline attributes are available though.

Returns:

  • (nil)


82
83
84
# File 'lib/sxrb/proxy.rb', line 82

def on_start(&block)
  @callback_tree.add_callback(:start, @current_path, &block)
end