Class: Atom::Xml::Parseable::ParseSpec

Inherits:
Object
  • Object
show all
Defined in:
lib/atom/xml/parser.rb

Overview

Contains the specification for how an element should be parsed.

This should not need to be constructed directly, instead use the element and elements macros in the declaration of the class.

See Parseable.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, options = {}) ⇒ ParseSpec

Returns a new instance of ParseSpec



354
355
356
357
358
# File 'lib/atom/xml/parser.rb', line 354

def initialize(name, options = {})
  @name = name.to_s
  @attribute = Atom.to_attrname(name)
  @options = options
end

Instance Attribute Details

#attributeObject (readonly)

:nodoc:



352
353
354
# File 'lib/atom/xml/parser.rb', line 352

def attribute
  @attribute
end

#nameObject (readonly)

:nodoc:



352
353
354
# File 'lib/atom/xml/parser.rb', line 352

def name
  @name
end

#optionsObject (readonly)

:nodoc:



352
353
354
# File 'lib/atom/xml/parser.rb', line 352

def options
  @options
end

Instance Method Details

#parse(target, xml) ⇒ Object

Parses a chunk of XML according the specification. The data extracted will be assigned to the target object.



363
364
365
366
367
368
369
370
371
372
# File 'lib/atom/xml/parser.rb', line 363

def parse(target, xml)
  case options[:type]
  when :single
    target.send("#{@attribute}=".to_sym, build(target, xml))
  when :collection
    collection = target.send(@attribute.to_s)
    element    = build(target, xml)
    collection << element
  end
end

#single?Boolean

Returns:

  • (Boolean)


374
375
376
# File 'lib/atom/xml/parser.rb', line 374

def single?
  options[:type] == :single
end