format_engine 0.2.0 → 0.3.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 6a255811c1f0df1bc9b92fc02eaaee55b58b2f73
4
- data.tar.gz: 10705de5b1a6bf5722cbf4d8350cce41f10a08c1
3
+ metadata.gz: b0baee6898a0254f3c7e17992bdb405855786cde
4
+ data.tar.gz: e4ed97834beefe97848e7c45e9a8bbda13918b4d
5
5
  SHA512:
6
- metadata.gz: b61ac28dd23cd0cb2c3a58f6ba6cf80b79d883933a0906f24de50c173019d718e49c89a807a0f476d444970dd407e9a2626e8b115bf47363adf9d77ba4db025d
7
- data.tar.gz: e76b17e94d5e83e26e0c99c2d4e43b82e1c00ec5bd49de5c490c6c06f149bbee446fffecee2d9564d806176e9e269b7400269873c626b7c0a350af4f6829e147
6
+ metadata.gz: 81baf16535d34920bfc19c3be71fe479fc728db0e0f07c9926b32e97c088a2072c241ea131e144d14ab19e2b8699875b0b50afedb82c1964e42f59ec73ea40f2
7
+ data.tar.gz: 2721ee739f1a909e8307612f55ff84a916b103b804b6108e2e89d612d191600ecd6f48a9766b806c83814b4b32e69baa5e7d3b4fe32d6610ae71d98feb789a0c
data/README.md CHANGED
@@ -105,7 +105,8 @@ creates the following format specification array:
105
105
  Variable("%S", ["-5", "2"]),
106
106
  Literal("!")]
107
107
  ```
108
- Where literals are processed as themselves and variables are executed by looking
108
+ Where literals are processed as themselves. If that literal ends with a space,
109
+ that space will match zero or more spaces. Variables are executed by looking
109
110
  up the format string in the library and executing the corresponding block.
110
111
 
111
112
  **Note:** If a format string does not correspond to an entry in the library,
@@ -145,14 +146,15 @@ Methods
145
146
  * found - The text found by the last parse (or parse!) operation.
146
147
 
147
148
  ###Format Specifier Attributes
148
- The format specifier (accessed as fmt above) has the following attributes:
149
+ The format specifier, used in both formatting and parsing and accessed as the
150
+ fmt attribute, has itself, the following attributes:
149
151
  * has_width? - Was a width specified?
150
152
  * width - The width parameter or 0 if not specified.
151
153
  * width_str - The actual width text or an empty string.
152
154
  * has_prec? - Was a precision specified?
153
155
  * prec - The precision parameter or 0 if not specified.
154
156
  * prec_str - The actual precision text or an empty string.
155
- * parm_str - The actual parameter text or an empty string.
157
+ * parm_str - The actual parameter (width and precision) text or an empty string.
156
158
 
157
159
  ## Philosophy
158
160
 
@@ -19,6 +19,7 @@ module FormatEngine
19
19
  def attr_formatter(method, library)
20
20
  engine = Engine.new(library)
21
21
 
22
+ #Create an instance method to do the formatting.
22
23
  define_method(method) do |spec_str|
23
24
  engine.do_format(self, spec_str)
24
25
  end
@@ -20,6 +20,7 @@ module FormatEngine
20
20
  def attr_parser(method, library)
21
21
  engine = Engine.new(library)
22
22
 
23
+ #Create a class method to do the parsing.
23
24
  define_singleton_method(method) do |src, spec_str|
24
25
  engine.do_parse(src, self, spec_str)
25
26
  end
@@ -9,6 +9,8 @@ module FormatEngine
9
9
  # Set up a literal format specification.
10
10
  def initialize(literal)
11
11
  @literal = literal
12
+ @head = literal.rstrip
13
+ @tail = literal.end_with?(' ')
12
14
  end
13
15
 
14
16
  # Is this literal supported by the engine? YES!
@@ -23,7 +25,8 @@ module FormatEngine
23
25
 
24
26
  # Parse from the input string
25
27
  def do_parse(spec_info)
26
- spec_info.parse!(literal)
28
+ spec_info.parse!(@head) unless @head.empty?
29
+ spec_info.parse(/\s*/) if @tail
27
30
  end
28
31
 
29
32
  # Inspect for debugging.
@@ -1,5 +1,5 @@
1
1
 
2
2
  module FormatEngine
3
3
  # The version of the format_engine gem.
4
- VERSION = "0.2.0"
4
+ VERSION = "0.3.0"
5
5
  end
@@ -19,8 +19,6 @@ class ParserTester < Minitest::Test
19
19
  "%l" => lambda { tmp[:ln] = found if parse(/(\w)+/ ) },
20
20
  "%L" => lambda { tmp[:ln] = found.upcase if parse(/(\w)+/) },
21
21
  "%-L" => lambda { tmp[:ln] = found.capitalize if parse(/(\w)+/) },
22
- "%s" => lambda { parse(/\s+/) },
23
- "%,s" => lambda { parse(/[,\s]\s*/) },
24
22
  "%t" => lambda { parse("\t") },
25
23
  "%!t" => lambda { parse!("\t") },
26
24
 
@@ -59,12 +57,28 @@ class ParserTester < Minitest::Test
59
57
 
60
58
  def test_that_it_can_flex_parse
61
59
  engine = make_parser
62
- spec = "%f%,s%l"
60
+ spec = "%f, %l"
61
+
62
+ #No spaces.
63
+ result = engine.do_parse("Squidly,Jones", TestPerson, spec)
64
+
65
+ assert_equal(TestPerson, result.class)
66
+ assert_equal("Squidly", result.first_name)
67
+ assert_equal("Jones", result.last_name)
68
+
69
+ #One space.
63
70
  result = engine.do_parse("Squidly, Jones", TestPerson, spec)
64
71
 
65
72
  assert_equal(TestPerson, result.class)
66
73
  assert_equal("Squidly", result.first_name)
67
74
  assert_equal("Jones", result.last_name)
75
+
76
+ #Two spaces.
77
+ result = engine.do_parse("Squidly, Jones", TestPerson, spec)
78
+
79
+ assert_equal(TestPerson, result.class)
80
+ assert_equal("Squidly", result.first_name)
81
+ assert_equal("Jones", result.last_name)
68
82
  end
69
83
 
70
84
  def test_that_it_can_tab_parse
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: format_engine
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.0
4
+ version: 0.3.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Peter Camilleri
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2015-07-17 00:00:00.000000000 Z
11
+ date: 2016-01-30 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler