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
- #clone ⇒ Object
- #count ⇒ Object
- #create(id: '0') ⇒ Object
- #deep_clone ⇒ Object
- #delete ⇒ Object
- #element(xpath) ⇒ Object
-
#initialize(node, id: '0') ⇒ PolyrexObject
constructor
A new instance of PolyrexObject.
- #inspect ⇒ 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
65 66 67 |
# File 'lib/polyrex-objects.rb', line 65 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 |
#clone ⇒ Object
27 28 29 |
# File 'lib/polyrex-objects.rb', line 27 def clone() self.class.new Rexle.new(self.node.to_a).root end |
#count ⇒ Object
31 32 33 |
# File 'lib/polyrex-objects.rb', line 31 def count() self.records.length end |
#create(id: '0') ⇒ Object
35 36 37 38 39 40 41 42 43 |
# File 'lib/polyrex-objects.rb', line 35 def create(id: '0') id ||=@@id id.succ! @create.id = id @create.record = @node.element('records') @create end |
#deep_clone ⇒ Object
49 50 51 |
# File 'lib/polyrex-objects.rb', line 49 def deep_clone() self.class.new Rexle.new(self.to_xml).root end |
#delete ⇒ Object
45 46 47 |
# File 'lib/polyrex-objects.rb', line 45 def delete() @node.delete end |
#element(xpath) ⇒ Object
53 54 55 |
# File 'lib/polyrex-objects.rb', line 53 def element(xpath) @node.element(xpath) end |
#inspect ⇒ Object
61 62 63 |
# File 'lib/polyrex-objects.rb', line 61 def inspect() "#<PolyrexObject:%s" % __id__ end |
#to_dynarex ⇒ Object
69 70 71 72 73 74 75 76 77 78 79 80 81 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 |
# File 'lib/polyrex-objects.rb', line 69 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 =<<EOF <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0"> <xsl:output encoding="UTF-8" indent="yes" omit-xml-declaration="yes"/> <xsl:template match="*"> <xsl:element name="{name()}"> <xsl:element name="summary"> <xsl:for-each select="summary/*"> <xsl:copy-of select="."/> </xsl:for-each> </xsl:element> <xsl:element name="records"> <xsl:for-each select="records/*"> <xsl:element name="{name()}"> <xsl:copy-of select="summary/*"/> </xsl:element> </xsl:for-each> </xsl:element> </xsl:element> </xsl:template> </xsl:stylesheet> EOF xslt = Nokogiri::XSLT(xsl_buffer) buffer = xslt.transform(Nokogiri::XML(root.xml)).to_s Dynarex.new buffer end |
#to_h ⇒ Object
115 116 117 118 119 |
# File 'lib/polyrex-objects.rb', line 115 def to_h() @fields.inject({}) do |r, field| r.merge(field.capitalize => self.method(field).call) end end |
#to_xml(options = {}) ⇒ Object
121 122 123 |
# File 'lib/polyrex-objects.rb', line 121 def to_xml(={}) @node.xml() end |
#with {|_self| ... } ⇒ Object
125 126 127 |
# File 'lib/polyrex-objects.rb', line 125 def with() yield(self) end |
#xpath(s) ⇒ Object
129 130 131 |
# File 'lib/polyrex-objects.rb', line 129 def xpath(s) @node.xpath(s) end |