microparser 0.0.1 → 0.0.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
data/README.txt CHANGED
@@ -1,6 +1,6 @@
1
1
  = microparser
2
2
 
3
- * http://microparser.rubyforge.org
3
+ * http://microparser.rubyforge.org/
4
4
 
5
5
  == DESCRIPTION:
6
6
 
@@ -11,11 +11,9 @@ A Regexp encapsulation class. See the synopsis for a more detailed explanation.
11
11
  //To parse a basic timestamp "2008-11-04"
12
12
 
13
13
  parser = MicroParser.new(/([0-9]{4})-([0-9]{2})-([0-9]{2}))/i) do
14
-
15
14
  mapping(:year,1)
16
15
  mapping(:month,2)
17
16
  mapping(:day,3)
18
-
19
17
  end
20
18
 
21
19
  parser.load("2008-11-04")
@@ -26,7 +24,7 @@ A Regexp encapsulation class. See the synopsis for a more detailed explanation.
26
24
 
27
25
  == INSTALL:
28
26
 
29
- * sudo gem install microparser
27
+ * sudo gem install
30
28
 
31
29
  == LICENSE:
32
30
 
@@ -82,7 +82,7 @@ module Microparser
82
82
 
83
83
  raise Errors::EmptyDataError.new("The parser must be provided data before attempting to access attributes!") if @data.nil?
84
84
 
85
- parser[@maps[meth]]
85
+ construct_mapping_value(meth)
86
86
  else
87
87
  super(method,*args,&block)
88
88
  end
@@ -91,6 +91,52 @@ module Microparser
91
91
 
92
92
  private
93
93
 
94
+ def construct_mapping_value(method)
95
+
96
+ begin
97
+
98
+ sig = @maps[method]
99
+
100
+ if sig.is_a?(Fixnum)
101
+ retrieve_fixnum_mapping_value(sig)
102
+
103
+ elsif sig.is_a?(Array)
104
+ retrieve_array_mapping_value(sig)
105
+
106
+ else
107
+ retrieve_hash_mapping_value(sig)
108
+
109
+ end
110
+
111
+ rescue
112
+
113
+ raise Errors::UnknownMappingTypeError.new("Unable to generate mapping value")
114
+
115
+ end
116
+
117
+ end
118
+
119
+ def retrieve_fixnum_mapping_value(f)
120
+ parser[f]
121
+ end
122
+
123
+ def retrieve_array_mapping_value(a)
124
+ a.map do |n|
125
+ retrieve_fixnum_mapping_value(n)
126
+ end
127
+ end
128
+
129
+ def retrieve_hash_mapping_value(h)
130
+
131
+ str = h[:template]
132
+ var = h[:values]
133
+
134
+ val = retrieve_array_mapping_value(var)
135
+
136
+ str % val
137
+
138
+ end
139
+
94
140
  def parser #:nodoc:
95
141
  if @parsed.nil?
96
142
  @parsed = @data.match(@pattern)
@@ -8,6 +8,8 @@ module Microparser
8
8
 
9
9
  class EmptyDataError < StandardError; end
10
10
 
11
+ class UnknownMappingTypeError < StandardError; end
12
+
11
13
  end
12
14
 
13
15
  end
@@ -2,7 +2,7 @@ module Microparser
2
2
  module VERSION #:nodoc:
3
3
  MAJOR = 0
4
4
  MINOR = 0
5
- TINY = 1
5
+ TINY = 2
6
6
 
7
7
  STRING = [MAJOR, MINOR, TINY].join('.')
8
8
  self
@@ -7,6 +7,8 @@ class TestMicroparser < Test::Unit::TestCase
7
7
  mapping(:year,1)
8
8
  mapping(:month,2)
9
9
  mapping(:day,3)
10
+ mapping(:day_month,[3,2])
11
+ mapping(:formatted_date,{:template => "%02d/%02d/%02d", :values => [3,2,1]})
10
12
  end
11
13
 
12
14
  @parser.load("2008-11-04")
@@ -27,7 +29,7 @@ class TestMicroparser < Test::Unit::TestCase
27
29
  end
28
30
 
