format_engine 0.3.1 → 0.3.2
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +0 -2
- data/lib/format_engine/format_spec/literal.rb +4 -4
- data/lib/format_engine/format_spec.rb +7 -1
- data/lib/format_engine/version.rb +1 -1
- data/mocks/demo/demo_parse.rb +3 -3
- data/mocks/demo.rb +4 -3
- data/tests/format_engine_tests.rb +4 -3
- 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: 5d7dfb82f1660f588d8c962202bc052410d0103a
|
4
|
+
data.tar.gz: afc3a10a88f09024a9d37493bf77308be189473b
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 5f0d437c4b5d083b40287823fe705cb3a09871b68b8ed8805eb09e64f5d28dbb80779fb5f471f261d1ff9e84d0ba4fcd01c8f0ee78fd32b7bc0fcaa4201a585d
|
7
|
+
data.tar.gz: 110691ceb8a920df1b626febe8f803402e2b8ad48fe7934bf5cd06a6f0d84e8f04bcacd404c2c885a1fa9da7ccfc4fbf432585ce5089345bf5bac18e432f60a1
|
data/README.md
CHANGED
@@ -8,9 +8,9 @@ module FormatEngine
|
|
8
8
|
|
9
9
|
# Set up a literal format specification.
|
10
10
|
def initialize(literal)
|
11
|
-
@literal
|
12
|
-
@head
|
13
|
-
@
|
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
|
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 =~
|
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
|
data/mocks/demo/demo_parse.rb
CHANGED
@@ -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
|
-
|
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.
|
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-
|
11
|
+
date: 2016-02-03 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|