Module: Atom::Parsers

Included in:
Element
Defined in:
lib/atom/element.rb

Instance Method Summary collapse

Instance Method Details

#on_parse(name_pair, &block) ⇒ Object

adds a parser that calls the given block for a single element that matches the given name and namespace (if it exists)



65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
# File 'lib/atom/element.rb', line 65

def on_parse name_pair, &block
  uri, name = name_pair
  @on_parse ||= []

  process = lambda do |e,x|
    el = e.get_elem(x, uri, name)

    if el
      block.call e, el

      e.extensions.delete_if do |c|
        c.namespace == uri and c.name == name.to_s
      end
    end
  end

  @on_parse << process
end

#on_parse_attr(name_pair, &block) ⇒ Object

adds a parser that calls the given block for the attribute that matches the given name (if it exists)



86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
# File 'lib/atom/element.rb', line 86

def on_parse_attr name_pair, &block
  uri, name = name_pair
  @on_parse ||= []

  process = lambda do |e,x|
    x = e.get_atom_attrb(x, name)

    if x
      block.call e, x

      e.extensions.attributes.delete name.to_s
    end
  end

  @on_parse << process
end

#on_parse_many(name_pair, &block) ⇒ Object

adds a parser that calls the given block for all elements that match the given name and namespace



105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
# File 'lib/atom/element.rb', line 105

def on_parse_many name_pair, &block
  uri, name = name_pair
  @on_parse ||= []

  process = lambda do |e,x|
    els = e.get_elems(x, uri, name)

    unless els.empty?
      block.call e, els

      els.each do |el|
        e.extensions.delete_if { |c| c.namespace == uri and c.name == name.to_s }
      end
    end
  end

  @on_parse << process
end

#on_parse_root(&block) ⇒ Object

adds a parser that calls the given block for this element



125
126
127
128
129
130
131
132
133
134
135
136
137
# File 'lib/atom/element.rb', line 125

def on_parse_root &block
  @on_parse ||= []

  process = lambda do |e,x|
    block.call e, x

    x.elements.each do |el|
      e.extensions.clear
    end
  end

  @on_parse << process
end

#parse_plain(uri, name) ⇒ Object

parses the text content of an element named ‘name’ into an attribute on this Element named ‘name’



141
142
143
144
145
# File 'lib/atom/element.rb', line 141

def parse_plain uri, name
  self.on_parse [uri, name] do |e,x|
    e.set(name, x.text)
  end
end