29
31
  def test_mappings
30
- assert_equal([:day,:month,:year], @parser.maps)
32
+ assert_equal([:day,:day_month,:formatted_date,:month,:year], @parser.maps)
31
33
  end
32
34
 
33
35
  def test_match
@@ -65,4 +67,12 @@ class TestMicroparser < Test::Unit::TestCase
65
67
  }
66
68
  end
67
69
 
70
+ def test_array_mapping
71
+ assert_equal(["04","11"],@parser.day_month)
72
+ end
73
+
74
+ def test_hash_mapping
75
+ assert_equal("04/11/2008",@parser.formatted_date)
76
+ end
77
+
68
78
  end
data/website/index.html CHANGED
@@ -33,14 +33,14 @@
33
33
  <h1>microparser</h1>
34
34
  <div id="version" class="clickable" onclick='document.location = "http://rubyforge.org/projects/microparser"; return false'>
35
35
  <p>Get Version</p>
36
- <a href="http://rubyforge.org/projects/microparser" class="numbers">0.0.1</a>
36
+ <a href="http://rubyforge.org/projects/microparser" class="numbers">0.0.2</a>
37
37
  </div>
38
38
  <h2>What</h2>
39
- <p>Microparser is a simple Regexp encapsulation class.<br />
40
- <br />
41
- Using a series of mappings between attribute names and regexp subgroup offsets, you can create basic proxy objects to provide a uniform interface for accessing the various components of strings without knowing the intimate details of the pattern used to parse them.<br />
42
- <br />
43
- Ok, that&#8217;s a really bad explanation. Have a look at the examples.</p>
39
+ Microparser is a simple Regexp encapsulation class.
40
+
41
+ Using a series of mappings between attribute names and regexp subgroup offsets, you can create basic proxy objects to provide a uniform interface for accessing the various components of strings without knowing the intimate details of the pattern used to parse them.
42
+
43
+ Ok, that&#8217;s a really bad explanation. Have a look at the examples.
44
44
  <h2>Installing</h2>
45
45
  <p><pre class='syntax'><span class="ident">sudo</span> <span class="ident">gem</span> <span class="ident">install</span> <span class="ident">microparser</span></pre></p>
46
46
  <h2>Demonstration of usage</h2>
@@ -50,6 +50,8 @@ parser_a = MicroParser.new(/([0-9]{4})-([0-9]{2})-([0-9]{2}))/i) do
50
50
  mapping(:year,1)
51
51
  mapping(:month,2)
52
52
  mapping(:day,3)
53
+ mapping(:day_month,[3,2])
54
+ mapping(:formatted_date,{:template =&gt; "%02d/%02d/%02d", :values =&gt; [3,2,1]})
53
55
  end
54
56
 
55
57
  parser_a.load("2008-11-04")
@@ -58,12 +60,15 @@ parser_a.year =&gt; "2008"
58
60
  parser_a.month =&gt; "11"
59
61
  parser_a.day =&gt; "04"
60
62
 
63
+ parser_a.day_month =&gt; ["04","11"]
64
+ parser_a.formatted_date =&gt; "04/11/2008"
65
+
61
66
  //To parse a different basic timestamp "2008-04-11" (i.e. year-day-month)
62
67
 
63
68
  parser_b = MicroParser.new(/([0-9]{4})-([0-9]{2})-([0-9]{2}))/i) do
64
69
  mapping(:year,1)
65
70
 
66
- // Notice the swapped offsets in the two mappings below
71
+ // Notice the swapped offsets in the two mappings below
67
72
 
68
73
  mapping(:month,3)
69
74
  mapping(:day,2)
@@ -79,15 +84,23 @@ parser_b.day =&gt; "04"
79
84
  // interface yet the underlying data is in a different format.
80
85
 
81
86
  </pre>
87
+ <h2>Getting the source</h2>
88
+ <p>You can fetch the source from:</p>
89
+ <ul>
90
+ <li>rubyforge: <a href="http://rubyforge.org/scm/?group_id=7262">http://rubyforge.org/scm/?group_id=7262</a></li>
91
+ </ul>
92
+ <h3>Build and test instructions</h3>
93
+ <pre>cd microparser
94
+ rake test
95
+ rake install_gem</pre>
82
96
  <h2>Limits</h2>
