polyrex 0.8.20 → 0.8.21

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/polyrex.rb +18 -94
  2. metadata +3 -5
@@ -1,4 +1,4 @@
1
- #/usr/bin/env ruby
1
+ #!/usr/bin/env ruby
2
2
 
3
3
  # file: polyrex.rb
4
4
 
@@ -10,6 +10,7 @@ require 'polyrex-object-methods'
10
10
  require 'recordx-xslt'
11
11
  require 'rexle'
12
12
  require 'recordx'
13
+ require 'rxraw-lineparser'
13
14
 
14
15
  module Enumerable
15
16
  def repeated_permutation(size, &blk)
@@ -60,6 +61,12 @@ class Polyrex
60
61
  def delete(id=nil)
61
62
  @doc.root.delete("//[@id='#{id}'")
62
63
  end
64
+
65
+ def delimiter=(separator)
66
+ @format_masks.map! do |format_mask|
67
+ format_mask.to_s.gsub(/\s/, separator)
68
+ end
69
+ end
63
70
 
64
71
  def record()
65
72
  @parent_node
@@ -199,11 +206,11 @@ class Polyrex
199
206
  def string_parse(buffer)
200
207
 
201
208
  raw_header = buffer.slice!(/<\?polyrex[^>]+>/)
202
-
209
+
203
210
  if raw_header then
204
211
  header = raw_header[/<?polyrex (.*)?>/,1]
