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', debug: false) ⇒ PolyrexObject

Returns a new instance of PolyrexObject.



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/polyrex-objects.rb', line 16

def initialize(node, id: '0', debug: false)

  @id = id
  @fields =[]
  @node, @debug = node, debug

  if node then
    
    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

end

Instance Attribute Details

#idObject (readonly)

Returns the value of attribute id.



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

def id
  @id
end

#nodeObject (readonly)

Returns the value of attribute node.



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

def node
  @node
end

Instance Method Details

#[](n) ⇒ Object



102
103
104
# File 'lib/polyrex-objects.rb', line 102

def [](n)
  self.records[n]
end

#add(pxobj) ⇒ Object



40
41
42
# File 'lib/polyrex-objects.rb', line 40

def add(pxobj)
  @node.element('records').add pxobj.node
end

#at_css(s) ⇒ Object



45
46
47
# File 'lib/polyrex-objects.rb', line 45

def at_css(s)
  @node.at_css s
end

#attributesObject



49
50
51
# File 'lib/polyrex-objects.rb', line 49

def attributes()
  @node.attributes
end

#cloneObject



53
54
55
# File 'lib/polyrex-objects.rb', line 53

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

#countObject



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

def count()
  self.records.length
end

#create(id: @id) ⇒ Object



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

def create(id: @id)      

  PolyrexCreateObject.new(id: id, record: @node)
end

#css(s) ⇒ Object



66
67
68
# File 'lib/polyrex-objects.rb', line 66

def css(s)
  @node.css s
end

#deep_cloneObject



74
75
76
# File 'lib/polyrex-objects.rb', line 74

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

#deleteObject



70
71
72
# File 'lib/polyrex-objects.rb', line 70

def delete()
  @node.delete
end

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



78
79
80
81
82
83
84
85
86
87
88
# File 'lib/polyrex-objects.rb', line 78

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



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

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

#inspectObject



98
99
100
# File 'lib/polyrex-objects.rb', line 98

def inspect()
  "#<%s:%s" % [self.class.name, __id__]
end

#parentObject



106
107
108
109
110
111
112
113
# File 'lib/polyrex-objects.rb', line 106

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

Returns:

  • (Boolean)


115
116
117
118
119
120
121
# File 'lib/polyrex-objects.rb', line 115

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

#recordsObject



123
124
125
126
127
128
129
# File 'lib/polyrex-objects.rb', line 123

def records()

  @node.xpath('records/*').map do |node|
    Kernel.const_get(node.name.capitalize).new node
  end

end

#to_docObject



131
132
133
# File 'lib/polyrex-objects.rb', line 131

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

#to_dynarexObject



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
183
184
185
186
187
188
# File 'lib/polyrex-objects.rb', line 135

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



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

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

#to_sObject



196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
# File 'lib/polyrex-objects.rb', line 196

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



212
213
214
# File 'lib/polyrex-objects.rb', line 212

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

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

Yields:

  • (_self)

Yield Parameters:



216
217
218
# File 'lib/polyrex-objects.rb', line 216

def with()
  yield(self)
end

#xpath(s) ⇒ Object



220
221
222
# File 'lib/polyrex-objects.rb', line 220

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