format_engine 0.3.0 → 0.3.1

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: b0baee6898a0254f3c7e17992bdb405855786cde
4
- data.tar.gz: e4ed97834beefe97848e7c45e9a8bbda13918b4d
3
+ metadata.gz: 31f6ac5a6816b11346d2bfe10a94108395ac2feb
4
+ data.tar.gz: 9f0aabe82aa6fcaeac3a9300bb17b2588e48ff52
5
5
  SHA512:
6
- metadata.gz: 81baf16535d34920bfc19c3be71fe479fc728db0e0f07c9926b32e97c088a2072c241ea131e144d14ab19e2b8699875b0b50afedb82c1964e42f59ec73ea40f2
7
- data.tar.gz: 2721ee739f1a909e8307612f55ff84a916b103b804b6108e2e89d612d191600ecd6f48a9766b806c83814b4b32e69baa5e7d3b4fe32d6610ae71d98feb789a0c
6
+ metadata.gz: 3a4c4b22b157eeccaaaa6225e6a22db1b6c2fef0c1ee38ff5e05b3088f840990c55832f26995c5d5de0175e70e2d6cccae6b822ad1c1e8e0c9b20170d4025073
7
+ data.tar.gz: 54370ba86beaeaae3faaf6b354e8fc6a63781b5b73695ccf1ccc5e20afd08f1d2a3a1808127df5887172e309e940ba026d319f88a6de811f358f19f27aebad00
data/README.md CHANGED
@@ -37,6 +37,8 @@ require 'format_engine'
37
37
 
38
38
  #A demo class for the format_engine gem.
39
39
 
40
+ require 'format_engine'
41
+
40
42
  class Customer
41
43
  extend FormatEngine::AttrFormatter
42
44
  extend FormatEngine::AttrParser
@@ -47,32 +49,39 @@ class Customer
47
49
  #Demo customer last name
48
50
  attr_reader :last_name
49
51
 
52
+ #Demo customer age
53
+ attr_reader :age
54
+
50
55
  #Demo defn of the strfmt method for formatted string output!
51
56
  attr_formatter :strfmt,
52
- {"%f" => lambda {cat "%#{fmt.width_str}s" % src.first_name },
53
- "%l" => lambda {cat "%#{fmt.width_str}s" % src.last_name } }
57
+ {"%a" => lambda {cat "%#{fmt.width_str}d" % src.age },
58
+ "%f" => lambda {cat "%#{fmt.width_str}s" % src.first_name },
59
+ "%l" => lambda {cat "%#{fmt.width_str}s" % src.last_name }
60
+ }
54
61
 
55
62
  #Demo defn of the strprs method for formatted string input!
56
63
  attr_parser :strprs,
57
- {"%f" => lambda { tmp[:fn] = found if parse(/(\w)+/ ) },
64
+ {"%a" => lambda { tmp[:age] = found.to_i if parse(/(\d)+/ ) },
65
+ "%f" => lambda { tmp[:fn] = found if parse(/(\w)+/ ) },
58
66
  "%l" => lambda { tmp[:ln] = found if parse(/(\w)+/ ) },
59
- :after => lambda { set dst.new(tmp[:fn], tmp[:ln]) } }
67
+ :after => lambda { set dst.new(tmp[:fn], tmp[:ln], tmp[:age]) }
68
+ }
60
69
 
61
70
  #Create an instance of the demo customer.
62
- def initialize(first_name, last_name)
63
- @first_name, @last_name = first_name, last_name
71
+ def initialize(first_name, last_name, age)
72
+ @first_name, @last_name, @age = first_name, last_name, age
64
73
  end
65
74
  end
66
75
 
67
76
  #Then later in the code...
68
77
 
69
- cust = Customer.new("Jane", "Doe")
70
- puts cust.strfmt("%f, %l")) #"Jane, Doe"
78
+ cust = Customer.strprs('Jane, Smith 22', "%f, %l %a")
79
+
80
+ # snd still in another part of Gotham City...
71
81
 
72
- #And elsewhere in Gotham City...
82
+ puts cust.strfmt('%f %l is %a years old.')
73
83
 
74
- in_str = "Jane, Doe"
75
- agent = Customer.strprs(in_str, "%f, %l")
84
+ # Prints out: Jane Smith is 22 years old.
76
85
 
77
86
  #Etc, etc, etc ...
78
87
 
@@ -10,7 +10,7 @@ module FormatEngine
10
10
  def initialize(literal)
