format_engine 0.1.1 → 0.2.0

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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 9c631ec0b15ff1952a3a5b86419b573da0abbff2
4
- data.tar.gz: 0186ba016892c41dad4f6e6e9d36f7a7cb2f2b04
3
+ metadata.gz: 6a255811c1f0df1bc9b92fc02eaaee55b58b2f73
4
+ data.tar.gz: 10705de5b1a6bf5722cbf4d8350cce41f10a08c1
5
5
  SHA512:
6
- metadata.gz: d499b1394e144d1f454f7050b2c9efd1fc6d303384c544a53491f485e1e894b62ab1cc994921c82948185a7151481156e9e4a579fae45ee2a3c2222fb3dfbee3
7
- data.tar.gz: 9e8ff69ca8bac9cf3c33ab3cfea213594c3a72a34acc3e154978d594fe6239c773fcaa3f0c8d8d4fc9461505a9124f3357e2badb6f8ae1abe3b17ca3a680c94b
6
+ metadata.gz: b61ac28dd23cd0cb2c3a58f6ba6cf80b79d883933a0906f24de50c173019d718e49c89a807a0f476d444970dd407e9a2626e8b115bf47363adf9d77ba4db025d
7
+ data.tar.gz: e76b17e94d5e83e26e0c99c2d4e43b82e1c00ec5bd49de5c490c6c06f149bbee446fffecee2d9564d806176e9e269b7400269873c626b7c0a350af4f6829e147
data/README.md CHANGED
@@ -49,8 +49,8 @@ class Customer
49
49
 
50
50
  #Demo defn of the strfmt method for formatted string output!
51
51
  attr_formatter :strfmt,
52
- {"%f" => lambda {cat src.first_name.ljust(fmt.width) },
53
- "%l" => lambda {cat src.last_name.ljust(fmt.width) } }
52
+ {"%f" => lambda {cat "%#{fmt.width_str}s" % src.first_name },
53
+ "%l" => lambda {cat "%#{fmt.width_str}s" % src.last_name } }
54
54
 
55
55
  #Demo defn of the strprs method for formatted string input!
56
56
  attr_parser :strprs,
@@ -82,11 +82,8 @@ agent = Customer.strprs(in_str, "%f, %l")
82
82
  Format String Specification Syntax (BNF):
83
83
 
84
84
  * spec = ( text | item )+
85
- * item = "%" flag* (parm ("." parm)?)? command
86
- * flag = ( "~" | "@" | "#" | "&" | "^" |
87
- "&" | "*" | "-" | "+" | "=" |
88
- "?" | "_" | "<" | ">" | "\\" |
89
- "/" | "." | "," | "|" | "!" )
85
+ * item = "%" flag* ("+" | "-")?(parm ("." parm)?)? command
86
+ * flag = "~"|"@"|"#"|"&"|"^"|"&"|"*"|"="|"?"|"_"|"<"|">"|"\\"|"/"|"."|","|"|"|"!"
90
87
  * parm = ("0" .. "9")+
91
88
  * command = ("a" .. "z" | "A" .. "Z")
92
89
 
@@ -95,7 +92,7 @@ Format String Specification Syntax (BNF):
95
92
 
96
93
  The format specification:
97
94
  ```ruby
98
- "Elapsed = %*02H:%M:%5.2S!"
95
+ "Elapsed = %*02H:%M:%-5.2S!"
99
96
  ```
100
97
  creates the following format specification array:
101
98
 
@@ -105,7 +102,7 @@ creates the following format specification array:
105
102
  Literal(":"),
106
103
  Variable("%M", nil).
107
104
  Literal(":"),
108
- Variable("%S", ["5", "2"]),
105
+ Variable("%S", ["-5", "2"]),
109
106
  Literal("!")]
