tagen 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.
data/Rakefile CHANGED
@@ -1,12 +1,17 @@
1
1
  desc "build a gem file"
2
2
  task :release do
3
- `rm *.gem`
4
- `gem build tagen.gemspec`
5
- `gem push *.gem`
6
- `rm *.gem`
3
+ `rm *.gem &>/dev/null`
4
+ sh "gem build tagen.gemspec"
5
+ sh "gem push *.gem"
6
+ sh "rm *.gem"
7
7
  end
8
8
 
9
9
  desc "testing the library"
10
10
  task :test do
11
- system "rspec --color spec"
11
+ sh "rspec --color spec"
12
+ end
13
+
14
+ def sh cmd
15
+ puts cmd
16
+ system cmd
12
17
  end
@@ -6,30 +6,33 @@ a python like string format libraray.
6
6
 
7
7
  1. "this is #{guten}" # Ruby Builtin
8
8
  2. "this is %s" % "guten" # Ruby Builtin
9
- 3. "this is {guten}".format(guten: 'x')
9
+ 3. "this is %{guten}".format(guten: 'x')
10
10
 
11
11
  use "#{var}" is easy and quick in many cases, but some times we need a more powerful format support.
12
12
 
13
13
  "I like %s and %s" % %(apple, football)
14
- "I like {fruit} and {sport}".format(%w(apple football)) # it has semantic meaning.
14
+ "I like %{fruit} and %{sport}".format(%w(apple football)) # it has semantic meaning.
15
15
 
16
16
  == Usage
17
17
 
18
18
  require "tagen/core"
19
- "it costs {:.2f} dollar".format(1.123) #=> "it costs 1.12 dollar"
19
+ "it costs %{:.2f} dollar".format(1.123) #=> "it costs 1.12 dollar"
20
20
 
21
21
  * support abritry-argument or hash-argument
22
- "{} {}".format(1,2) #=> "1 2"
23
- "{a} {b}".format(a:1, b:2) #=> "1 2"
24
- "{a} {b}".format(1, b:2) #=> "1 2"
22
+ "%{} %{}".format(1,2) #=> "1 2"
23
+ "%{a} %{b}".format(a:1, b:2) #=> "1 2"
24
+ "%{a} %{b}".format(1, b:2) #=> "1 2"
25
25
 
26
26
  * escape
27
- "my {{name}} is {name}".format("guten") #=> my name is guten.
27
+ "my \\%{name} is %{name}".format("guten") #=> my name is guten.
28
28
 
29
29
  == Examples
30
30
 
31
- "{:.2f}"
32
- "{name:.2f}"
31
+ "%{:.2f}"
32
+ "%{name:.2f}"
33
+
34
+ "I'm %{guten} and is #{age} years old".format(guten: "guten") # combine Ruby Buintin with PyFormat
35
+
33
36
 
34
37
  == Specification
35
38
 
@@ -77,10 +80,14 @@ class PyFormat
77
80
  # args -> argh and args
78
81
  argh = Hash===args[-1] ? args.pop : {}
79
82
 
80
- # "{0:.5f}"
81
- pat = /{{.*?}} | { (.*?)? (?: :(.*?) )? } /x
83
+ # "%{0:.5f}"
84
+ pat = /
85
+ \\%{.*?} # esacpe
86
+ | %{ (.*?)? (?: :(.*?) )? } # %{0:.f}
87
+ /x
88
+
82
89
  ret = @fmt.gsub(pat) do |m|
83
- if m.start_with? "{{"
90
+ if m.start_with? "\\%{"
84
91
  m
85
92
  else
86
93
  field, spec = $1, $2
@@ -5,60 +5,61 @@ require "spec_helper"
5
5
  describe PyFormat do
6
6
  before :all do
7
7
  # from go-src/fmt
8
- @test_datas = [
8
+ @test_datas = [ #{{{1
9
9
 
10
10
  # type ::= s c b o d x X ¦ f/F g/G e/E n %
11
- [ "{}", 1, "1"],
12
- [ "{:s}", 1, "1"],
13
- [ "{:c}", 'a', "a" ],
14
- [ "{:b}", 0b10, "10" ],
15
- [ "{:o}", 010, "10" ],
16
- [ "{:x}", 0xa, "a" ],
17
- [ "{:X}", 0xa, "A" ],
18
- [ "{:f}", 1, "1.0"],
19
- [ "{:F}", 1, "1.0"],
20
- [ "{:e}", 1, "1.000000e+00" ],
21
- [ "{:E}", 1, "1.000000E+00" ],
22
- [ "{:g}", 1, "1"],
23
- [ "{:g}", 1e6, "1e+06" ],
24
- [ "{:G}", 1e6, "1E+06" ],
11
+ [ "%{}", 1, "1"],
12
+ [ "%{:s}", 1, "1"],
13
+ [ "%{:c}", 'a', "a" ],
14
+ [ "%{:b}", 0b10, "10" ],
15
+ [ "%{:o}", 010, "10" ],
16
+ [ "%{:x}", 0xa, "a" ],
17
+ [ "%{:X}", 0xa, "A" ],
18
+ [ "%{:f}", 1, "1.0"],
19
+ [ "%{:F}", 1, "1.0"],
20
+ [ "%{:e}", 1, "1.000000e+00" ],
21
+ [ "%{:E}", 1, "1.000000E+00" ],
22
+ [ "%{:g}", 1, "1"],
23
+ [ "%{:g}", 1e6, "1e+06" ],
24
+ [ "%{:G}", 1e6, "1E+06" ],
25
25
  #[ "{:n}" ],
26
- [ "{:%}", 1, "100%" ],
26
+ [ "%{:%}", 1, "100%" ],
27
27
 
28
28
  # width
29
- [ "{:5s}", 1234, " 1234"],
30
- [ "{:3s}", 1234, "1234"],
31
- [ "{:.3s}", 1234, "123"],
29
+ [ "%{:5s}", 1234, " 1234"],
30
+ [ "%{:3s}", 1234, "1234"],
31
+ [ "%{:.3s}", 1234, "123"],
32
32
 
33
- [ "{:3f}", 1.123, "1.123"],
34
- [ "{:.1f}", 1.123, "1.1"],
35
- [ "{:4.1f}", 1.123, " 1.1"],
33
+ [ "%{:3f}", 1.123, "1.123"],
34
+ [ "%{:.1f}", 1.123, "1.1"],
35
+ [ "%{:4.1f}", 1.123, " 1.1"],
36
36
 
37
37
  # fill align
38
- [ "{:0<2}", 1, "10"],
39
- [ "{:0>2}", 1, "01"],
40
- [ "{:0^4}", 1, "0100"],
41
- [ "{:02}", 1, "01"],
38
+ [ "%{:0<2}", 1, "10"],
39
+ [ "%{:0>2}", 1, "01"],
40
+ [ "%{:0^4}", 1, "0100"],
41
+ [ "%{:02}", 1, "01"],
42
42
 
43
43
  # sign
44
- [ "{:+}", 1, "+1"],
45
- [ "{: }", 1, " 1"],
46
- [ "{:-}", 1, "1"],
44
+ [ "%{:+}", 1, "+1"],
45
+ [ "%{: }", 1, " 1"],
46
+ [ "%{:-}", 1, "1"],
47
47
 
48
- [ "{:+3}", 1, " +1"],
49
- [ "{:=3}", 1, "+01"],
48
+ [ "%{:+3}", 1, " +1"],
49
+ [ "%{:=3}", 1, "+01"],
50
50
 
51
51
  # alternate
52
- [ "{:#b}", 0b1, "0b1"],
53
- [ "{:#4b}", 0b1, " 0b1"],
54
- [ "{:#o}", 01, "01"],
55
- [ "{:#x}", 0x1, "0x1"],
52
+ [ "%{:#b}", 0b1, "0b1"],
53
+ [ "%{:#4b}", 0b1, " 0b1"],
54
+ [ "%{:#o}", 01, "01"],
55
+ [ "%{:#x}", 0x1, "0x1"],
56
56
 
57
57
  # comma
58
- [ "{:,}", 1234, "1,234"],
58
+ [ "%{:,}", 1234, "1,234"],
59
59
 
60
60
  ] #
61
61
  end
62
+ #}}}1
62
63
 
63
64
  it "parse_spec. PAT.match()" do
64
65
  a = "0> #07,.7f"
@@ -66,13 +67,23 @@ describe PyFormat do
66
67
  PyFormat::Field::PAT.match(a).to_a[1..-1].should == b
67
68
  end
68
69
 
69
- it "parse: '{} {}'" do
70
- [
71
- [ "{} {}", [1, 2], "1 2" ],
72
- [ "{a} {{b}}", [{a:1}], "1 {{b}}" ],
73
- [ "{} {a} {b}", [1,2,3,{b:4}],"1 2 4" ],
74
- ].each do |src, args, rst|
75
- src.format(*args).should == rst
70
+ describe "#format" do
71
+ it "parse %{name}" do
72
+ "%{a}".format(a: 1).should == "1"
73
+ "%{a}".format(1).should == "1"
74
+ end
75
+
76
+ it "parse empty format %{}" do
77
+ "%{}".format(1).should == "1"
78
+ end
79
+
80
+ it "parse escape format. \%{name}" do
81
+ '\%{a}'.format.should == '\%{a}'
82
+ end
83
+
84
+ it "parse all formats %{} %{name} \%{name}" do
85
+ '%{} %{a} \%{a}'.format(1,a: 2).should == '1 2 \%{a}'
86
+ '%{} %{a} \%{a}'.format(1, 2).should == '1 2 \%{a}'
76
87
  end
77
88
  end
78
89
 
@@ -83,3 +94,5 @@ describe PyFormat do
83
94
  end
84
95
  end
85
96
  end
97
+
98
+ # vim: foldmethod=marker
data/version.rb CHANGED
@@ -1,7 +1,7 @@
1
1
  module VERSION
2
2
  MAJOR = 0
3
- MINOR = 1
4
- PATCH = 1
3
+ MINOR = 2
4
+ PATCH = 0
5
5
 
6
6
  IS = [MAJOR, MINOR, PATCH].join(".")
7
7
  end
metadata CHANGED
@@ -2,7 +2,7 @@
2
2
  name: tagen
3
3
  version: !ruby/object:Gem::Version
4
4
  prerelease:
5
- version: 0.1.1
5
+ version: 0.2.0
6
6
  platform: ruby
7
7
  authors:
8
8
  - Guten