tmplt 0.0.1 → 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/LICENSE +21 -0
- data/README.md +21 -10
- data/lib/tmplt.rb +3 -3
- data/spec/tmplt_spec.rb +36 -22
- metadata +5 -4
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 7cb3b9c8e34de70b61862ed37537f9ee4b804bba
|
4
|
+
data.tar.gz: cdaabb91114afd902ce1a53005f115b8c378d38b
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: dda371cc08c21393979d1d18b3149b65c7a562a3823a26883b734f65b79234e783e99fb47f7cdb77f895e924fb70ec3a449b172a4f60db160aac32e4049fec99
|
7
|
+
data.tar.gz: 07c1a06cf8432d8c5ff27e1ceee6a57e40889a1683a924437cd5b9db4ba34b459976cdeaf80a8b412b5148a6a3736129c6f14c1fedf464037df6e092d71bca8e
|
data/LICENSE
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2014 Lim Yuan Qing
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
7
|
+
in the Software without restriction, including without limitation the rights
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
10
|
+
furnished to do so, subject to the following conditions:
|
11
|
+
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
13
|
+
copies or substantial portions of the Software.
|
14
|
+
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
21
|
+
SOFTWARE.
|
data/README.md
CHANGED
@@ -1,32 +1,43 @@
|
|
1
|
-
#
|
1
|
+
# Tmplt.rb [![Gem Version](http://img.shields.io/gem/v/tmplt.svg?style=flat)](https://rubygems.org/gems/tmplt) [![Build Status](https://img.shields.io/travis/yuanqing/tmplt.rb.svg?style=flat)](https://travis-ci.org/yuanqing/tmplt.rb) [![Coverage Status](https://img.shields.io/coveralls/yuanqing/tmplt.rb.svg?style=flat)](https://coveralls.io/r/yuanqing/tmplt.rb)
|
2
2
|
|
3
|
-
A Ruby gem for interpolating values from a hash into a template string.
|
3
|
+
> A Ruby gem for interpolating values from a hash or array into a template string.
|
4
4
|
|
5
5
|
## Usage
|
6
6
|
|
7
|
-
Straight-up substitution, nothing more:
|
7
|
+
Straight-up substitution, nothing more. Here’s a slightly contrived example:
|
8
8
|
|
9
|
-
```
|
9
|
+
```rb
|
10
10
|
tmpl = "{{ foo }}, {{ bar.baz }}!"
|
11
11
|
data = {
|
12
12
|
:foo => "Hello",
|
13
|
-
:bar =>
|
14
|
-
|
13
|
+
:bar => "World",
|
14
|
+
:baz => {
|
15
|
+
:qux => Proc.new {|d| "#{d[:bar]}" }
|
15
16
|
}
|
16
17
|
}
|
17
18
|
puts Tmplt.render(tmpl, data) #=> "Hello, World!"
|
18
19
|
```
|
19
20
|
|
20
|
-
|
21
|
+
More usage examples are in [the tests](https://github.com/yuanqing/tmplt.rb/blob/master/spec/tmplt_spec.rb).
|
22
|
+
|
23
|
+
## API
|
24
|
+
|
25
|
+
### Tmplt.render(tmpl, data)
|
26
|
+
|
27
|
+
Interpolates values from `data` into the `tmpl` string.
|
28
|
+
|
29
|
+
- `tmpl` is a `string` with tags enclosed in double braces. Use a dot-delimited tag to reference nested values in `data`.
|
30
|
+
|
31
|
+
- `data` can be a `hash` or `array`, with arbitrary nesting. If a `proc` in `data` is referenced in `tmpl`, it will be invoked, and its return value will be substituted into `tmpl`. The `proc` takes a single argument, namely the `data` object itself.
|
21
32
|
|
22
33
|
## Installation
|
23
34
|
|
24
|
-
|
35
|
+
Install via [RubyGems](https://rubygems.org/):
|
25
36
|
|
26
|
-
```
|
37
|
+
```bash
|
27
38
|
$ gem install tmplt
|
28
39
|
```
|
29
40
|
|
30
41
|
## License
|
31
42
|
|
32
|
-
MIT license
|
43
|
+
[MIT license](https://github.com/yuanqing/tmplt.rb/blob/master/LICENSE)
|
data/lib/tmplt.rb
CHANGED
@@ -3,9 +3,9 @@ class Tmplt
|
|
3
3
|
class << self
|
4
4
|
|
5
5
|
def render(tmpl, data)
|
6
|
-
raise ArgumentError unless tmpl.is_a?(String) && (data.is_a?(Hash))
|
6
|
+
raise ArgumentError unless tmpl.is_a?(String) && (data.is_a?(Hash) || data.is_a?(Array))
|
7
7
|
tmpl.gsub(/{{(.+?)}}/) do
|
8
|
-
val =
|
8
|
+
val = follow_path(data, $1)
|
9
9
|
if val.is_a?(Proc)
|
10
10
|
val = val.call(data)
|
11
11
|
end
|
@@ -19,7 +19,7 @@ class Tmplt
|
|
19
19
|
true if Float(obj) rescue false
|
20
20
|
end
|
21
21
|
|
22
|
-
def
|
22
|
+
def follow_path(data, keys)
|
23
23
|
keys.split(".").each do |key|
|
24
24
|
key.strip!
|
25
25
|
if key.start_with?(":")
|
data/spec/tmplt_spec.rb
CHANGED
@@ -7,36 +7,48 @@ describe Tmplt do
|
|
7
7
|
expect { Tmplt.render(1, data) }.to raise_error(ArgumentError)
|
8
8
|
end
|
9
9
|
|
10
|
-
it "should raise an ArgumentError if data is not a hash" do
|
10
|
+
it "should raise an ArgumentError if data is not a hash or an array" do
|
11
|
+
expect { Tmplt.render("{{ foo }}", 1) }.to raise_error(ArgumentError)
|
12
|
+
end
|
13
|
+
|
14
|
+
it "should replace the tag with the empty string if the tag is not in the data hash or array" do
|
15
|
+
data = {}
|
16
|
+
expect(Tmplt.render("{{ foo }}", data)).to eq("")
|
11
17
|
data = []
|
12
|
-
expect
|
18
|
+
expect(Tmplt.render("{{ 0 }}", data)).to eq("")
|
13
19
|
end
|
14
20
|
|
15
|
-
it "should
|
16
|
-
data = {
|
21
|
+
it "should allow string tag names" do
|
22
|
+
data = { "foo" => "bar" }
|
17
23
|
expect(Tmplt.render("{{ foo }}", data)).to eq("bar")
|
18
24
|
expect(Tmplt.render("{{foo}}", data)).to eq("bar")
|
19
25
|
end
|
20
26
|
|
21
|
-
it "should
|
22
|
-
data = {
|
23
|
-
expect(Tmplt.render("{{ foo
|
27
|
+
it "should allow whitespace within the string tag name" do
|
28
|
+
data = { "foo bar" => "baz" }
|
29
|
+
expect(Tmplt.render("{{ foo bar }}", data)).to eq("baz")
|
30
|
+
expect(Tmplt.render("{{foo bar}}", data)).to eq("baz")
|
24
31
|
end
|
25
32
|
|
26
|
-
it "should
|
27
|
-
data = {}
|
28
|
-
expect(Tmplt.render("{{ foo }}", data)).to eq("")
|
33
|
+
it "should allow symbol tag names" do
|
34
|
+
data = { :foo => "bar" }
|
35
|
+
expect(Tmplt.render("{{ foo }}", data)).to eq("bar")
|
36
|
+
expect(Tmplt.render("{{ :foo }}", data)).to eq("bar")
|
29
37
|
end
|
30
38
|
|
31
39
|
it "should allow numeric tag names" do
|
32
40
|
data = { 0 => "foo" }
|
33
41
|
expect(Tmplt.render("{{ 0 }}", data)).to eq("foo")
|
42
|
+
data = [ "foo", "bar" ]
|
43
|
+
expect(Tmplt.render("{{ 1 }}", data)).to eq("bar")
|
44
|
+
expect(Tmplt.render("{{1}}", data)).to eq("bar")
|
34
45
|
end
|
35
46
|
|
36
|
-
it "should
|
37
|
-
data = {
|
38
|
-
expect(Tmplt.render("{{ foo
|
39
|
-
|
47
|
+
it "should be a global replace" do
|
48
|
+
data = { :foo => "bar" }
|
49
|
+
expect(Tmplt.render("{{ foo }} {{ foo }}", data)).to eq("bar bar")
|
50
|
+
data = [ "foo", "bar" ]
|
51
|
+
expect(Tmplt.render("{{ 1 }} {{ 1 }}", data)).to eq("bar bar")
|
40
52
|
end
|
41
53
|
|
42
54
|
it "should match the string key and not the symbol key if the two keys have the same name" do
|
@@ -63,7 +75,7 @@ describe Tmplt do
|
|
63
75
|
expect(Tmplt.render("{{ foo }}{{ bar }}", data)).to eq("3.14159")
|
64
76
|
end
|
65
77
|
|
66
|
-
it "should interpolate
|
78
|
+
it "should interpolate proc values" do
|
67
79
|
data = {
|
68
80
|
:foo => "qux",
|
69
81
|
:bar => "quux",
|
@@ -72,16 +84,18 @@ describe Tmplt do
|
|
72
84
|
expect(Tmplt.render("{{ baz }}", data)).to eq("qux quux")
|
73
85
|
end
|
74
86
|
|
75
|
-
it "should interpolate nested keys" do
|
87
|
+
it "should interpolate values corresponding to nested keys" do
|
76
88
|
data = {
|
77
|
-
:foo =>
|
78
|
-
|
79
|
-
|
89
|
+
:foo => [
|
90
|
+
{
|
91
|
+
"bar baz" => {
|
92
|
+
:qux => "quux"
|
93
|
+
}
|
80
94
|
}
|
81
|
-
|
95
|
+
]
|
82
96
|
}
|
83
|
-
expect(Tmplt.render("{{ foo.bar baz.qux }}", data)).to eql("quux")
|
84
|
-
expect(Tmplt.render("{{foo . bar baz . qux}}", data)).to eql("quux")
|
97
|
+
expect(Tmplt.render("{{ foo.0.bar baz.qux }}", data)).to eql("quux")
|
98
|
+
expect(Tmplt.render("{{ foo . 0 . bar baz . qux }}", data)).to eql("quux")
|
85
99
|
end
|
86
100
|
|
87
101
|
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: tmplt
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0
|
4
|
+
version: 0.1.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- yuanqing
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2014-
|
11
|
+
date: 2014-08-15 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|
@@ -73,11 +73,12 @@ executables: []
|
|
73
73
|
extensions: []
|
74
74
|
extra_rdoc_files: []
|
75
75
|
files:
|
76
|
+
- LICENSE
|
76
77
|
- README.md
|
77
78
|
- lib/tmplt.rb
|
78
79
|
- spec/spec_helper.rb
|
79
80
|
- spec/tmplt_spec.rb
|
80
|
-
homepage: https://github.com/yuanqing/tmplt
|
81
|
+
homepage: https://github.com/yuanqing/tmplt.rb
|
81
82
|
licenses:
|
82
83
|
- MIT
|
83
84
|
metadata: {}
|
@@ -100,7 +101,7 @@ rubyforge_project:
|
|
100
101
|
rubygems_version: 2.1.11
|
101
102
|
signing_key:
|
102
103
|
specification_version: 4
|
103
|
-
summary: Interpolate values from a hash into a template string.
|
104
|
+
summary: Interpolate values from a hash or array into a template string.
|
104
105
|
test_files:
|
105
106
|
- spec/spec_helper.rb
|
106
107
|
- spec/tmplt_spec.rb
|