rxraw-lineparser 0.1.0 → 0.1.1

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/rxraw-lineparser.rb +33 -7
  2. metadata +2 -2
@@ -1,14 +1,38 @@
1
- #�!/usr/bin/env ruby
1
+ #!/usr/bin/env ruby
2
2
 
3
3
  # file: rxraw-lineparser.rb
4
4
 
5
- class RXRawLineParser
5
+ module Enumerable
6
+ def repeated_permutation(size, &blk)
7
+ f = proc do |memo, &blk|
8
+ if memo.size == size
9
+ blk.call memo
10
+ else
11
+ self.each do |i|
12
+ f.call memo + [i], &blk
13
+ end
14
+ end
15
+ end
16
+
17
+ if block_given?
18
+ f.call [], &blk
19
+ else
20
+ Enumerator.new(f, :call, [])
21
+ end
22
+ end
23
+ end
6
24
 
7
- def self.parse(format_mask, line)
25
+ class RXRawLineParser
26
+
27
+ def initialize(format_mask)
28
+ @format_mask = format_mask
29
+ end
30
+
31
+ def parse(line)
8
32
 
9
- field_names = format_mask.to_s.scan(/\[!(\w+)\]/).flatten.map(&:to_sym)
33
+ field_names = @format_mask.to_s.scan(/\[!(\w+)\]/).flatten.map(&:to_sym)
10
34
 
11
- patterns = possible_patterns(format_mask)
35
+ patterns = possible_patterns(@format_mask)
12
36
  pattern = patterns.detect {|x| line.match(/#{x.join}/)}.join
13
37
  field_values = line.match(/#{pattern}/).captures
14
38
 
@@ -23,6 +47,8 @@ class RXRawLineParser
23
47
  [field_names, field_values]
24
48
  end
25
49
 
50
+ private
51
+
26
52
  def possible_patterns(format_mask)
27
53
 
28
54
  tot_fields = format_mask.scan(/\[!\w+\]/).length
@@ -54,10 +80,10 @@ class RXRawLineParser
54
80
  end
55
81
 
56
82
  count = 2**main_fields
57
- rr2 = rr.take(count).map {|x| x + [a[-1] + '(.*)']}
83
+ rr2 = rr.take(count+1).map {|x| x + [a[-1] + '(.*)']}
58
84
  wild_r = rr2.slice!(-1)
59
85
 
60
- rrr = rr2 + rr[0..count-2] + [wild_r] + rr[count-1..-1]
86
+ rrr = rr2 + rr[0..count-1] + [wild_r] + rr[count..-1]
61
87
 
62
88
  end
63
89
 
metadata CHANGED
@@ -2,7 +2,7 @@
2
2
  name: rxraw-lineparser
3
3
  version: !ruby/object:Gem::Version
4
4
  prerelease:
5
- version: 0.1.0
5
+ version: 0.1.1
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: 2012-04-18 00:00:00 +01:00
13
+ date: 2012-04-19 00:00:00 +01:00
14
14
  default_executable:
15
15
  dependencies: []
16
16