format_engine 0.1.0 → 0.1.1
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.
- checksums.yaml +4 -4
- data/README.md +6 -1
- data/lib/format_engine/format_spec/variable.rb +41 -16
- data/lib/format_engine/version.rb +1 -1
- data/tests/format_spec_tests.rb +15 -0
- data/tests/variable_spec_tests.rb +16 -0
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 9c631ec0b15ff1952a3a5b86419b573da0abbff2
|
4
|
+
data.tar.gz: 0186ba016892c41dad4f6e6e9d36f7a7cb2f2b04
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: d499b1394e144d1f454f7050b2c9efd1fc6d303384c544a53491f485e1e894b62ab1cc994921c82948185a7151481156e9e4a579fae45ee2a3c2222fb3dfbee3
|
7
|
+
data.tar.gz: 9e8ff69ca8bac9cf3c33ab3cfea213594c3a72a34acc3e154978d594fe6239c773fcaa3f0c8d8d4fc9461505a9124f3357e2badb6f8ae1abe3b17ca3a680c94b
|
data/README.md
CHANGED
@@ -102,7 +102,7 @@ creates the following format specification array:
|
|
102
102
|
```ruby
|
103
103
|
[Literal("Elapsed = "),
|
104
104
|
Variable("%*H", ["02"]),
|
105
|
-
Literal("
|
105
|
+
Literal(":"),
|
106
106
|
Variable("%M", nil).
|
107
107
|
Literal(":"),
|
108
108
|
Variable("%S", ["5", "2"]),
|
@@ -149,8 +149,13 @@ Methods
|
|
149
149
|
|
150
150
|
###Format Specifier Attributes
|
151
151
|
The format specifier (accessed as fmt above) has the following attributes:
|
152
|
+
* has_width? - Was a width specified?
|
152
153
|
* width - The width parameter or 0 if not specified.
|
154
|
+
* width_str - The actual width text or an empty string.
|
155
|
+
* has_prec? - Was a precision specified?
|
153
156
|
* prec - The precision parameter or 0 if not specified.
|
157
|
+
* prec_str - The actual precision text or an empty string.
|
158
|
+
* parm_str - The actual parameter text or an empty string.
|
154
159
|
|
155
160
|
## Philosophy
|
156
161
|
|
@@ -1,60 +1,85 @@
|
|
1
1
|
module FormatEngine
|
2
2
|
|
3
|
-
#
|
3
|
+
#A format engine variable specification.
|
4
4
|
class FormatVariable
|
5
5
|
|
6
|
-
#
|
6
|
+
#The fixed part of this variable specification.
|
7
7
|
attr_reader :format
|
8
8
|
|
9
|
-
#
|
9
|
+
#The (optional) numeric format parameters or nil.
|
10
10
|
attr_reader :parms
|
11
11
|
|
12
|
-
#
|
12
|
+
#The (optional) parameter data as a string or an empty string.
|
13
|
+
attr_reader :parm_str
|
14
|
+
|
15
|
+
#Setup a variable format specification.
|
13
16
|
def initialize(format)
|
14
17
|
if format =~ /(\d+(\.\d+)?)/
|
15
|
-
@format
|
18
|
+
@format = $PREMATCH + $POSTMATCH
|
19
|
+
@parm_str = $MATCH
|
16
20
|
|
17
|
-
if (
|
21
|
+
if (@parm_str) =~ /\./
|
18
22
|
@parms = [$PREMATCH, $POSTMATCH]
|
19
23
|
else
|
20
|
-
@parms = [
|
24
|
+
@parms = [@parm_str]
|
21
25
|
end
|
22
26
|
|
23
27
|
else
|
24
|
-
@
|
25
|
-
@
|
28
|
+
@format = format
|
29
|
+
@parm_str = ""
|
30
|
+
@parms = nil
|
26
31
|
end
|
27
32
|
end
|
28
33
|
|
29
|
-
#
|
34
|
+
#Is this variable supported by the engine?
|
30
35
|
def validate(engine)
|
31
36
|
fail "Unsupported tag = #{format.inspect}" unless engine[format]
|
32
37
|
self
|
33
38
|
end
|
34
39
|
|
40
|
+
#Has a width been specified?
|
41
|
+
def has_width?
|
42
|
+
parms
|
43
|
+
end
|
44
|
+
|
35
45
|
# Get the width parameter.
|
36
46
|
def width
|
37
|
-
|
47
|
+
has_width? ? parms[0].to_i : 0
|
48
|
+
end
|
49
|
+
|
50
|
+
#Get the width as a string
|
51
|
+
def width_str
|
52
|
+
has_width? ? parms[0] : ""
|
38
53
|
end
|
39
54
|
|
40
|
-
#
|
55
|
+
#Has a precision been specified?
|
56
|
+
def has_prec?
|
57
|
+
has_width? && parms.length > 1
|
58
|
+
end
|
59
|
+
|
60
|
+
#Get the precision parameter.
|
41
61
|
def prec
|
42
|
-
|
62
|
+
has_prec? ? parms[1].to_i : 0
|
63
|
+
end
|
64
|
+
|
65
|
+
#Get the precision as a string
|
66
|
+
def prec_str
|
67
|
+
has_prec? ? parms[1] : ""
|
43
68
|
end
|
44
69
|
|
45
|
-
#
|
70
|
+
#Format onto the output string
|
46
71
|
def do_format(spec_info)
|
47
72
|
spec_info.fmt = self
|
48
73
|
spec_info.instance_exec(&spec_info.engine[self.format])
|
49
74
|
end
|
50
75
|
|
51
|
-
#
|
76
|
+
#Parse from the input string
|
52
77
|
def do_parse(spec_info)
|
53
78
|
spec_info.fmt = self
|
54
79
|
spec_info.instance_exec(&spec_info.engine[self.format])
|
55
80
|
end
|
56
81
|
|
57
|
-
#
|
82
|
+
#Inspect for debugging.
|
58
83
|
def inspect
|
59
84
|
"Variable(#{format.inspect}, #{parms.inspect})"
|
60
85
|
end
|
data/tests/format_spec_tests.rb
CHANGED
@@ -32,6 +32,11 @@ class FormatSpecTester < Minitest::Test
|
|
32
32
|
assert_equal(1, test.specs.length)
|
33
33
|
assert_equal(FormatEngine::FormatVariable, test.specs[0].class)
|
34
34
|
assert_equal("%#{char}A", test.specs[0].format)
|
35
|
+
refute(test.specs[0].has_width?)
|
36
|
+
refute(test.specs[0].has_prec?)
|
37
|
+
assert_equal("", test.specs[0].width_str)
|
38
|
+
assert_equal("", test.specs[0].prec_str)
|
39
|
+
assert_equal("", test.specs[0].parm_str)
|
35
40
|
end
|
36
41
|
end
|
37
42
|
|
@@ -45,6 +50,11 @@ class FormatSpecTester < Minitest::Test
|
|
45
50
|
assert_equal(Array, test.specs[0].parms.class)
|
46
51
|
assert_equal(1, test.specs[0].parms.length)
|
47
52
|
assert_equal("123", test.specs[0].parms[0])
|
53
|
+
assert(test.specs[0].has_width?)
|
54
|
+
refute(test.specs[0].has_prec?)
|
55
|
+
assert_equal("123", test.specs[0].width_str)
|
56
|
+
assert_equal("", test.specs[0].prec_str)
|
57
|
+
assert_equal("123", test.specs[0].parm_str)
|
48
58
|
end
|
49
59
|
|
50
60
|
def test_that_it_scans_double_variable_formats
|
@@ -58,6 +68,11 @@ class FormatSpecTester < Minitest::Test
|
|
58
68
|
assert_equal(2, test.specs[0].parms.length)
|
59
69
|
assert_equal("123", test.specs[0].parms[0])
|
60
70
|
assert_equal("456", test.specs[0].parms[1])
|
71
|
+
assert(test.specs[0].has_width?)
|
72
|
+
assert(test.specs[0].has_prec?)
|
73
|
+
assert_equal("123", test.specs[0].width_str)
|
74
|
+
assert_equal("456", test.specs[0].prec_str)
|
75
|
+
assert_equal("123.456", test.specs[0].parm_str)
|
61
76
|
end
|
62
77
|
|
63
78
|
def test_multipart_formats
|
@@ -20,15 +20,31 @@ class VariableSpecTester < Minitest::Test
|
|
20
20
|
|
21
21
|
def test_the_parms
|
22
22
|
test = FormatEngine::FormatVariable.new("%B")
|
23
|
+
refute(test.has_width?)
|
23
24
|
assert_equal(0, test.width)
|
25
|
+
assert_equal("", test.width_str)
|
26
|
+
refute(test.has_prec?)
|
24
27
|
assert_equal(0, test.prec)
|
28
|
+
assert_equal("", test.prec_str)
|
29
|
+
assert_equal("", test.parm_str)
|
25
30
|
|
26
31
|
test = FormatEngine::FormatVariable.new("%10B")
|
32
|
+
assert(test.has_width?)
|
27
33
|
assert_equal(10, test.width)
|
34
|
+
assert_equal("10", test.width_str)
|
35
|
+
refute(test.has_prec?)
|
28
36
|
assert_equal(0, test.prec)
|
37
|
+
assert_equal("", test.prec_str)
|
38
|
+
assert_equal("10", test.parm_str)
|
29
39
|
|
30
40
|
test = FormatEngine::FormatVariable.new("%10.5B")
|
41
|
+
assert(test.has_width?)
|
31
42
|
assert_equal(10, test.width)
|
43
|
+
assert_equal("10", test.width_str)
|
44
|
+
assert(test.has_prec?)
|
32
45
|
assert_equal(5, test.prec)
|
46
|
+
assert_equal("5", test.prec_str)
|
47
|
+
assert_equal("10.5", test.parm_str)
|
48
|
+
|
33
49
|
end
|
34
50
|
end
|
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.1.
|
4
|
+
version: 0.1.1
|
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-
|
11
|
+
date: 2015-07-11 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|