Class: Polyrex

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(location = nil, opt = {}) ⇒ Polyrex

Returns a new instance of Polyrex.



43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/polyrex.rb', line 43

def initialize(location=nil, opt={})

  options = {id_counter: '1'}.merge opt

  @id_counter = options[:id_counter]
  @format_masks = []
  @delimiter = '' 
  
  if location then

    self.method(:schema=).call(options[:schema]) if options[:schema]

    openx(location)

    if options[:schema] then

      fields = @schema[/\/.*/].scan(/\[([^\]]+)/).map \
        {|x| x.first.split(',').map(&:strip)}
      refresh_records self.records, fields, 0

    end

    @summary = RecordX.new @doc.root.xpath("summary/*")
    @summary_fields = @summary.keys
    

  end
  
  @polyrex_xslt = RecordxXSLT.new
end

Instance Attribute Details

#delimiterObject

Returns the value of attribute delimiter.



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

def delimiter
  @delimiter
end

#id_counterObject

Returns the value of attribute id_counter.



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

def id_counter
  @id_counter
end

#schemaObject

Returns the value of attribute schema.



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

def schema
  @schema
end

#summary_fieldsObject

Returns the value of attribute summary_fields.



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

def summary_fields
  @summary_fields
end

#typeObject

Returns the value of attribute type.



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

def type
  @type
end

#xsltObject

Returns the value of attribute xslt.



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

def xslt
  @xslt
end

#xslt_schemaObject

Returns the value of attribute xslt_schema.



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

def xslt_schema
  @xslt_schema
end

Instance Method Details

#add(pxobj) ⇒ Object



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

def add(pxobj)
  self.record.add pxobj.node
end

#build(records, indent = 0) ⇒ Object



239
240
241
242
243
244
245
246
247
248
249
250
251
# File 'lib/polyrex.rb', line 239

def build(records, indent=0)

  records.map do |item|

    summary = item.element 'summary'
    format_mask = summary.text('format_mask').to_s
    line = format_mask.gsub(/\[![^\]]+\]/){|x| summary.text(x[2..-2]).to_s}

    records = item.element('records').elements.to_a
    line = line + "\n" + build(records, indent + 1).join("\n") if records.length > 0
    ('  ' * indent) + line
  end
end

#content(options = {}) ⇒ Object



78
79
80
# File 'lib/polyrex.rb', line 78

def content(options={})
  CGI.unescapeHTML(to_xml(options))
end

#create(id: nil) ⇒ Object



82
83
84
85
86
87
88
# File 'lib/polyrex.rb', line 82

def create(id: nil)
    # @create is a PolyrexCreateObject, @parent_node is a Rexle::Element pointing to the current record
  
  @create.id = id || @id_counter
  @create.record = @parent_node.name == 'records' ? @parent_node.root : @parent_node.root.element('records')
  @create
end

#delete(x = nil) ⇒ Object



90
91
92
93
94
95
96
97
# File 'lib/polyrex.rb', line 90

def delete(x=nil)

  if x.to_i.to_s  == x.to_s then
    @doc.root.delete("//[@id='#{x}'")
  else
    @doc.root.xpath(x).each(&:delete)
  end
end

#element(s) ⇒ Object



160
161
162
# File 'lib/polyrex.rb', line 160

def element(s)
  @doc.root.element(s)
end

#find_by_id(id) ⇒ Object

– start of crud methods –



132
133
134
135
# File 'lib/polyrex.rb', line 132

def find_by_id(id)
  @parent_node = @doc.root.element("//[@id='#{id}']")
  @objects[@parent_node.name].new(@parent_node, id: @id)
end

#format_masksObject

– start of full text edit methods



145
146
147
# File 'lib/polyrex.rb', line 145

def format_masks
  @format_masks
end

#id(id) ⇒ Object



137
138
139
140
# File 'lib/polyrex.rb', line 137

def id(id)
  @parent_node = @doc.root.element("//[@id='#{id}']")
  self
end

#leaf_nodes_to_dxObject



164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
# File 'lib/polyrex.rb', line 164

