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.



261
262
263
264
265
# File 'lib/atom/xml/parser.rb', line 261

def initialize(name, options = {})
  @name = name.to_s
  @attribute = name.to_s.sub(/:/, '_')
  @options = options
end

Instance Attribute Details

#attributeObject (readonly)

:nodoc:



259
260
261
# File 'lib/atom/xml/parser.rb', line 259

def attribute
  @attribute
end

#nameObject (readonly)

:nodoc:



259
260
261
# File 'lib/atom/xml/parser.rb', line 259

def name
  @name
end

#optionsObject (readonly)

:nodoc:



259
260
261
# File 'lib/atom/xml/parser.rb', line 259

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.



270
271
272
273
274
275
276
277
# File 'lib/atom/xml/parser.rb', line 270

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

#single?Boolean

Returns:

  • (Boolean)


279
280
281
# File 'lib/atom/xml/parser.rb', line 279

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