Class: PolyrexObjects::PolyrexObject
- Inherits:
-
Object
- Object
- PolyrexObjects::PolyrexObject
- Defined in:
- lib/polyrex-objects.rb
Instance Method Summary collapse
- #[](n) ⇒ Object
- #add(pxobj) ⇒ Object
- #at_css(s) ⇒ Object
- #attributes ⇒ Object
- #clone ⇒ Object
- #count ⇒ Object
- #create(id: @id) ⇒ Object
- #css(s) ⇒ Object
- #deep_clone ⇒ Object
- #delete ⇒ Object
- #each_recursive(parent = self, level = 0, &blk) ⇒ Object
- #element(xpath) ⇒ Object
- #id ⇒ Object
-
#initialize(node, id: '0', debug: false) ⇒ PolyrexObject
constructor
A new instance of PolyrexObject.
- #inspect ⇒ Object
- #parent ⇒ Object
- #parent? ⇒ Boolean
- #records ⇒ 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', debug: false) ⇒ PolyrexObject
Returns a new instance of PolyrexObject.
14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 |
# File 'lib/polyrex-objects.rb', line 14 def initialize(node, id: '0', debug: false) @id = id @fields =[] @node, @debug = node, debug e = node.element('summary') @schema = e.text('schema') @child_schema = @schema =~ /\// ? @schema[/(?<=\/).*/] : @schema puts '@child_schema:' + @child_schema.inspect if @debug @record = @schema[/^[^\[]+/] @fields = @schema[/(?<=\[)[^\]]+/].split(/ *, */) attr = @fields.map {|x| [x, e.text(x)] } build_attributes attr define_singleton_method(@record.to_sym) { self.records} end |
Instance Method Details
#[](n) ⇒ Object
96 97 98 |
# File 'lib/polyrex-objects.rb', line 96 def [](n) self.records[n] end |
#add(pxobj) ⇒ Object
34 35 36 |
# File 'lib/polyrex-objects.rb', line 34 def add(pxobj) @node.element('records').add pxobj.node end |
#at_css(s) ⇒ Object
39 40 41 |
# File 'lib/polyrex-objects.rb', line 39 def at_css(s) @node.at_css s end |
#attributes ⇒ Object
43 44 45 |
# File 'lib/polyrex-objects.rb', line 43 def attributes() @node.attributes end |
#clone ⇒ Object
47 48 49 |
# File 'lib/polyrex-objects.rb', line 47 def clone() self.class.new Rexle.new(self.node.to_a).root end |
#count ⇒ Object
51 52 53 |
# File 'lib/polyrex-objects.rb', line 51 def count() self.records.length end |
#create(id: @id) ⇒ Object
55 56 57 58 |
# File 'lib/polyrex-objects.rb', line 55 def create(id: @id) PolyrexCreateObject.new(id: id, record: @node) end |
#css(s) ⇒ Object
60 61 62 |
# File 'lib/polyrex-objects.rb', line 60 def css(s) @node.css s end |
#deep_clone ⇒ Object
68 69 70 |
# File 'lib/polyrex-objects.rb', line 68 def deep_clone() self.class.new Rexle.new(self.to_xml).root end |
#delete ⇒ Object
64 65 66 |
# File 'lib/polyrex-objects.rb', line 64 def delete() @node.delete end |
#each_recursive(parent = self, level = 0, &blk) ⇒ Object
72 73 74 75 76 77 78 79 80 81 82 |
# File 'lib/polyrex-objects.rb', line 72 def each_recursive(parent=self, level=0, &blk) parent.records.each.with_index do |x, index| blk.call(x, parent, level, index) if block_given? each_recursive(x, level+1, &blk) if x.records.any? end end |
#element(xpath) ⇒ Object
84 85 86 |
# File 'lib/polyrex-objects.rb', line 84 def element(xpath) @node.element(xpath) end |
#id ⇒ Object
88 89 90 |
# File 'lib/polyrex-objects.rb', line 88 def id() @node.attributes[:id] end |
#inspect ⇒ Object
92 93 94 |
# File 'lib/polyrex-objects.rb', line 92 def inspect() "#<%s:%s" % [self.class.name, __id__] end |
#parent ⇒ Object
100 101 102 103 104 105 106 107 |
# File 'lib/polyrex-objects.rb', line 100 def parent() parent_node = self.node.parent.parent Kernel.const_get(parent_node.name.capitalize)\ .new(parent_node, id: parent_node.attributes[:id]) end |
#parent? ⇒ Boolean
109 110 111 112 113 114 115 |
# File 'lib/polyrex-objects.rb', line 109 def parent?() # if the node is at the 1st level of records it will not have a # PolyrexObject parent node.parent.parent.parent.parent ? true : false end |
#records ⇒ Object
117 118 119 120 121 122 123 |
# File 'lib/polyrex-objects.rb', line 117 def records() @node.xpath('records/*').map do |node| Kernel.const_get(node.name.capitalize).new node end end |
#to_doc ⇒ Object
125 126 127 |
# File 'lib/polyrex-objects.rb', line 125 def to_doc() Rexle.new @node.to_a end |
#to_dynarex ⇒ Object
129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 |
# File 'lib/polyrex-objects.rb', line 129 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
184 185 186 187 188 |
# File 'lib/polyrex-objects.rb', line 184 def to_h() @fields.inject({}) do |r, field| r.merge(field=> self.method(field).call) end end |
#to_s ⇒ Object
190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 |
# File 'lib/polyrex-objects.rb', line 190 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
206 207 208 |
# File 'lib/polyrex-objects.rb', line 206 def to_xml(={}) @node.xml() end |
#with {|_self| ... } ⇒ Object
210 211 212 |
# File 'lib/polyrex-objects.rb', line 210 def with() yield(self) end |
#xpath(s) ⇒ Object
214 215 216 |
# File 'lib/polyrex-objects.rb', line 214 def xpath(s) @node.xpath(s) end |