110
107
  ```
111
108
  Where literals are processed as themselves and variables are executed by looking
@@ -14,7 +14,7 @@ module FormatEngine
14
14
 
15
15
  #Setup a variable format specification.
16
16
  def initialize(format)
17
- if format =~ /(\d+(\.\d+)?)/
17
+ if format =~ /[+-]?(\d+(\.\d+)?)/
18
18
  @format = $PREMATCH + $POSTMATCH
19
19
  @parm_str = $MATCH
20
20
 
@@ -41,7 +41,7 @@ module FormatEngine
41
41
  #Scan the format string extracting literals and variables.
42
42
  def scan_spec(fmt_string)
43
43
  until fmt_string.empty?
44
- if fmt_string =~ /%[~@#$^&*\-+=?_<>\\\/\.,\|!]*(\d+(\.\d+)?)?[a-zA-Z]/
44
+ if fmt_string =~ /%[~@#$^&*\=?_<>\\\/\.,\|!]*[-+]?(\d+(\.\d+)?)?[a-zA-Z]/
45
45
  @specs << FormatLiteral.new($PREMATCH) unless $PREMATCH.empty?
46
46
  @specs << FormatVariable.new($MATCH)
47
47
  fmt_string = $POSTMATCH
@@ -1,5 +1,5 @@
1
1
 
2
2
  module FormatEngine
3
3
  # The version of the format_engine gem.
4
- VERSION = "0.1.1"
4
+ VERSION = "0.2.0"
5
5
  end
@@ -7,7 +7,7 @@ class Customer
7
7
  #The specification of the formatter method of the demo \Customer class.
8
8
 
9
9
  attr_formatter :strfmt,
10
- {"%f" => lambda {cat src.first_name.ljust(fmt.width) },
11
- "%l" => lambda {cat src.last_name.ljust(fmt.width) } }
10
+ {"%f" => lambda {cat "%#{fmt.width_str}s" % src.first_name },
11
+ "%l" => lambda {cat "%#{fmt.width_str}s" % src.last_name } }
12
12
 
13
13
  end
@@ -75,6 +75,24 @@ class FormatSpecTester < Minitest::Test
75
75
  assert_equal("123.456", test.specs[0].parm_str)
76
76
  end
77
77
 
78
+ def test_negative_variable_formats
79
+ test = FormatEngine::FormatSpec.get_spec "%-123.456A"
80
+ assert_equal(Array, test.specs.class)
81
+ assert_equal(1, test.specs.length)
82
+ assert_equal(FormatEngine::FormatVariable, test.specs[0].class)
83
+ assert_equal("%A", test.specs[0].format)
84
+
85
+ assert_equal(Array, test.specs[0].parms.class)
86
+ assert_equal(2, test.specs[0].parms.length)
87
+ assert_equal("-123", test.specs[0].parms[0])
88
+ assert_equal("456", test.specs[0].parms[1])
89
+ assert(test.specs[0].has_width?)
90
+ assert(test.specs[0].has_prec?)
91
+ assert_equal("-123", test.specs[0].width_str)
92
+ assert_equal("456", test.specs[0].prec_str)
93
+ assert_equal("-123.456", test.specs[0].parm_str)
94
+ end
95
+
78
96
  def test_multipart_formats
79
97
  test = FormatEngine::FormatSpec.get_spec "T(%+02A:%3B:%4.1C)"
80
98
 
@@ -85,9 +103,9 @@ class FormatSpecTester < Minitest::Test
85
103
  assert_equal("T(", test.specs[0].literal)
86
104
 
87
105
  assert_equal(FormatEngine::FormatVariable, test.specs[1].class)
88
- assert_equal("%+A", test.specs[1].format)
106
+ assert_equal("%A", test.specs[1].format)
89
107
  assert_equal(1, test.specs[1].parms.length)
90
- assert_equal("02", test.specs[1].parms[0])
108
+ assert_equal("+02", test.specs[1].parms[0])
91
109
 
92
110
  assert_equal(FormatEngine::FormatLiteral, test.specs[2].class)
93
111
  assert_equal(":", test.specs[2].literal)
@@ -13,14 +13,10 @@ class FormatterTester < Minitest::Test
13
13
 
14
14
  def make_formatter
15
15
  FormatEngine::Engine.new(
16
- "%f" => lambda {cat src.first_name.ljust(fmt.width) },
17
- "%-f" => lambda {cat src.first_name.rjust(fmt.width) },
18
- "%F" => lambda {cat src.first_name.upcase.ljust(fmt.width) },
19
- "%-F" => lambda {cat src.first_name.upcase.rjust(fmt.width) },
20
- "%l" => lambda {cat src.last_name.ljust(fmt.width)},
21
- "%-l" => lambda {cat src.last_name.rjust(fmt.width)},
22
- "%L" => lambda {cat src.last_name.upcase.ljust(fmt.width) },
23
- "%-L" => lambda {cat src.last_name.upcase.rjust(fmt.width) })
16
+ "%f" => lambda {cat "%#{fmt.width_str}s" % src.first_name},
17
+ "%F" => lambda {cat "%#{fmt.width_str}s" % src.first_name.upcase},
18
+ "%l" => lambda {cat "%#{fmt.width_str}s" % src.last_name},
19
+ "%L" => lambda {cat "%#{fmt.width_str}s" % src.last_name.upcase})
24
20
  end
25
21
 
26
22
  def make_person
@@ -46,12 +42,12 @@ class FormatterTester < Minitest::Test
46
42
  end
47
43
 
48
44
  def test_that_it_can_format_wider
49
- engine, obj, spec = make_all("Name = %10f %10l")
45
+ engine, obj, spec = make_all("Name = %-10f %-10l")
50
46
  assert_equal("Name = Squidly Jones ", engine.do_format(obj, spec))
51
47
  end
52
48
 
53
49
  def test_that_it_can_format_right
54
- engine, obj, spec = make_all("Name = %-10f %-10l")
50
+ engine, obj, spec = make_all("Name = %10f %10l")
55
51
  assert_equal("Name = Squidly Jones", engine.do_format(obj, spec))
56
52
  end
57
53
 
@@ -46,5 +46,14 @@ class VariableSpecTester < Minitest::Test
46
46
  assert_equal("5", test.prec_str)
47
47
  assert_equal("10.5", test.parm_str)
48
48
 
49
+ test = FormatEngine::FormatVariable.new("%-10.5B")
50
+ assert(test.has_width?)
51
+ assert_equal(-10, test.width)
52
+ assert_equal("-10", test.width_str)
53
+ assert(test.has_prec?)
54
+ assert_equal(5, test.prec)
55
+ assert_equal("5", test.prec_str)
56
+ assert_equal("-10.5", test.parm_str)
57
+
49
58
  end
50
59
  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.1
4
+ version: 0.2.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-11 00:00:00.000000000 Z
11
+ date: 2015-07-17 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler