Module: Atom::Converters
- Included in:
- Element
- Defined in:
- lib/atom/element.rb
Instance Method Summary collapse
-
#atom_attrb(name) ⇒ Object
an XML attribute in the Atom namespace.
-
#atom_element(name, klass) ⇒ Object
an element that is parsed by Element descendant ‘klass’.
-
#atom_elements(one_name, many_name, klass) ⇒ Object
like #elements but in the Atom namespace.
-
#atom_link(name, criteria) ⇒ Object
a type of Atom Link.
-
#atom_string(name) ⇒ Object
an element in the Atom namespace containing text.
-
#atom_time(name) ⇒ Object
an element in the Atom namespace containing a timestamp.
-
#attrb(ns, name) ⇒ Object
an XML attribute in the namespace ‘ns’.
- #build_plain(ns, name) ⇒ Object
-
#element(ns, name, klass) ⇒ Object
an element that is parsed by Element descendant ‘klass’.
-
#elements(ns, one_name, many_name, klass) ⇒ Object
an element that can appear multiple times that is parsed by Element descendant ‘klass’.
-
#strings(ns, one_name, many_name) ⇒ Object
an element that can appear multiple times that contains text.
-
#time(ns, name) ⇒ Object
an element in namespace ‘ns’ containing a RFC3339 timestamp.
Instance Method Details
#atom_attrb(name) ⇒ Object
an XML attribute in the Atom namespace
304 305 306 |
# File 'lib/atom/element.rb', line 304 def atom_attrb(name) self.attrb(['atom', Atom::NS], name) end |
#atom_element(name, klass) ⇒ Object
an element that is parsed by Element descendant ‘klass’
217 218 219 |
# File 'lib/atom/element.rb', line 217 def atom_element(name, klass) self.element(['atom', Atom::NS], name, klass) end |
#atom_elements(one_name, many_name, klass) ⇒ Object
like #elements but in the Atom namespace
277 278 279 |
# File 'lib/atom/element.rb', line 277 def atom_elements(one_name, many_name, klass) self.elements(['atom', Atom::NS], one_name, many_name, klass) end |
#atom_link(name, criteria) ⇒ Object
a type of Atom Link. specifics defined by Hash ‘criteria’
309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 |
# File 'lib/atom/element.rb', line 309 def atom_link name, criteria def_get name do existing = find_link(criteria) existing and existing.href end def_set name do |value| existing = find_link(criteria) if existing existing.href = value else links.new criteria.merge(:href => value) end end end |
#atom_string(name) ⇒ Object
an element in the Atom namespace containing text
159 160 161 162 163 164 |
# File 'lib/atom/element.rb', line 159 def atom_string(name) attr_accessor name self.parse_plain(Atom::NS, name) self.build_plain(['atom', Atom::NS], name) end |
#atom_time(name) ⇒ Object
an element in the Atom namespace containing a timestamp
189 190 191 |
# File 'lib/atom/element.rb', line 189 def atom_time(name) self.time ['atom', Atom::NS], name end |
#attrb(ns, name) ⇒ Object
an XML attribute in the namespace ‘ns’
282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 |
# File 'lib/atom/element.rb', line 282 def attrb(ns, name) attr_accessor name self.on_parse_attr [ns[1], name] do |e,x| e.set(name, x) end self.on_build do |e,x| if v = e.get(name) n = name.to_s if x.namespace != ns[1] x.add_namespace *ns unless x.namespaces[ns[0]] n = "#{ns[0]}:#{n}" end x.attributes[n] = v.to_s end end end |
#build_plain(ns, name) ⇒ Object
149 150 151 152 153 154 155 156 |
# File 'lib/atom/element.rb', line 149 def build_plain ns, name self.on_build do |e,x| if v = e.get(name) el = e.append_elem(x, ns, name) el.text = v.to_s end end end |
#element(ns, name, klass) ⇒ Object
an element that is parsed by Element descendant ‘klass’
194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 |
# File 'lib/atom/element.rb', line 194 def element(ns, name, klass) el_name = name name = name.to_s.gsub(/-/, '_') attr_reader name self.on_parse [ns[1], el_name] do |e,x| e.instance_variable_set("@#{name}", klass.parse(x, e.base)) end self.on_build do |e,x| if v = e.get(name) el = e.append_elem(x, ns, el_name) v.build(el) end end def_set name do |value| instance_variable_set("@#{name}", klass.new(value)) end end |
#elements(ns, one_name, many_name, klass) ⇒ Object
an element that can appear multiple times that is parsed by Element descendant ‘klass’
‘one_name’ is the name of the element, ‘many_name’ is the name of the attribute that will be created on this Element
252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 |
# File 'lib/atom/element.rb', line 252 def elements(ns, one_name, many_name, klass) attr_reader many_name self.on_init do var = Atom::Multiple(klass).new instance_variable_set("@#{many_name}", var) end self.on_parse_many [ns[1], one_name] do |e,xs| var = e.get(many_name) xs.each do |el| var << klass.parse(el, e.base) end end self.on_build do |e,x| e.get(many_name).each do |v| el = e.append_elem(x, ns, one_name) v.build(el) end end end |
#strings(ns, one_name, many_name) ⇒ Object
an element that can appear multiple times that contains text
‘one_name’ is the name of the element, ‘many_name’ is the name of the attribute that will be created on this Element
225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 |
# File 'lib/atom/element.rb', line 225 def strings(ns, one_name, many_name) attr_reader many_name self.on_init do instance_variable_set("@#{many_name}", []) end self.on_parse_many [ns[1], one_name] do |e,xs| var = e.instance_variable_get("@#{many_name}") xs.each do |el| var << el.text end end self.on_build do |e,x| e.instance_variable_get("@#{many_name}").each do |v| e.append_elem(x, ns, one_name).text = v end end end |
#time(ns, name) ⇒ Object
an element in namespace ‘ns’ containing a RFC3339 timestamp
167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 |
# File 'lib/atom/element.rb', line 167 def time(ns, name) attr_reader name self.def_set name do |time| unless time.respond_to? :iso8601 time = Time.parse(time.to_s) end def time.to_s; iso8601; end instance_variable_set("@#{name}", time) end define_method "#{name}!".to_sym do set(name, Time.now) end self.parse_plain(ns[1], name) self.build_plain(ns, name) end |