dynarex 0.9.4 → 0.9.5

Sign up to get free protection for your applications and to get access to all the features.
Files changed (2) hide show
  1. data/lib/dynarex.rb +59 -52
  2. metadata +2 -2
@@ -2,14 +2,13 @@
2
2
 
3
3
  # file: dynarex.rb
4
4
 
5
- require 'rexml/document'
6
5
  require 'nokogiri'
7
6
  require 'open-uri'
8
7
  require 'builder'
9
8
  require 'ostruct'
9
+ require 'rexle'
10
10
 
11
11
  class Dynarex
12
- include REXML
13
12
 
14
13
  attr_accessor :format_mask, :delimiter, :schema
15
14
 
@@ -81,14 +80,15 @@ xsl_buffer =<<EOF
81
80
  </xsl:stylesheet>
82
81
  EOF
83
82
 
84
- format_mask = XPath.first(@doc.root, 'summary/format_mask/text()').to_s
85
- dynarex = Dynarex.new
83
+ #format_mask = XPath.first(@doc.root, 'summary/format_mask/text()').to_s
84
+ format_mask = @doc.element('summary/format_mask/text()')
85
+ dynarex = Dynarex.new
86
86
 
87
- s = %q(schema="companies/company(name, last_contacted, contact)" delimiter=" # ")
88
- s.split(/(?=\w+\="[^"]+")/)
89
- raw_a = s.scan(/\w+\="[^"]+"/)
90
- a = raw_a.map{|x| r = x.split(/=/); [(r[0] + "=").to_sym, r[1][/^"(.*)"$/,1]] }
91
- a.each{|name, value| dynarex.method(name).call(value)}
87
+ s = %q(schema="companies/company(name, last_contacted, contact)" delimiter=" # ")
88
+ s.split(/(?=\w+\="[^"]+")/)
89
+ raw_a = s.scan(/\w+\="[^"]+"/)
90
+ a = raw_a.map{|x| r = x.split(/=/); [(r[0] + "=").to_sym, r[1][/^"(.*)"$/,1]] }
91
+ a.each{|name, value| dynarex.method(name).call(value)}
92
92
 
93
93
  xslt_format = format_mask.to_s.gsub(/\s(?=\[!\w+\])/,'<xsl:text> </xsl:text>').gsub(/\[!(\w+)\]/, '<xsl:value-of select="\1"/>')
94
94
  xsl_buffer.sub!(/\[!regex_values\]/, xslt_format)
@@ -96,6 +96,7 @@ a.each{|name, value| dynarex.method(name).call(value)}
96
96
  xslt = Nokogiri::XSLT(xsl_buffer)
97
97
  out = xslt.transform(Nokogiri::XML(@doc.to_s))
98
98
  out.text
99
+
99
100
  end
100
101
 
101
102
  def to_xml
@@ -121,7 +122,8 @@ a.each{|name, value| dynarex.method(name).call(value)}
121
122
  header.scan(/\w+\="[^"]+\"/).map{|x| r = x.split(/=/); [(r[0] + "=").to_sym, r[1][/^"(.*)"$/,1]] }.each {|name, value| self.method(name).call(value)}
122
123
  end
123
124
 
124
- i = XPath.match(@doc.root, 'records/*/attribute::id').max_by(&:value).to_s.to_i
125
+ #i = XPath.match(@doc.root, 'records/*/attribute::id').max_by(&:value).to_s.to_i
126
+ i = @doc.xpath('records/*/attribute::id').max_by(&:value).to_s.to_i
125
127
  t = @format_mask.to_s.gsub(/\[!(\w+)\]/, '(.*)').sub(/\[/,'\[').sub(/\]/,'\]')
126
128
 
127
129
  lines = buffer.strip.split(/\r?\n|\r(?!\n)/).map {|x|x.match(/#{t}/).captures}
@@ -191,7 +193,7 @@ a.each{|name, value| dynarex.method(name).call(value)}
191
193
  fields = capture_fields(params)
192
194
 
193
195
  # for each field update each record field
194
- record = XPath.first(@doc.root, "records/#{@record_name}[@id=#{id.to_s}]")
196
+ record = @doc.element("records/#{@record_name}[@id=#{id.to_s}]")
195
197
  fields.each {|k,v| record.elements[k.to_s].text = v if v}
196
198
  record.add_attribute('last_modified', Time.now.to_s)
197
199
 
@@ -203,7 +205,7 @@ a.each{|name, value| dynarex.method(name).call(value)}
203
205
  # dyarex.delete 3 # deletes record with id 3
204
206
 
205
207
  def delete(id)
206
- node = XPath.first(@doc.root, "records/*[@id='#{id}']")
208
+ node = @doc.element("records/*[@id='#{id}']")
207
209
  node.parent.delete node
208
210
  load_records
209
211
  self
@@ -211,44 +213,46 @@ a.each{|name, value| dynarex.method(name).call(value)}
211
213
 
212
214
  def sort_by!(&element_blk)
213
215
  refresh_doc
214
- a = XPath.match(@doc.root, 'records/*').sort_by &element_blk
215
- records = XPath.first(@doc.root, 'records')
216
- records.parent.delete records
217
- records = Element.new 'records'
216
+ a = @doc.xpath('records/*').sort_by &element_blk
217
+ @doc.delete('records')
218
+
219
+ records = Rexle::Element.new 'records'
218
220
  a.each {|record| records.add record}
219
221
  @doc.root.add records
222
+
220
223
  load_records
221
224
  self
222
225
  end
223
226
 
224
227
  def record(id)
225
- h = Hash[*XPath.match(@doc.root, "records/*[@id='#{id}']/*").map {|x| [x.name, x.text]}.flatten]
228
+ h = Hash[*@doc.xpath("records/*[@id='#{id}']/*").map {|x| [x.name, x.text]}.flatten]
226
229
  OpenStruct.new h
227
230
  end
228
231
 
229
232
  def record_exists?(id)
230
- XPath.first(@doc.root, "records/*[@id='#{id}']").nil? ? false : true
233
+ !@doc.element("records/*[@id='#{id}']").nil?
231
234
  end
232
235
 
233
236
  private
234
237
 
235
238
  def hash_create(params={}, id=nil)
236
239
  fields = capture_fields(params)
237
- record = Element.new @record_name
240
+ record = Rexle::Element.new @record_name
238
241
  fields.each do |k,v|
239
- element = Element.new(k.to_s)
242
+ element = Rexle::Element.new(k.to_s)
240
243
  element.text = v if v
241
244
  record.add element
242
245
  end
243
246
 
244
247
  unless id
245
- ids = XPath.match(@doc.root,'records/*/attribute::id').map {|x| x.value.to_i}
248
+ ids = @doc.xpath('records/*/attribute::id').map(&:to_i)
246
249
  id = ids.empty? ? (id || 1) : ids.max.succ
247
250
  end
248
251
 
249
252
  attributes = {id: id, created: Time.now.to_s, last_modified: nil}
250
253
  attributes.each {|k,v| record.add_attribute(k.to_s, v)}
251
- @doc.root.elements['records'].add record
254
+
255
+ @doc.element('records').add record
252
256
  end
253
257
 
254
258
  def capture_fields(params)
@@ -279,7 +283,7 @@ a.each{|name, value| dynarex.method(name).call(value)}
279
283
  end
280
284
  end
281
285
 
282
- @doc = Document.new buffer
286
+ @doc = Rexle.new buffer
283
287
  buffer
284
288
 
285
289
  end
@@ -317,29 +321,32 @@ a.each{|name, value| dynarex.method(name).call(value)}
317
321
  @fields.each do |field|
318
322
  self.instance_eval(
319
323
  %Q(def find_by_#{field}(s)
320
- Hash[@fields.zip(XPath.match(@doc.root, "records/*[#{field}='\#{s}']/*/text()").map &:to_s)]
324
+ Hash[@fields.zip(@doc.xpath("records/*[#{field}='\#{s}']/*/text()"))]
321
325
  end))
322
326
  end
323
327
  end
324
328
 
325
329
  def open(s)
326
- if s[/\(/] then # schema
330
+
331
+ if s.strip[/^</] then # xml
332
+ puts 'fuh'
333
+ buffer = s
334
+ elsif s[/\(/] # schema
335
+ puts 'duh'
327
336
  buffer = dynarex_new s
328
337
  elsif s[/^https?:\/\//] then # url
329
338
  buffer = Kernel.open(s, 'UserAgent' => 'Dynarex-Reader').read
330
- elsif s[/\</] # xml
331
- buffer = s
332
339
  else # local file
333
340
  @local_filepath = s
334
341
  buffer = File.open(s,'r').read
335
342
  end
336
343
 
337
- @doc = Document.new buffer
344
+ @doc = Rexle.new buffer
338
345
  @schema = @doc.root.text('summary/schema')
339
346
  @root_name = @doc.root.name
340
347
  @summary = summary_to_h
341
- @default_key = XPath.first(@doc.root, 'summary/default_key/text()')
342
- @format_mask = XPath.first(@doc.root, 'summary/format_mask/text()')
348
+ @default_key = @doc.element('summary/default_key/text()')
349
+ @format_mask = @doc.element('summary/format_mask/text()')
343
350
 
344
351
  @fields = @format_mask.to_s.scan(/\[!(\w+)\]/).flatten.map(&:to_sym) if @format_mask
345
352
 
@@ -352,10 +359,12 @@ end))
352
359
  @default_key = @fields[0] unless @default_key
353
360
  # load the record query handler methods
354
361
  attach_record_methods
362
+ else
363
+ @default_key = @doc.xpath('records/*/*').first.name
355
364
  end
356
365
 
357
- if XPath.match(@doc.root, 'records/*').length > 0 then
358
- @record_name = XPath.first(@doc.root, 'records/*[1]').name
366
+ if @doc.xpath('records/*').length > 0 then
367
+ @record_name = @doc.element('records/*[1]').name
359
368
  load_records
360
369
  end
361
370
 
@@ -373,44 +382,42 @@ end))
373
382
  #Returns a ready-only snapshot of records as a simple Hash.
374
383
 
375
384
  def flat_records_to_h
376
- XPath.match(@doc.root, 'records/*').map do |row|
377
- XPath.match(row, '*').inject({}) do |r,node|
378
- r[node.name.to_s.to_sym] = node.text.to_s
379
- r
385
+ @doc.xpath('records/*').map do |row|
386
+ row.xpath('*').inject({}) do |r,node|
387
+ r.merge node.name.to_s.to_sym => node.text
380
388
  end
381
389
  end
382
390
  end
383
391
 
384
392
  def records_to_h()
385
- i = XPath.match(@doc.root, 'records/*/attribute::id').max_by(&:value).to_s.to_i
393
+
394
+ i = @doc.xpath('max(records/*/attribute::id)') || 0
386
395
 
387
- ah = XPath.match(@doc.root, 'records/*').map do |row|
388
-
396
+ @doc.xpath('records/*').inject({}) do |result,row|
397
+
389
398
  created = Time.now.to_s
390
399
  last_modified = ''
391
400
 
392
- if row.attribute('id') then
393
- id = row.attribute('id').value.to_s
401
+ if row.attributes['id'] then
402
+ id = row.attributes['id']
394
403
  else
395
404
  i += 1; id = i.to_s
396
405
  end
397
- created = row.attribute('created').value.to_s if row.attribute('created')
398
- last_modified = row.attribute('last_modified').value.to_s if row.attribute('last_modified')
399
- body = XPath.match(row, '*').inject({}) do |r,node|
400
- r[node.name.to_s.to_sym] = node.text.to_s
401
- r
406
+ created = row.attributes['created'] if row.attributes['created']
407
+ last_modified = row.attributes['last_modified'] if row.attributes['last_modified']
408
+ body = row.xpath('*').inject({}) do |r,node|
409
+ r.merge node.name.to_s => node.text
402
410
  end
403
411
 
404
- [body[@default_key],{id: id, created: created, last_modified: \
405
- last_modified, body: body}]
412
+ result.merge body[@default_key] => {id: id, created: created, last_modified: last_modified, body: body}
406
413
  end
407
- Hash[*ah.flatten]
414
+
408
415
  end
409
416
 
410
417
  def summary_to_h
411
- XPath.match(@doc.root, 'summary/*').inject({}) do |r,node|
412
- r[node.name.to_s.to_sym] = node.text.to_s
413
- r
418
+
419
+ @doc.xpath('summary/*').inject({}) do |r,node|
420
+ r.merge node.name.to_s.to_sym => node.text.to_s
414
421
  end
415
422
  end
416
423
 
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: dynarex
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.9.4
4
+ version: 0.9.5
5
5
  platform: ruby
6
6
  authors: []
7
7
 
@@ -9,7 +9,7 @@ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
11
 
12
- date: 2010-11-23 00:00:00 +00:00
12
+ date: 2010-12-07 00:00:00 +00:00
13
13
  default_executable:
14
14
  dependencies: []
15
15