Class: Objectify::Xml
- Inherits:
-
Object
- Object
- Objectify::Xml
- Defined in:
- lib/objectify_xml.rb,
lib/objectify_xml/dsl.rb
Overview
Base class inherited by the DocumentParser and ElementParser. Not intended for independent use.
Direct Known Subclasses
Defined Under Namespace
Modules: Dsl
Constant Summary collapse
- VERSION =
'0.2.3'
Instance Attribute Summary collapse
-
#attributes ⇒ Object
readonly
A hash containing the values of the xml document’s nodes.
-
#parent ⇒ Object
readonly
When child nodes are created, they are given the name of the node that created them which is available here.
Class Method Summary collapse
-
.first_element(xml) ⇒ Object
Returns the first Nokogiri::XML::Element if any in the document.
- .inherited(target) ⇒ Object
Instance Method Summary collapse
-
#initialize(xml, parent = nil) ⇒ Xml
constructor
A new instance of Xml.
-
#inspect ⇒ Object
A short but informative indication of data type and which and how many elements are present.
-
#pretty_print(q) ⇒ Object
require ‘pp’.
Constructor Details
#initialize(xml, parent = nil) ⇒ Xml
Returns a new instance of Xml.
48 49 50 51 52 53 |
# File 'lib/objectify_xml.rb', line 48 def initialize(xml, parent = nil) @parent = parent @attributes = {} xml = self.class.first_element(xml) primary_xml_element(xml) if xml end |
Instance Attribute Details
#attributes ⇒ Object (readonly)
A hash containing the values of the xml document’s nodes. The data is usually better accessed through the getter and setter methods that are created for all attributes, has_one and has_many associations.
23 24 25 |
# File 'lib/objectify_xml.rb', line 23 def attributes @attributes end |
#parent ⇒ Object (readonly)
When child nodes are created, they are given the name of the node that created them which is available here.
18 19 20 |
# File 'lib/objectify_xml.rb', line 18 def parent @parent end |
Class Method Details
.first_element(xml) ⇒ Object
Returns the first Nokogiri::XML::Element if any in the document.
The xml attribute may be a string, a File object or a Nokogiri::XML object.
34 35 36 37 38 39 40 41 42 43 44 45 46 |
# File 'lib/objectify_xml.rb', line 34 def self.first_element(xml) return if xml.nil? if xml.is_a?(String) or xml.is_a?(File) xml = Nokogiri::XML.parse(xml) end # skip the <?xml?> tag xml = xml.child if xml.class == Nokogiri::XML::Document while xml and xml.class != Nokogiri::XML::Element # skips past things like xml-stylesheet declarations. xml = xml.next end xml end |
.inherited(target) ⇒ Object
25 26 27 28 |
# File 'lib/objectify_xml.rb', line 25 def self.inherited(target) # The Dsl module is added to every class that inherits from this target.extend Dsl end |
Instance Method Details
#inspect ⇒ Object
A short but informative indication of data type and which and how many elements are present.
57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 |
# File 'lib/objectify_xml.rb', line 57 def inspect begin attrs = (attributes || {}).map do |k,v| if v.is_a? Objectify::Xml "#{ k }:#{ v.class.name }" elsif v.is_a? Array "#{ k }:#{ v.length }" else k.to_s end end "<#{ self.class.name } #{ attrs.join(', ') }>" rescue => e "<#{ self.class.name } Error inspecting class: #{ e.name } #{ e. }>" end end |
#pretty_print(q) ⇒ Object
require ‘pp’
A more detailed recursive dump of the data in and associated to the object.
78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 |
# File 'lib/objectify_xml.rb', line 78 def pretty_print(q) begin q.object_group(self) do q.breakable q.seplist(attributes, nil, :each_pair) do |k, v| q.text "#{ k.to_s }: " if v.is_a? String and v.length > 200 q.text "#{ v[0..80] }...".inspect else q.pp v end end end rescue => e q.text "<#{ self.class.name } Error inspecting class: #{ e.name } #{ e. }>" end end |