dynarex 1.0.28 → 1.1.0

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 +44 -33
  2. metadata +13 -2
@@ -1,8 +1,7 @@
1
- #!/usr/bin/ruby
1
+ #!/usr/bin/env ruby
2
2
 
3
3
  # file: dynarex.rb
4
4
 
5
- require 'nokogiri'
6
5
  require 'open-uri'
7
6
  require 'ostruct'
8
7
  require 'dynarex-import'
@@ -106,9 +105,9 @@ EOF
106
105
  xslt_format = format_mask.to_s.gsub(/\s(?=\[!\w+\])/,'<xsl:text> </xsl:text>').gsub(/\[!(\w+)\]/, '<xsl:value-of select="\1"/>')
107
106
  xsl_buffer.sub!(/\[!regex_values\]/, xslt_format)
108
107
 
109
- xslt = Nokogiri::XSLT(xsl_buffer)
110
- out = xslt.transform(Nokogiri::XML(@doc.to_s))
111
- out.text
108
+ # jr 211111 the following statement has not yet been tested.
109
+ Rexslt.new(xsl_buffer, @doc.to_s).to_s
110
+ ""
112
111
 
113
112
  end
114
113
 
@@ -194,8 +193,8 @@ EOF
194
193
 
195
194
  def element(x)
196
195
  @doc.element x
197
- end
198
-
196
+ end
197
+
199
198
  def sort_by!(&element_blk)
200
199
  refresh_doc
201
200
  a = @doc.xpath('records/*').sort_by &element_blk
@@ -207,7 +206,7 @@ EOF
207
206
 
208
207
  load_records
209
208
  self
210
- end
209
+ end
211
210
 
212
211
  def record(id)
213
212
  recordx_to_record @doc.element("records/*[@id='#{id}']")
@@ -250,6 +249,7 @@ EOF
250
249
  params = Hash[raw_params.keys.map(&:to_sym).zip(raw_params.values)]
251
250
 
252
251
  fields = capture_fields(params)
252
+
253
253
  record = Rexle::Element.new @record_name
254
254
  fields.each do |k,v|
255
255
  element = Rexle::Element.new(k.to_s)
@@ -257,7 +257,6 @@ EOF
257
257
  record.add element
258
258
  end
259
259
 
260
-
261
260
  id = (@doc.xpath('max(records/*/attribute::id)') || '0').succ unless id
262
261
 
263
262
  attributes = {id: id, created: Time.now.to_s, last_modified: nil}
@@ -274,9 +273,9 @@ EOF
274
273
  end
275
274
 
276
275
 
277
- def display_xml
276
+ def display_xml(opt={})
278
277
  rebuild_doc()
279
- @doc.xml #jr230711 pretty: true
278
+ @doc.xml(opt) #jr230711 pretty: true
280
279
  end
281
280
 
282
281
  def rebuild_doc
@@ -290,7 +289,7 @@ EOF
290
289
  xml.records do
291
290
 
292
291
  @records.each do |k, item|
293
- p 'foo ' + item.inspect
292
+ #p 'foo ' + item.inspect
294
293
  xml.send(@record_name, {id: item[:id], created: item[:created], \
295
294
  last_modified: item[:last_modified]}, '') do
296
295
  item[:body].each{|name,value| xml.send name, value}
@@ -298,6 +297,8 @@ EOF
298
297
  end
299
298
 
300
299
  end
300
+ else
301
+ xml.records
301
302
  end # end of if @records
302
303
  end
303
304
 
@@ -369,26 +370,33 @@ EOF
369
370
  def dynarex_new(s)
370
371
  @schema = s
371
372
  ptrn = %r((\w+)\[?([^\]]+)?\]?\/(\w+)\(([^\)]+)\))
372
- root_name, raw_summary, record_name, raw_fields = s.match(ptrn).captures
373
- summary, fields = [raw_summary || '',raw_fields].map {|x| x.split(/,/).map &:strip}
374
- create_find fields
375
-
376
- reserved = %w(require parent)
377
- raise 'reserved keyword' if (fields & reserved).any?
378
-
379
- lines =<<LINES
380
- #{root_name}
381
- summary#{"\n" + ' ' * 4 + summary.join("\n" + ' ' * 4) unless summary.empty?}
382
- recordx_type dynarex
383
- format_mask #{fields.map {|x| "[!%s]" % x}.join(' ')}
384
- schema #{s}
385
- records
386
- LINES
387
373
 
374
+ if s.match(ptrn) then
375
+ @root_name, raw_summary, record_name, raw_fields = s.match(ptrn).captures
376
+ summary, fields = [raw_summary || '',raw_fields].map {|x| x.split(/,/).map &:strip}
377
+ create_find fields
378
+
379
+ reserved = %w(require parent)
380
+ raise 'reserved keyword' if (fields & reserved).any?
381
+
382
+ else
383
+ ptrn = %r((\w+)\[?([^\]]+)?\]?)
384
+ @root_name, raw_summary = s.match(ptrn).captures
385
+ summary = raw_summary.split(/,/).map &:strip
386
+
387
+ end
388
+
389
+
390
+ format_mask = fields ? fields.map {|x| "[!%s]" % x}.join(' ') : ''
391
+
392
+ @summary = Hash[summary.zip([''] * summary.length).flatten.each_slice(2)\
393
+ .map{|x1,x2| [x1.to_sym,x2]}]
394
+ @summary.merge!({recordx_type: 'dynarex', format_mask: format_mask, schema: s})
388
395
  @records = {}
389
396
  @flat_records = {}
390
397
 
391
- LineTree.new(lines).to_xml
398
+ rebuild_doc
399
+
392
400
  end
393
401
 
394
402
  def attach_record_methods()
@@ -399,8 +407,8 @@ LINES
399
407
 
400
408
  if s[/</] then # xml
401
409
  buffer = s
402
- elsif s[/\(/] # schema
403
- buffer = dynarex_new s
410
+ elsif s[/[\[\(]/] # schema
411
+ dynarex_new(s)
404
412
  elsif s[/^https?:\/\//] then # url
405
413
  buffer = Kernel.open(s, 'UserAgent' => 'Dynarex-Reader').read
406
414
  else # local file
@@ -408,18 +416,21 @@ LINES
408
416
  buffer = File.open(s,'r').read
409
417
  end
410
418
 
411
- @doc = Rexle.new buffer
419
+
420
+ @doc = Rexle.new(buffer) unless @doc
421
+
412
422
  @schema = @doc.root.text('summary/schema')
413
423
  @root_name = @doc.root.name
424
+
414
425
  @summary = summary_to_h
415
426
  @default_key = @doc.element('summary/default_key/text()')
416
427
  @format_mask = @doc.element('summary/format_mask/text()')
417
428
 
418
429
  @fields = @format_mask.to_s.scan(/\[!(\w+)\]/).flatten.map(&:to_sym) if @format_mask
419
430
 
420
- if @schema then
431
+ if @schema and @schema.match(/(\w+)\(([^\)]+)/) then
421
432
  @record_name, raw_fields = @schema.match(/(\w+)\(([^\)]+)/).captures
422
- @fields = raw_fields.split(',').map{|x| x.strip.to_sym} unless @fields
433
+ @fields = raw_fields.split(',').map{|x| x.strip.to_sym} unless @fields
423
434
  end
424
435
 
425
436
  if @fields then
metadata CHANGED
@@ -2,7 +2,7 @@
2
2
  name: dynarex
3
3
  version: !ruby/object:Gem::Version
4
4
  prerelease:
5
- version: 1.0.28
5
+ version: 1.1.0
6
6
  platform: ruby
7
7
  authors:
8
8
  - James Robertson
@@ -10,7 +10,7 @@ autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
12
 
13
- date: 2011-07-24 00:00:00 +01:00
13
+ date: 2011-11-21 00:00:00 +00:00
14
14
  default_executable:
15
15
  dependencies:
16
16
  - !ruby/object:Gem::Dependency
@@ -57,6 +57,17 @@ dependencies:
57
57
  version: "0"
58
58
  type: :runtime
59
59
  version_requirements: *id004
60
+ - !ruby/object:Gem::Dependency
61
+ name: rexslt
62
+ prerelease: false
63
+ requirement: &id005 !ruby/object:Gem::Requirement
64
+ none: false
65
+ requirements:
66
+ - - ">="
67
+ - !ruby/object:Gem::Version
68
+ version: "0"
69
+ type: :runtime
70
+ version_requirements: *id005
60
71
  description:
61
72
  email:
62
73
  executables: []