def leaf_nodes_to_dx()
  
  schema, record_name = @summary.schema\
                              .match(/([^\/]+\/([^\/]+)\[[^\[]+$)/).captures
  
  xml = RexleBuilder.new

  xml.items do
    xml.summary do
      xml.schema schema.sub(/(\/\w+)\[([^\]]+)\]/,'\1(\2)')
    end
    xml.records 
  end
  
  doc = Rexle.new xml.to_a
  body = doc.root.element 'records'
  a = self.xpath('//' + record_name)

  a.each do |record|
    body.add record.deep_clone
  end

  make_dynarex doc.root
end

#parse(x = nil, options = {}) ⇒ Object Also known as: import



149
150
151
152
153
154
155
156
# File 'lib/polyrex.rb', line 149

def parse(x=nil, options={})

  buffer, type = RXFHelper.read(x)
  
  buffer = yield if block_given?          
  string_parse buffer.clone, options
  self
end

#recordObject



108
109
110
# File 'lib/polyrex.rb', line 108

def record()
  @parent_node
end

#recordsObject



189
190
191
192
193
# File 'lib/polyrex.rb', line 189

def records
  @doc.root.xpath("records/*").map do |record|      
    @objects_a[0].new(record)
  end
end

#rxpath(s) ⇒ Object



195
196
197
198
199
200
201
202
# File 'lib/polyrex.rb', line 195

def rxpath(s)
  
  a = @doc.root.xpath s.split('/').map \
                {|x| x.sub('[','[summary/').prepend('records/')}.join('/')
  
  a.map {|x| @objects[x.name].new(x, id: x.attributes[:id]) }

end

#save(filepath = nil, opt = {}, options: opt, pretty: false) ⇒ Object



117
118
119
120
121
122
123
124
125
126
127
128
# File 'lib/polyrex.rb', line 117

def save(filepath=nil, opt={}, options: opt, pretty: false)    
  
  refresh_summary
  filepath ||= @local_filepath
  @local_filepath = filepath
  
  options.merge!({pretty: pretty}) if options.empty?
  xml = @doc.to_s(options)
  
  buffer = block_given? ? yield(xml) : xml
  File.open(filepath,'w'){|f| f.write buffer}    
end

#summaryObject



215
216
217
# File 'lib/polyrex.rb', line 215

def summary
  @summary
end

#to_aObject



219
220
221
# File 'lib/polyrex.rb', line 219

def to_a()
  recordx_map @doc.root
end

#to_dynarexObject



223
224
225
226
227
228
229
230
231
232
233
234
235
# File 'lib/polyrex.rb', line 223

def to_dynarex()
  root = @doc.root.deep_clone

  summary = root.element('summary')
  summary.delete('format_mask')
  summary.element('recordx_type').text = 'dynarex'

  summary.add root.element('records/*/summary/format_mask').clone    
  e = summary.element('schema')
  e.text = e.text[/[^\/]+\/[^\/]+/].sub(/(\/\w+)\[([^\]]+)\]/,'\1(\2)')

  make_dynarex(root)
end

#to_sObject



237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
# File 'lib/polyrex.rb', line 237

def to_s()

  def build(records, indent=0)

    records.map do |item|

      summary = item.element 'summary'
      format_mask = summary.text('format_mask').to_s
      line = format_mask.gsub(/\[![^\]]+\]/){|x| summary.text(x[2..-2]).to_s}

      records = item.element('records').elements.to_a
      line = line + "\n" + build(records, indent + 1).join("\n") if records.length > 0
      ('  ' * indent) + line
    end
  end

  sumry = ''


  summary_fields = self.summary.to_h.keys

  %w(recordx_type schema format_mask).each {|x| summary_fields.delete x}
  sumry = summary_fields.map {|x| x + ': ' + \
                     self.summary.method(x.to_sym).call}.join("\n") + "\n"


  if @raw_header then
    declaration = @raw_header
  else

    smry_fields = %i(schema)              
    if self.delimiter.length > 0 then
      smry_fields << :delimiter 
    else
      smry_fields << :format_mask
    end
    s = smry_fields.map {|x| "%s=\"%s\"" % \
      [x, self.summary.send(x).gsub('"', '\"') ]}.join ' '

    declaration = %Q(<?polyrex %s?>\n) % s
  end

  header = declaration + sumry
  out = build(self.records).join("\n")
  header + "\n" + out
end

#to_xml(options = {}) ⇒ Object



112
113
114
115
# File 'lib/polyrex.rb', line 112

def to_xml(options={})
  refresh_summary
  @doc.to_s(options)
end

#to_xsltObject



284
285
286
287
# File 'lib/polyrex.rb', line 284

def to_xslt()    
  @polyrex_xslt.schema = @schema
  @polyrex_xslt.to_xslt
end

#xpath(s, &blk) ⇒ Object



289
290
291
292
293
294
295
296
# File 'lib/polyrex.rb', line 289

def xpath(s, &blk)

  if block_given? then
    @doc.root.xpath(s, &blk)
  else
    @doc.root.xpath s
  end
end