11
11
  @literal = literal
12
12
  @head = literal.rstrip
13
- @tail = literal.end_with?(' ')
13
+ @tail = @head != @literal
14
14
  end
15
15
 
16
16
  # Is this literal supported by the engine? YES!
@@ -1,5 +1,5 @@
1
1
 
2
2
  module FormatEngine
3
3
  # The version of the format_engine gem.
4
- VERSION = "0.3.0"
4
+ VERSION = "0.3.1"
5
5
  end
@@ -7,7 +7,9 @@ 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 "%#{fmt.width_str}s" % src.first_name },
11
- "%l" => lambda {cat "%#{fmt.width_str}s" % src.last_name } }
10
+ {"%a" => lambda {cat "%#{fmt.width_str}d" % src.age },
11
+ "%f" => lambda {cat "%#{fmt.width_str}s" % src.first_name },
12
+ "%l" => lambda {cat "%#{fmt.width_str}s" % src.last_name }
13
+ }
12
14
 
13
15
  end
@@ -7,8 +7,10 @@ class Customer
7
7
  #The specification of the parser method of the demo \Customer class.
8
8
 
9
9
  attr_parser :strprs,
10
- {"%f" => lambda { tmp[:fn] = found if parse(/(\w)+/ ) },
10
+ {"%a" => lambda { tmp[:age] = found.to_i if parse(/(\d)+/ ) },
11
+ "%f" => lambda { tmp[:fn] = found if parse(/(\w)+/ ) },
11
12
  "%l" => lambda { tmp[:ln] = found if parse(/(\w)+/ ) },
12
- :after => lambda { set dst.new(tmp[:fn], tmp[:ln]) } }
13
+ :after => lambda { set dst.new(tmp[:fn], tmp[:ln], tmp[:age]) }
14
+ }
13
15
 
14
16
  end
data/mocks/demo.rb CHANGED
@@ -1,5 +1,7 @@
1
1
  # A simple class of customer info.
2
2
 
3
+ require_relative '../lib/format_engine'
4
+
3
5
  require_relative 'demo/demo_format'
4
6
  require_relative 'demo/demo_parse'
5
7
 
@@ -11,8 +13,15 @@ class Customer
11
13
  #Demo customer last name
12
14
  attr_reader :last_name
13
15
 
16
+ #Demo customer age
17
+ attr_reader :age
18
+
14
19
  #Create an instance of the demo customer.
15
- def initialize(first_name, last_name)
16
- @first_name, @last_name = first_name, last_name
20
+ def initialize(first_name, last_name, age)
21
+ @first_name, @last_name, @age = first_name, last_name, age
17
22
  end
18
23
  end
24
+
25
+ cust = Customer.strprs('Jane, Smith 22', "%f, %l %a")
26
+
27
+ puts cust.strfmt('%f %l is %a years old.')
@@ -3,8 +3,9 @@
3
3
  class TestPerson
4
4
  attr_reader :first_name
5
5
  attr_reader :last_name
6
+ attr_reader :age
6
7
 
7
- def initialize(first_name, last_name)
8
- @first_name, @last_name = first_name, last_name
8
+ def initialize(first_name, last_name, age)
9
+ @first_name, @last_name, @age = first_name, last_name, age
9
10
  end
10
11
  end
@@ -25,6 +25,23 @@ class FormatSpecTester < Minitest::Test
25
25
  assert_equal(nil, test.specs[0].parms)
26
26
  end
27
27
 
28
+ def test_that_it_scans_tab_seperators
29
+ test = FormatEngine::FormatSpec.get_spec "%A\t%B"
30
+ assert_equal(Array, test.specs.class)
31
+ assert_equal(3, test.specs.length)
32
+
33
+ assert_equal(FormatEngine::FormatVariable, test.specs[0].class)
34
+ assert_equal("%A", test.specs[0].format)
35
+ assert_equal(nil, test.specs[0].parms)
36
+
37
+ assert_equal(FormatEngine::FormatLiteral, test.specs[1].class)
38
+ assert_equal("\t", test.specs[1].literal)
39
+
40
+ assert_equal(FormatEngine::FormatVariable, test.specs[2].class)
41
+ assert_equal("%B", test.specs[2].format)
42
+ assert_equal(nil, test.specs[2].parms)
43
+ end
44
+
28
45
  def test_that_it_scans_option_variable_formats
29
46
  "~@#&^&*-+=?_<>\\/.,|".each_char do |char|