205
- header.scan(/\w+\="[^"]+\"/).map{|x| r = x.split(/=/); \
206
- [(r[0] + "=").to_sym, r[1][/^"(.*)"$/,1]] }.each do |name, value|
212
+ header.scan(/\w+\=["'][^"']+["']/).map{|x| r = x.split(/=/); \
213
+ [(r[0] + "=").to_sym, r[1][/^["'](.*)["']$/,1]] }.each do |name, value|
207
214
  self.method(name).call(value)
208
215
  end
209
216
  end
@@ -252,29 +259,11 @@ class Polyrex
252
259
  line = x.shift
253
260
 
254
261
  unless @format_masks[i][/^\(.*\)$/] then
255
-
256
- @field_names = @format_masks[i].to_s.scan(/\[!(\w+)\]/).flatten.map(&:to_sym)
257
-
258
- =begin
259
- t = regexify_fmask(@format_masks[i]) #.sub(/\[/,'\[').sub(/\]/,'\]')
260
- a = t.reverse.split(/(?=\)[^\(]+\()/).reverse.map &:reverse
261
- patterns = tail_map(a)
262
- =end
263
-
264
- patterns = possible_patterns(@format_masks[i])
265
- pattern = patterns.detect {|x| line.match(/#{x.join}/)}.join
266
- field_values = line.match(/#{pattern}/).captures
267
-
268
-
269
- found_quotes = find_qpattern(pattern)
270
-
271
- if found_quotes then
272
- found_quotes.each {|i| field_values[i] = field_values[i][1..-2]}
273
- end
274
-
275
- field_values += [''] * (@field_names.length - field_values.length)
262
+
263
+ @field_names, field_values = RXRawLineParser.new(format_masks[i]).parse(line)
264
+
276
265
  else
277
-
266
+
278
267
  format_masks = @format_masks[i][1..-2].split('|')
279
268
  patterns = format_masks.map do |x|
280
269
  regexify_fmask(x) #.sub(/\[/,'\[').sub(/\]/,'\]')
@@ -286,7 +275,7 @@ class Polyrex
286
275
  @field_names = format_masks[i].to_s.scan(/\[!(\w+)\]/).flatten.map(&:to_sym)
287
276
 
288
277
  field_values = line.match(/#{pattern}/).captures
289
-
278
+
290
279
  end
291
280
 
292
281
  @id_counter.succ!
@@ -375,37 +364,10 @@ class Polyrex
375
364
  @parent_node = @doc.root.element('records')
376
365
 
377
366
  end
378
-
379
- def diminishing_permutation(max_fields)
380
- result = max_fields.times.inject([]) do |r,i|
381
- r + [1,0].repeated_permutation(max_fields-i).to_a
382
- end
383
- end
384
-
385
- def find_qpattern(s)
386
- s.split(/(?=\([^\)]+\))/).map.with_index\
387
- .select{|x,i| x[/\["'\]\[\^"'\]\+\["'\]/] }.map(&:last)
388
- end
389
-
390
- def fmask_delimiters(f)
391
- a = f.split(/(?=\[!\w+\])/)[0..-2].map do |x|
392
-
393
- aa = x.split(/(?=[^\]]+$)/)
394
-
395
- if aa.length == 2 and aa.first[/\[!\w+\]/] then
396
- field, delimiter = *aa
397
- delimiter ||= '$'
398
- d = delimiter[0]
399
- d
400
- end
401
- end
402
- end
403
-
404
-
405
- # jr140412 to be removed
367
+
406
368
  def regexify_fmask(f)
407
369
 
408
- a = f.split(/(?=\[!\w+\])/).i do |x|
370
+ a = f.split(/(?=\[!\w+\])/).map do |x|
409
371
 
410
372
  aa = x.split(/(?=[^\]]+$)/)
411
373
 
@@ -422,48 +384,10 @@ class Polyrex
422
384
  a.join
423
385
  end
424
386
 
425
- #jr140412 to be removed
426
387
  def tail_map(a)
427
388
  [a] + (a.length > 1 ? tail_map(a[0..-2]) : [])
428
389
  end
429
390
 
430
- def possible_patterns(format_mask)
431
-
432
- tot_fields = format_mask.scan(/\[!\w+\]/).length
433
- return [['(.*)']] if tot_fields <= 1
434
- main_fields = tot_fields - 1
435
- qpattern = %q{(["'][^"']+["'])}
436
-
437
- a = fmask_delimiters(format_mask)
438
- r = diminishing_permutation(main_fields)
439
-
440
- if r.length > 2 then
441
- itemx = [r.slice!(-2)]
442
- r2 = r[0..-3] + itemx + r[-2..-1]
443
- else
444
- r2 = r
445
- end
446
-
447
- rr = r2.map do |x|
448
- x.each_with_index.map do |item, i|
449
- d = a[i]
450
- case item
451
- when 1
452
- i > 0 ? d + qpattern : qpattern
453
- when 0
454
- s = "([^%s]+)" % d
455
- i > 0 ? d + s : s
456
- end
457
- end
458
- end
459
-
460
- count = 2**main_fields
461
- rr2 = rr.take(count).map {|x| x + [a[-1] + '(.*)']}
462
- wild_r = rr2.slice!(-1)
463
-
464
- rrr = rr2 + rr[0..count-2] + [wild_r] + rr[count-1..-1]
465
-
466
- end
467
391
 
468
392
  def load_find_by(schema)
469
393
  a = PolyrexObjectMethods.new(schema).to_a
metadata CHANGED
@@ -2,7 +2,7 @@
2
2
  name: polyrex
3
3
  version: !ruby/object:Gem::Version
4
4
  prerelease:
5
- version: 0.8.20
5
+ version: 0.8.21
6
6
  platform: ruby
7
7
  authors:
8
8
  - James Robertson
@@ -10,8 +10,7 @@ autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
12
 
13
- date: 2012-04-16 00:00:00 +01:00
14
- default_executable:
13
+ date: 2012-05-24 00:00:00 Z
15
14
  dependencies:
16
15
  - !ruby/object:Gem::Dependency
17
16
  name: polyrex-schema
@@ -89,7 +88,6 @@ extra_rdoc_files: []
89
88
 
90
89
  files:
91
90
  - lib/polyrex.rb
92
- has_rdoc: true
93
91
  homepage:
94
92
  licenses: []
95
93
 
@@ -113,7 +111,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
113
111
  requirements: []
114
112
 
115
113
  rubyforge_project:
116
- rubygems_version: 1.5.2
114
+ rubygems_version: 1.8.23
117
115
  signing_key:
118
116
  specification_version: 3
119
117
  summary: polyrex