83
97
  <ul>
84
- <li>microparser only supports basic mapping (i.e. no way of accessing nested subgroups)</li>
85
98
  <li>There is no offset validation (i.e. it is possible to create a mapping to an inexistent offset).</li>
86
99
  </ul>
87
100
  <h2>License</h2>
88
101
  <p>This code is free to use under the terms of the <span class="caps">MIT</span> license.</p>
89
102
  <p class="coda">
90
- <a href="mailto:douglas.willcocks@gmail.com">Douglas Willcocks</a>, 4th November 2008<br>
103
+ <a href="mailto:douglas.willcocks@gmail.com">Douglas Willcocks</a>, 28th September 2009<br>
91
104
  Theme extended from <a href="http://rb2js.rubyforge.org/">Paul Battley</a>
92
105
  </p>
93
106
  </div>
data/website/index.txt CHANGED
@@ -2,11 +2,11 @@ h1. microparser
2
2
 
3
3
  h2. What
4
4
 
5
- Microparser is a simple Regexp encapsulation class.
6
-
7
- Using a series of mappings between attribute names and regexp subgroup offsets, you can create basic proxy objects to provide a uniform interface for accessing the various components of strings without knowing the intimate details of the pattern used to parse them.
8
-
9
- Ok, that's a really bad explanation. Have a look at the examples.
5
+ Microparser is a simple Regexp encapsulation class.
6
+
7
+ Using a series of mappings between attribute names and regexp subgroup offsets, you can create basic proxy objects to provide a uniform interface for accessing the various components of strings without knowing the intimate details of the pattern used to parse them.
8
+
9
+ Ok, that's a really bad explanation. Have a look at the examples.
10
10
 
11
11
  h2. Installing
12
12
 
@@ -20,6 +20,8 @@ parser_a = MicroParser.new(/([0-9]{4})-([0-9]{2})-([0-9]{2}))/i) do
20
20
  mapping(:year,1)
21
21
  mapping(:month,2)
22
22
  mapping(:day,3)
23
+ mapping(:day_month,[3,2])
24
+ mapping(:formatted_date,{:template => "%02d/%02d/%02d", :values => [3,2,1]})
23
25
  end
24
26
 
25
27
  parser_a.load("2008-11-04")
@@ -28,12 +30,15 @@ parser_a.year => "2008"
28
30
  parser_a.month => "11"
29
31
  parser_a.day => "04"
30
32
 
33
+ parser_a.day_month => ["04","11"]
34
+ parser_a.formatted_date => "04/11/2008"
35
+
31
36
  //To parse a different basic timestamp "2008-04-11" (i.e. year-day-month)
32
37
 
33
38
  parser_b = MicroParser.new(/([0-9]{4})-([0-9]{2})-([0-9]{2}))/i) do
34
39
  mapping(:year,1)
35
40
 
36
- // Notice the swapped offsets in the two mappings below
41
+ // Notice the swapped offsets in the two mappings below
37
42
 
38
43
  mapping(:month,3)
39
44
  mapping(:day,2)
@@ -68,7 +73,6 @@ rake install_gem</pre>
68
73
 
69
74
  h2. Limits
70
75
 
71
- * microparser only supports basic mapping (i.e. no way of accessing nested subgroups)
72
76
  * There is no offset validation (i.e. it is possible to create a mapping to an inexistent offset).
73
77
 
74
78
 
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: microparser
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1
4
+ version: 0.0.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Douglas Willcocks
@@ -9,7 +9,7 @@ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
11
 
12
- date: 2008-11-06 00:00:00 +00:00
12
+ date: 2009-09-28 00:00:00 +01:00
13
13
  default_executable:
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency
@@ -20,7 +20,7 @@ dependencies:
20
20
  requirements:
21
21
  - - ">="
22
22
  - !ruby/object:Gem::Version
23
- version: 1.8.0
23
+ version: 1.8.3
24
24
  version:
25
25
  description: Regexp encapsulation layer
26
26
  email: