tmplt 0.0.1 → 0.1.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.
Files changed (6) hide show
  1. checksums.yaml +4 -4
  2. data/LICENSE +21 -0
  3. data/README.md +21 -10
  4. data/lib/tmplt.rb +3 -3
  5. data/spec/tmplt_spec.rb +36 -22
  6. metadata +5 -4
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: c189981acda50cb7656dc65f92a09e8f6fa566de
4
- data.tar.gz: 745c42a88c6c176372dbacd88c36117c1529cd3a
3
+ metadata.gz: 7cb3b9c8e34de70b61862ed37537f9ee4b804bba
4
+ data.tar.gz: cdaabb91114afd902ce1a53005f115b8c378d38b
5
5
  SHA512:
6
- metadata.gz: db70c9a996ddb096dedcf2380fbff22562de6d2095d14c13facba2bbd5a984258449f9fff586dc9f8a86086eaa94fa6fb2816257ab319b71b79c740b048739d9
7
- data.tar.gz: 4c29c1342b7a6dead2866124d4624c045e9fc58a6cb35a419c6ab3f73fa7effe842a6c140e47964f738d15155c125f6537ff33e016ae9c4ad3af96c36829be73
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
- # tmplt [![Gem Version](http://img.shields.io/gem/v/tmplt.svg)](https://rubygems.org/gems/tmplt) [![Build Status](https://img.shields.io/travis/yuanqing/tmplt.svg)](https://travis-ci.org/yuanqing/tmplt) [![Coverage Status](https://img.shields.io/coveralls/yuanqing/tmplt.svg)](https://coveralls.io/r/yuanqing/tmplt)
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
- ```ruby
9
+ ```rb
10
10
  tmpl = "{{ foo }}, {{ bar.baz }}!"
11
11
  data = {
12
12
  :foo => "Hello",
13
- :bar => {
14
- :baz => "World"
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
- Full usage details are in [the tests](https://github.com/yuanqing/tmplt/blob/master/spec/tmplt_spec.rb).
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
- Via [RubyGems](https://rubygems.org/), of course:
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 = access(data, $1)
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 access(data, keys)
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 { Tmplt.render("{{ 0 }}", data) }.to raise_error(ArgumentError)
18
+ expect(Tmplt.render("{{ 0 }}", data)).to eq("")
13
19
  end
14
20
 
15
- it "should replace the tag with the corresponding value in the data hash" do
16
- data = { :foo => "bar" }
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 be a global replace" do
22
- data = { :foo => "bar" }
23
- expect(Tmplt.render("{{ foo }} {{ foo }}", data)).to eq("bar bar")
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 replace the tag with the empty string if the tag is not found in the data" do
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 allow whitespace within the tag name" do
37
- data = { "foo bar" => "baz" }
38
- expect(Tmplt.render("{{ foo bar }}", data)).to eq("baz")
39
- expect(Tmplt.render("{{foo bar}}", data)).to eq("baz")
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 Proc values" do
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
- "bar baz" => {
79
- :qux => "quux"
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.1
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-07-15 00:00:00.000000000 Z
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