Class: PolyrexObjects::PolyrexObject

Inherits:
Object
  • Object
show all
Defined in:
lib/polyrex-objects.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

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

#idObject (readonly)

Returns the value of attribute id.



15
16
17
# File 'lib/polyrex-objects.rb', line 15

def id
  @id
end

#nodeObject (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



89
90
91
# File 'lib/polyrex-objects.rb', line 89

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

#cloneObject



35
36
37
# File 'lib/polyrex-objects.rb', line 35

def clone()
  self.class.new Rexle.new(self.node.to_a).root
end

#countObject



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_cloneObject



61
62
63
# File 'lib/polyrex-objects.rb', line 61

def deep_clone()
  self.class.new Rexle.new(self.to_xml).root
end

#deleteObject



57
58
59
# File 'lib/polyrex-objects.rb', line 57

def delete()
  @node.delete
end

#each_recursive(parent = self, level = 0, &blk) ⇒ Object



65
66
67
68
69
70
71
72
73
74
75
# File 'lib/polyrex-objects.rb', line 65

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



77
78
79
# File 'lib/polyrex-objects.rb', line 77

def element(xpath)
  @node.element(xpath)
end

#inspectObject



85
86
87
# File 'lib/polyrex-objects.rb', line 85

def inspect()
  "#<PolyrexObject:%s" % __id__
end

#parentObject



93
94
95
96
97
98
99
# File 'lib/polyrex-objects.rb', line 93

def parent()

  parent_node = self.node.parent.parent
  @objects[parent_node.name]\
      .new(parent_node, id: parent_node.attributes[:id], objects: @objects)

end

#parent?Boolean

Returns:

  • (Boolean)


101
102
103
104
105
106
107
# File 'lib/polyrex-objects.rb', line 101

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

#to_docObject



109
110
111
# File 'lib/polyrex-objects.rb', line 109

def to_doc()
  Rexle.new @node.to_a
end

#to_dynarexObject



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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
# File 'lib/polyrex-objects.rb', line 113

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 =<<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


  begin
    rexslt = Rexslt.new(xsl_buffer, root.xml)
    buffer = rexslt.to_s
  rescue
    puts ($!).inspect
    exit
  end

  r = Dynarex.new buffer
  r

end

#to_hObject



169
170
171
172
173
# File 'lib/polyrex-objects.rb', line 169

def to_h()
  @fields.inject({}) do |r, field|
    r.merge(field=> self.method(field).call)
  end
end

#to_sObject



175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
# File 'lib/polyrex-objects.rb', line 175

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



191
192
193
# File 'lib/polyrex-objects.rb', line 191

def to_xml(options={})
  @node.xml(options)
end

#with {|_self| ... } ⇒ Object

Yields:

  • (_self)

Yield Parameters:



195
196
197
# File 'lib/polyrex-objects.rb', line 195

def with()
  yield(self)
end

#xpath(s) ⇒ Object



199
200
201
# File 'lib/polyrex-objects.rb', line 199

def xpath(s)
  @node.xpath(s)
end