30
47
  test = FormatEngine::FormatSpec.get_spec "%#{char}A"
@@ -13,6 +13,7 @@ class FormatterTester < Minitest::Test
13
13
 
14
14
  def make_formatter
15
15
  FormatEngine::Engine.new(
16
+ "%a" => lambda {cat "%#{fmt.width_str}d" % src.age},
16
17
  "%f" => lambda {cat "%#{fmt.width_str}s" % src.first_name},
17
18
  "%F" => lambda {cat "%#{fmt.width_str}s" % src.first_name.upcase},
18
19
  "%l" => lambda {cat "%#{fmt.width_str}s" % src.last_name},
@@ -20,7 +21,7 @@ class FormatterTester < Minitest::Test
20
21
  end
21
22
 
22
23
  def make_person
23
- TestPerson.new("Squidly", "Jones")
24
+ TestPerson.new("Squidly", "Jones", 21)
24
25
  end
25
26
 
26
27
  def make_spec(str)
@@ -32,23 +33,23 @@ class FormatterTester < Minitest::Test
32
33
  end
33
34
 
34
35
  def test_that_it_can_format_normally
35
- engine, obj, spec = make_all("Name = %f %l")
36
- assert_equal("Name = Squidly Jones", engine.do_format(obj, spec))
36
+ engine, obj, spec = make_all("Name = %f %l %a")
37
+ assert_equal("Name = Squidly Jones 21", engine.do_format(obj, spec))
37
38
  end
38
39
 
39
40
  def test_that_it_can_format_shouting
40
- engine, obj, spec = make_all("Name = %F %L")
41
- assert_equal("Name = SQUIDLY JONES", engine.do_format(obj, spec))
41
+ engine, obj, spec = make_all("Name = %F %L %a")
42
+ assert_equal("Name = SQUIDLY JONES 21", engine.do_format(obj, spec))
42
43
  end
43
44
 
44
45
  def test_that_it_can_format_wider
45
- engine, obj, spec = make_all("Name = %-10f %-10l")
46
- assert_equal("Name = Squidly Jones ", engine.do_format(obj, spec))
46
+ engine, obj, spec = make_all("Name = %-10f %-10l %-5a")
47
+ assert_equal("Name = Squidly Jones 21 ", engine.do_format(obj, spec))
47
48
  end
48
49
 
49
50
  def test_that_it_can_format_right
50
- engine, obj, spec = make_all("Name = %10f %10l")
51
- assert_equal("Name = Squidly Jones", engine.do_format(obj, spec))
51
+ engine, obj, spec = make_all("Name = %10f %10l %5a")
52
+ assert_equal("Name = Squidly Jones 21", engine.do_format(obj, spec))
52
53
  end
53
54
 
54
55
  def test_that_it_calls_before_and_after
@@ -13,7 +13,8 @@ class ParserTester < Minitest::Test
13
13
 
14
14
  def make_parser
15
15
  FormatEngine::Engine.new(
16
- "%f" => lambda { tmp[:fn] = found if parse(/(\w)+/ ) },
16
+ "%a" => lambda { tmp[:age] = found.to_i if parse(/\d+/) },
17
+ "%f" => lambda { tmp[:fn] = found if parse(/(\w)+/) },
17
18
  "%F" => lambda { tmp[:fn] = found.upcase if parse(/(\w)+/) },
18
19
  "%-F" => lambda { tmp[:fn] = found.capitalize if parse(/(\w)+/) },
19
20
  "%l" => lambda { tmp[:ln] = found if parse(/(\w)+/ ) },
@@ -22,13 +23,15 @@ class ParserTester < Minitest::Test
22
23
  "%t" => lambda { parse("\t") },
23
24
  "%!t" => lambda { parse!("\t") },
24
25
 
25
- :after => lambda { set TestPerson.new(tmp[:fn], tmp[:ln]) })
26
+ :after => lambda do
27
+ set TestPerson.new(*[tmp[:fn], tmp[:ln], tmp[:age]].delete_if(&:nil?))
28
+ end)
26
29
  end
27
30
 
28
31
  def test_that_it_can_parse
29
32
  engine = make_parser
30
- spec = "%f, %l"
31
- result = engine.do_parse("Squidly, Jones", TestPerson, spec)
33
+ spec = "%f, %l %a"
34
+ result = engine.do_parse("Squidly, Jones 55", TestPerson, spec)
32
35
 
33
36
  assert_equal(TestPerson, result.class)
34
37
  assert_equal("Squidly", result.first_name)
