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') ⇒ PolyrexObject
constructor
A new instance of PolyrexObject.
- #inspect ⇒ Object
- #to_doc ⇒ Object
- #to_dynarex ⇒ Object
- #to_h ⇒ Object
- #to_xml(options = {}) ⇒ Object
- #with {|_self| ... } ⇒ Object
- #xpath(s) ⇒ Object
Constructor Details
#initialize(node, id: '0') ⇒ PolyrexObject
Returns a new instance of PolyrexObject.
17 18 19 20 21 |
# File 'lib/polyrex-objects.rb', line 17 def initialize(node, id: '0') @@id = id @node = node @fields =[] 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
74 75 76 |
# File 'lib/polyrex-objects.rb', line 74 def [](n) self.records[n] end |
#add(pxobj) ⇒ Object
23 24 25 |
# File 'lib/polyrex-objects.rb', line 23 def add(pxobj) @node.element('records').add pxobj.node end |
#at_css(s) ⇒ Object
28 29 30 |
# File 'lib/polyrex-objects.rb', line 28 def at_css(s) @node.at_css s end |
#clone ⇒ Object
32 33 34 |
# File 'lib/polyrex-objects.rb', line 32 def clone() self.class.new Rexle.new(self.node.to_a).root end |
#count ⇒ Object
36 37 38 |
# File 'lib/polyrex-objects.rb', line 36 def count() self.records.length end |
#create(id: '0') ⇒ Object
40 41 42 43 44 45 46 47 48 |
# File 'lib/polyrex-objects.rb', line 40 def create(id: '0') id ||=@@id id.succ! @create.id = id @create.record = @node.element('records') @create end |
#css(s) ⇒ Object
50 51 52 |
# File 'lib/polyrex-objects.rb', line 50 def css(s) @node.css s end |
#deep_clone ⇒ Object
58 59 60 |
# File 'lib/polyrex-objects.rb', line 58 def deep_clone() self.class.new Rexle.new(self.to_xml).root end |
#delete ⇒ Object
54 55 56 |
# File 'lib/polyrex-objects.rb', line 54 def delete() @node.delete end |
#element(xpath) ⇒ Object
62 63 64 |
# File 'lib/polyrex-objects.rb', line 62 def element(xpath) @node.element(xpath) end |
#inspect ⇒ Object
70 71 72 |
# File 'lib/polyrex-objects.rb', line 70 def inspect() "#<PolyrexObject:%s" % __id__ end |
#to_doc ⇒ Object
78 79 80 |
# File 'lib/polyrex-objects.rb', line 78 def to_doc() Rexle.new @node.to_a end |
#to_dynarex ⇒ Object
82 83 84 85 86 87 88 89 90 91 92 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 |
# File 'lib/polyrex-objects.rb', line 82 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.element('recordx_type').text = 'dynarex' 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" xslt = Nokogiri::XSLT(xsl_buffer) buffer = xslt.transform(Nokogiri::XML(root.xml)).to_s Dynarex.new buffer end |
#to_h ⇒ Object
128 129 130 131 132 |
# File 'lib/polyrex-objects.rb', line 128 def to_h() @fields.inject({}) do |r, field| r.merge(field.capitalize => self.method(field).call) end end |
#to_xml(options = {}) ⇒ Object
134 135 136 |
# File 'lib/polyrex-objects.rb', line 134 def to_xml(={}) @node.xml() end |
#with {|_self| ... } ⇒ Object
138 139 140 |
# File 'lib/polyrex-objects.rb', line 138 def with() yield(self) end |
#xpath(s) ⇒ Object
142 143 144 |
# File 'lib/polyrex-objects.rb', line 142 def xpath(s) @node.xpath(s) end |