Class: Ox::Mapper::Handler Private

Inherits:
Object
  • Object
show all
Defined in:
lib/ox/mapper/handler.rb

Overview

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

Sax handler

Constant Summary collapse

OUTPUT_ENCODING =

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

Encoding::UTF_8

Instance Method Summary collapse

Constructor Details

#initializeHandler

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.

Returns a new instance of Handler.



12
13
14
15
16
17
18
19
20
21
# File 'lib/ox/mapper/handler.rb', line 12

def initialize
  # ox supports lines and columns starting from 1.9.0
  # we just need to set these ivars
  @line, @column = nil, nil
  @stack = []
  # collected elements
  @elements_callbacks = Hash.new
  # collected attributes
  @attributes = Hash.new
end

Instance Method Details

#attr(name, value) ⇒ Object

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.

attributes handler assigns attribute value to current element



66
67
68
# File 'lib/ox/mapper/handler.rb', line 66

def attr(name, value)
  top[name] = encode(value) if collect_attribute?(name)
end

#collect_attribute(tag_name, attribute_name) ⇒ Object

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.

Collect values of attributes with given attribute_name at elements with tag of given tag_name

Parameters:

  • tag_name (String, Symbol)
  • attribute_name (String, Symbol)


36
37
38
# File 'lib/ox/mapper/handler.rb', line 36

def collect_attribute(tag_name, attribute_name)
  (@attributes[tag_name.to_sym] ||= Set.new) << attribute_name.to_sym
end

#end_elementObject

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.

“end_element” handler pushes an element if it is attached to callbacks



53
54
55
56
57
58
59
60
61
# File 'lib/ox/mapper/handler.rb', line 53

def end_element(*)
  element = @stack.pop

  if collect_element?(element.name)
    encode(element.text)

    @elements_callbacks[element.name].each { |cb| cb.call(element) }
  end
end

#setup_element_callback(name, callback) ⇒ Object

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.

Assigns callback to elements with given tag name

Parameters:

  • name (String, Symbol)
  • callback (Proc)


27
28
29
# File 'lib/ox/mapper/handler.rb', line 27

def setup_element_callback(name, callback)
  (@elements_callbacks[name.to_sym] ||= []) << callback.to_proc
end

#start_element(name) ⇒ Object

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.

“start_element” handler just pushes an element to stack and assigns a pointer to parent element



43
44
45
46
47
48
# File 'lib/ox/mapper/handler.rb', line 43

def start_element(name)
  element = Element.new(name, @line, @column)
  element.parent = top

  @stack.push(element)
end

#text(value) ⇒ Object Also known as: cdata

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.

text handler appends given value to current element text attribute



73
74
75
76
77
78
79
80
81
82
83
# File 'lib/ox/mapper/handler.rb', line 73

def text(value)
  if value && top
    value.strip!

    if top.text
      top.text << encode(value)
    else
      top.text = encode(value)
    end
  end
end