@@ -37,8 +40,8 @@ class ParserTester < Minitest::Test
37
40
 
38
41
  def test_that_it_can_parse_loudly
39
42
  engine = make_parser
40
- spec = "%F, %L"
41
- result = engine.do_parse("Squidly, Jones", TestPerson, spec)
43
+ spec = "%F, %L %a"
44
+ result = engine.do_parse("Squidly, Jones 55", TestPerson, spec)
42
45
 
43
46
  assert_equal(TestPerson, result.class)
44
47
  assert_equal("SQUIDLY", result.first_name)
@@ -47,8 +50,8 @@ class ParserTester < Minitest::Test
47
50
 
48
51
  def test_that_it_can_fix_shouting
49
52
  engine = make_parser
50
- spec = "%-F, %-L"
51
- result = engine.do_parse("SQUIDLY, JONES", TestPerson, spec)
53
+ spec = "%-F, %-L %a"
54
+ result = engine.do_parse("SQUIDLY, JONES 55", TestPerson, spec)
52
55
 
53
56
  assert_equal(TestPerson, result.class)
54
57
  assert_equal("Squidly", result.first_name)
@@ -57,24 +60,24 @@ class ParserTester < Minitest::Test
57
60
 
58
61
  def test_that_it_can_flex_parse
59
62
  engine = make_parser
60
- spec = "%f, %l"
63
+ spec = "%f, %l, %a"
61
64
 
62
65
  #No spaces.
63
- result = engine.do_parse("Squidly,Jones", TestPerson, spec)
66
+ result = engine.do_parse("Squidly,Jones,55", TestPerson, spec)
64
67
 
65
68
  assert_equal(TestPerson, result.class)
66
69
  assert_equal("Squidly", result.first_name)
67
70
  assert_equal("Jones", result.last_name)
68
71
 
69
72
  #One space.
70
- result = engine.do_parse("Squidly, Jones", TestPerson, spec)
73
+ result = engine.do_parse("Squidly, Jones, 55", TestPerson, spec)
71
74
 
72
75
  assert_equal(TestPerson, result.class)
73
76
  assert_equal("Squidly", result.first_name)
74
77
  assert_equal("Jones", result.last_name)
75
78
 
76
79
  #Two spaces.
77
- result = engine.do_parse("Squidly, Jones", TestPerson, spec)
80
+ result = engine.do_parse("Squidly, Jones, 55", TestPerson, spec)
78
81
 
79
82
  assert_equal(TestPerson, result.class)
80
83
  assert_equal("Squidly", result.first_name)
@@ -83,8 +86,8 @@ class ParserTester < Minitest::Test
83
86
 
84
87
  def test_that_it_can_tab_parse
85
88
  engine = make_parser
86
- spec = "%f%t%l"
87
- result = engine.do_parse("Squidly\tJones", TestPerson, spec)
89
+ spec = "%f %l %a"
90
+ result = engine.do_parse("Squidly\tJones\t55", TestPerson, spec)
88
91
 
89
92
  assert_equal(TestPerson, result.class)
90
93
  assert_equal("Squidly", result.first_name)
@@ -93,17 +96,30 @@ class ParserTester < Minitest::Test
93
96
 
94
97
  def test_that_it_can_detect_errors
95
98
  engine = make_parser
96
- spec = "%f, %l"
99
+ spec = "%f, %l %a"
97
100
 
98
101
  assert_raises(RuntimeError) do
99
- engine.do_parse("Squidly Jones", TestPerson, spec)
102
+ engine.do_parse("Squidly Jones 55", TestPerson, spec)
100
103
  end
101
104
 
102
- spec = "%f%!t%l"
105
+ spec = "%f%!t%l %a"
103
106
 
104
107
  assert_raises(RuntimeError) do
108
+ engine.do_parse("Squidly Jones 55", TestPerson, spec)
109
+ end
110
+
111
+ spec = "%f %l"
112
+
113
+ assert_raises(ArgumentError) do
114
+ engine.do_parse("Squidly Jones 55", TestPerson, spec)
115
+ end
116
+
117
+ spec = "%f %l %a"
118
+
119
+ assert_raises(ArgumentError) do
105
120
  engine.do_parse("Squidly Jones", TestPerson, spec)
106
121
  end
122
+
107
123
  end
108
124
 
109
125
  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.3.0
4
+ version: 0.3.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: 2016-01-30 00:00:00.000000000 Z
11
+ date: 2016-01-31 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler