format_engine 0.3.1 → 0.3.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.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 31f6ac5a6816b11346d2bfe10a94108395ac2feb
4
- data.tar.gz: 9f0aabe82aa6fcaeac3a9300bb17b2588e48ff52
3
+ metadata.gz: 5d7dfb82f1660f588d8c962202bc052410d0103a
4
+ data.tar.gz: afc3a10a88f09024a9d37493bf77308be189473b
5
5
  SHA512:
6
- metadata.gz: 3a4c4b22b157eeccaaaa6225e6a22db1b6c2fef0c1ee38ff5e05b3088f840990c55832f26995c5d5de0175e70e2d6cccae6b822ad1c1e8e0c9b20170d4025073
7
- data.tar.gz: 54370ba86beaeaae3faaf6b354e8fc6a63781b5b73695ccf1ccc5e20afd08f1d2a3a1808127df5887172e309e940ba026d319f88a6de811f358f19f27aebad00
6
+ metadata.gz: 5f0d437c4b5d083b40287823fe705cb3a09871b68b8ed8805eb09e64f5d28dbb80779fb5f471f261d1ff9e84d0ba4fcd01c8f0ee78fd32b7bc0fcaa4201a585d
7
+ data.tar.gz: 110691ceb8a920df1b626febe8f803402e2b8ad48fe7934bf5cd06a6f0d84e8f04bcacd404c2c885a1fa9da7ccfc4fbf432585ce5089345bf5bac18e432f60a1
data/README.md CHANGED
@@ -37,8 +37,6 @@ require 'format_engine'
37
37
 
38
38
  #A demo class for the format_engine gem.
39
39
 
40
- require 'format_engine'
41
-
42
40
  class Customer
43
41
  extend FormatEngine::AttrFormatter
44
42
  extend FormatEngine::AttrParser
@@ -8,9 +8,9 @@ module FormatEngine
8
8
 
9
9
  # Set up a literal format specification.
10
10
  def initialize(literal)
11
- @literal = literal
12
- @head = literal.rstrip
13
- @tail = @head != @literal
11
+ @literal = literal
12
+ @head = literal.rstrip
13
+ @has_tail = @head != @literal
14
14
  end
15
15
 
16
16
  # Is this literal supported by the engine? YES!
@@ -26,7 +26,7 @@ module FormatEngine
26
26
  # Parse from the input string
27
27
  def do_parse(spec_info)
28
28
  spec_info.parse!(@head) unless @head.empty?
29
- spec_info.parse(/\s*/) if @tail
29
+ spec_info.parse(/\s*/) if @has_tail
30
30
  end
31
31
 
32
32
  # Inspect for debugging.
@@ -17,6 +17,12 @@ module FormatEngine
17
17
 
18
18
  #The format string parser.
19
19
  class FormatSpec
20
+ #The regex used to parse variable specifications.
21
+ VAR_REGEX = %r{(?<flags> [~@#$^&*\=?_<>\\\/\.,\|!]*){0}
22
+ (?<parms> [-+]?(\d+(\.\d+)?)?){0}
23
+ (%\g<flags>\g<parms>[a-zA-Z])
24
+ }x
25
+
20
26
  #Don't use new, use get_spec instead.
21
27
  private_class_method :new
22
28
 
@@ -41,7 +47,7 @@ module FormatEngine
41
47
  #Scan the format string extracting literals and variables.
42
48
  def scan_spec(fmt_string)
43
49
  until fmt_string.empty?
44
- if fmt_string =~ /%[~@#$^&*\=?_<>\\\/\.,\|!]*[-+]?(\d+(\.\d+)?)?[a-zA-Z]/
50
+ if fmt_string =~ VAR_REGEX
45
51
  @specs << FormatLiteral.new($PREMATCH) unless $PREMATCH.empty?
46
52
  @specs << FormatVariable.new($MATCH)
47
53
  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.3.1"
4
+ VERSION = "0.3.2"
5
5
  end
@@ -7,9 +7,9 @@ class Customer
7
7
  #The specification of the parser method of the demo \Customer class.
8
8
 
9
9
  attr_parser :strprs,
10
- {"%a" => lambda { tmp[:age] = found.to_i if parse(/(\d)+/ ) },
11
- "%f" => lambda { tmp[:fn] = found if parse(/(\w)+/ ) },
12
- "%l" => lambda { tmp[:ln] = found if parse(/(\w)+/ ) },
10
+ {"%a" => lambda { tmp[:age] = found.to_i if parse(/(\d)+/) },
11
+ "%f" => lambda { tmp[:fn] = found if parse(/(\w)+/) },
12
+ "%l" => lambda { tmp[:ln] = found if parse(/(\w)+/) },
13
13
  :after => lambda { set dst.new(tmp[:fn], tmp[:ln], tmp[:age]) }
14
14
  }
15
15
 
data/mocks/demo.rb CHANGED
@@ -22,6 +22,7 @@ class Customer
22
22
  end
23
23
  end
24
24
 
25
- cust = Customer.strprs('Jane, Smith 22', "%f, %l %a")
26
-
27
- puts cust.strfmt('%f %l is %a years old.')
25
+ if __FILE__ == $0
26
+ cust = Customer.strprs('Jane, Smith 22', "%f, %l %a")
27
+ puts cust.strfmt('%f %l is %a years old.')
28
+ end
@@ -12,18 +12,19 @@ class FormatEngineTester < Minitest::Test
12
12
  MinitestVisible.track self, __FILE__
13
13
 
14
14
  def test_basic_formatting
15
- cust = Customer.new("Jane", "Doe")
15
+ cust = Customer.new("Jane", "Doe", 21)
16
16
 
17
- assert_equal("Jane, Doe", cust.strfmt("%f, %l"))
17
+ assert_equal("Jane, Doe 21", cust.strfmt("%f, %l %a"))
18
18
  end
19
19
 
20
20
 
21
21
  def test_basic_parsing
22
- cust = Customer.strprs("Jane, Doe", "%f, %l")
22
+ cust = Customer.strprs("Jane, Doe 21", "%f, %l %a")
23
23
 
24
24
  assert_equal(Customer, cust.class)
25
25
  assert_equal("Jane", cust.first_name)
26
26
  assert_equal("Doe", cust.last_name)
27
+ assert_equal(21, cust.age)
27
28
  end
28
29
 
29
30
  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.1
4
+ version: 0.3.2
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-31 00:00:00.000000000 Z
11
+ date: 2016-02-03 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler