Class: PolyrexObjects::PolyrexObject
- Inherits:
-
Object
- Object
- PolyrexObjects::PolyrexObject
- Defined in:
- lib/polyrex-objects.rb
Instance Attribute Summary collapse
-
#id ⇒ Object
readonly
Returns the value of attribute id.
-
#node ⇒ Object
readonly
Returns the value of attribute node.
Instance Method Summary collapse
- #[](n) ⇒ Object
- #add(pxobj) ⇒ Object
- #at_css(s) ⇒ Object
- #clone ⇒ Object
- #count ⇒ Object
- #create(id: '0') ⇒ Object
- #css(s) ⇒ Object
- #deep_clone ⇒ Object
- #delete ⇒ Object
- #element(xpath) ⇒ Object
-
#initialize(node, id: '0', objects: nil) ⇒ PolyrexObject
constructor
A new instance of PolyrexObject.
- #inspect ⇒ Object
- #parent ⇒ Object
- #to_doc ⇒ Object
- #to_dynarex ⇒ Object
- #to_h ⇒ Object
- #to_s ⇒ Object
- #to_xml(options = {}) ⇒ Object
- #with {|_self| ... } ⇒ Object
- #xpath(s) ⇒ Object
Constructor Details
#initialize(node, id: '0', objects: nil) ⇒ PolyrexObject
Returns a new instance of PolyrexObject.
17 18 19 20 21 22 23 24 |
# File 'lib/polyrex-objects.rb', line 17 def initialize(node, id: '0', objects: nil) @@id = id @node = node @fields =[] @objects = objects end |
Instance Attribute Details
#id ⇒ Object (readonly)
Returns the value of attribute id.
15 16 17 |
# File 'lib/polyrex-objects.rb', line 15 def id @id end |
#node ⇒ Object (readonly)
Returns the value of attribute node.
15 16 17 |
# File 'lib/polyrex-objects.rb', line 15 def node @node end |
Instance Method Details
#[](n) ⇒ Object
77 78 79 |
# File 'lib/polyrex-objects.rb', line 77 def [](n) self.records[n] end |
#add(pxobj) ⇒ Object
26 27 28 |
# File 'lib/polyrex-objects.rb', line 26 def add(pxobj) @node.element('records').add pxobj.node end |
#at_css(s) ⇒ Object
31 32 33 |
# File 'lib/polyrex-objects.rb', line 31 def at_css(s) @node.at_css s end |
#clone ⇒ Object
35 36 37 |
# File 'lib/polyrex-objects.rb', line 35 def clone() self.class.new Rexle.new(self.node.to_a).root end |
#count ⇒ Object
39 40 41 |
# File 'lib/polyrex-objects.rb', line 39 def count() self.records.length end |
#create(id: '0') ⇒ Object
43 44 45 46 47 48 49 50 51 |
# File 'lib/polyrex-objects.rb', line 43 def create(id: '0') id ||=@@id id.succ! @create.id = id @create.record = @node.element('records') @create end |
#css(s) ⇒ Object
53 54 55 |
# File 'lib/polyrex-objects.rb', line 53 def css(s) @node.css s end |
#deep_clone ⇒ Object
61 62 63 |
# File 'lib/polyrex-objects.rb', line 61 def deep_clone() self.class.new Rexle.new(self.to_xml).root end |
#delete ⇒ Object
57 58 59 |
# File 'lib/polyrex-objects.rb', line 57 def delete() @node.delete end |
#element(xpath) ⇒ Object
65 66 67 |
# File 'lib/polyrex-objects.rb', line 65 def element(xpath) @node.element(xpath) end |
#inspect ⇒ Object
73 74 75 |
# File 'lib/polyrex-objects.rb', line 73 def inspect() "#<PolyrexObject:%s" % __id__ end |
#parent ⇒ Object
81 82 83 84 85 86 87 |
# File 'lib/polyrex-objects.rb', line 81 def parent() parent_node = self.node.parent.parent @objects[parent_node.name]\ .new(parent_node, id: parent_node.attributes[:id], objects: @objects) end |
#to_doc ⇒ Object
89 90 91 |
# File 'lib/polyrex-objects.rb', line 89 def to_doc() Rexle.new @node.to_a end |
#to_dynarex ⇒ Object
93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 |
# File 'lib/polyrex-objects.rb', line 93 def to_dynarex() root = Rexle.new(self.to_xml).root summary = root.element('summary') e = summary.element('schema') child_schema = root.element('records/*/summary/schema/text()')\ .sub('[','(').sub(']',')') e.text = "%s/%s" % [e.text, child_schema] summary.delete('format_mask') summary.delete('recordx_type') summary.add root.element('records/*/summary/format_mask').clone root.xpath('records/*/summary/format_mask').each(&:delete) xsl_buffer ="<xsl:stylesheet xmlns:xsl=\"http://www.w3.org/1999/XSL/Transform\" version=\"1.0\">\n<xsl:output encoding=\"UTF-8\"\n indent=\"yes\"\n omit-xml-declaration=\"yes\"/>\n\n <xsl:template match=\"*\">\n<xsl:element name=\"{name()}\">\n<xsl:element name=\"summary\">\n <xsl:for-each select=\"summary/*\">\n <xsl:copy-of select=\".\"/>\n </xsl:for-each>\n</xsl:element>\n<xsl:element name=\"records\">\n <xsl:for-each select=\"records/*\">\n <xsl:element name=\"{name()}\">\n <xsl:copy-of select=\"summary/*\"/>\n </xsl:element>\n </xsl:for-each>\n</xsl:element>\n</xsl:element>\n </xsl:template>\n</xsl:stylesheet>\n" begin rexslt = Rexslt.new(xsl_buffer, root.xml) buffer = rexslt.to_s rescue puts ($!).inspect exit end r = Dynarex.new buffer r end |
#to_h ⇒ Object
149 150 151 152 153 |
# File 'lib/polyrex-objects.rb', line 149 def to_h() @fields.inject({}) do |r, field| r.merge(field=> self.method(field).call) end end |
#to_s ⇒ Object
155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 |
# File 'lib/polyrex-objects.rb', line 155 def to_s() if self.respond_to? :records then build(self.records).join("\n") else summary = self.element 'summary' format_mask = summary.text('format_mask').to_s format_mask.gsub(/\[![^\]]+\]/){|x| summary.text(x[2..-2]).to_s} end end |
#to_xml(options = {}) ⇒ Object
171 172 173 |
# File 'lib/polyrex-objects.rb', line 171 def to_xml(={}) @node.xml() end |
#with {|_self| ... } ⇒ Object
175 176 177 |
# File 'lib/polyrex-objects.rb', line 175 def with() yield(self) end |
#xpath(s) ⇒ Object
179 180 181 |
# File 'lib/polyrex-objects.rb', line 179 def xpath(s) @node.xpath(s) end |