tmplt 0.0.1

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 ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: c189981acda50cb7656dc65f92a09e8f6fa566de
4
+ data.tar.gz: 745c42a88c6c176372dbacd88c36117c1529cd3a
5
+ SHA512:
6
+ metadata.gz: db70c9a996ddb096dedcf2380fbff22562de6d2095d14c13facba2bbd5a984258449f9fff586dc9f8a86086eaa94fa6fb2816257ab319b71b79c740b048739d9
7
+ data.tar.gz: 4c29c1342b7a6dead2866124d4624c045e9fc58a6cb35a419c6ab3f73fa7effe842a6c140e47964f738d15155c125f6537ff33e016ae9c4ad3af96c36829be73
data/README.md ADDED
@@ -0,0 +1,32 @@
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)
2
+
3
+ A Ruby gem for interpolating values from a hash into a template string.
4
+
5
+ ## Usage
6
+
7
+ Straight-up substitution, nothing more:
8
+
9
+ ```ruby
10
+ tmpl = "{{ foo }}, {{ bar.baz }}!"
11
+ data = {
12
+ :foo => "Hello",
13
+ :bar => {
14
+ :baz => "World"
15
+ }
16
+ }
17
+ puts Tmplt.render(tmpl, data) #=> "Hello, World!"
18
+ ```
19
+
20
+ Full usage details are in [the tests](https://github.com/yuanqing/tmplt/blob/master/spec/tmplt_spec.rb).
21
+
22
+ ## Installation
23
+
24
+ Via [RubyGems](https://rubygems.org/), of course:
25
+
26
+ ```
27
+ $ gem install tmplt
28
+ ```
29
+
30
+ ## License
31
+
32
+ MIT license
data/lib/tmplt.rb ADDED
@@ -0,0 +1,41 @@
1
+ class Tmplt
2
+
3
+ class << self
4
+
5
+ def render(tmpl, data)
6
+ raise ArgumentError unless tmpl.is_a?(String) && (data.is_a?(Hash))
7
+ tmpl.gsub(/{{(.+?)}}/) do
8
+ val = access(data, $1)
9
+ if val.is_a?(Proc)
10
+ val = val.call(data)
11
+ end
12
+ is_number?(val) ? val.to_s : String.try_convert(val)
13
+ end
14
+ end
15
+
16
+ private
17
+
18
+ def is_number?(obj)
19
+ true if Float(obj) rescue false
20
+ end
21
+
22
+ def access(data, keys)
23
+ keys.split(".").each do |key|
24
+ key.strip!
25
+ if key.start_with?(":")
26
+ data = data[key[1..-1].to_sym] # drop the ":"
27
+ else
28
+ if key.to_i.to_s == key # true if key is a number
29
+ data = data[key.to_i]
30
+ else
31
+ data = data[key.to_s] || data[key.to_sym]
32
+ end
33
+ end
34
+ break unless data
35
+ end
36
+ data
37
+ end
38
+
39
+ end
40
+
41
+ end
@@ -0,0 +1,4 @@
1
+ require "coveralls"
2
+ Coveralls.wear!
3
+
4
+ require "tmplt"
@@ -0,0 +1,87 @@
1
+ require "spec_helper"
2
+
3
+ describe Tmplt do
4
+
5
+ it "should raise an ArgumentError if tmpl is not a string" do
6
+ data = { :foo => "bar" }
7
+ expect { Tmplt.render(1, data) }.to raise_error(ArgumentError)
8
+ end
9
+
10
+ it "should raise an ArgumentError if data is not a hash" do
11
+ data = []
12
+ expect { Tmplt.render("{{ 0 }}", data) }.to raise_error(ArgumentError)
13
+ end
14
+
15
+ it "should replace the tag with the corresponding value in the data hash" do
16
+ data = { :foo => "bar" }
17
+ expect(Tmplt.render("{{ foo }}", data)).to eq("bar")
18
+ expect(Tmplt.render("{{foo}}", data)).to eq("bar")
19
+ end
20
+
21
+ it "should be a global replace" do
22
+ data = { :foo => "bar" }
23
+ expect(Tmplt.render("{{ foo }} {{ foo }}", data)).to eq("bar bar")
24
+ end
25
+
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("")
29
+ end
30
+
31
+ it "should allow numeric tag names" do
32
+ data = { 0 => "foo" }
33
+ expect(Tmplt.render("{{ 0 }}", data)).to eq("foo")
34
+ end
35
+
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")
40
+ end
41
+
42
+ it "should match the string key and not the symbol key if the two keys have the same name" do
43
+ data = {
44
+ :foo => "bar",
45
+ "foo" => "baz"
46
+ }
47
+ expect(Tmplt.render("{{ foo }}", data)).to eq("baz")
48
+ end
49
+
50
+ it "should match the symbol key and not the string key if the tag name is prefixed with ':'" do
51
+ data = {
52
+ :foo => "bar",
53
+ "foo" => "baz"
54
+ }
55
+ expect(Tmplt.render("{{ :foo }}", data)).to eq("bar")
56
+ end
57
+
58
+ it "should interpolate numeric values" do
59
+ data = {
60
+ :foo => 3.14,
61
+ :bar => 159,
62
+ }
63
+ expect(Tmplt.render("{{ foo }}{{ bar }}", data)).to eq("3.14159")
64
+ end
65
+
66
+ it "should interpolate Proc values" do
67
+ data = {
68
+ :foo => "qux",
69
+ :bar => "quux",
70
+ :baz => Proc.new {|d| "#{d[:foo]} #{d[:bar]}" }
71
+ }
72
+ expect(Tmplt.render("{{ baz }}", data)).to eq("qux quux")
73
+ end
74
+
75
+ it "should interpolate nested keys" do
76
+ data = {
77
+ :foo => {
78
+ "bar baz" => {
79
+ :qux => "quux"
80
+ }
81
+ }
82
+ }
83
+ expect(Tmplt.render("{{ foo.bar baz.qux }}", data)).to eql("quux")
84
+ expect(Tmplt.render("{{foo . bar baz . qux}}", data)).to eql("quux")
85
+ end
86
+
87
+ end
metadata ADDED
@@ -0,0 +1,106 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: tmplt
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - yuanqing
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2014-07-15 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ~>
18
+ - !ruby/object:Gem::Version
19
+ version: '1.3'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ~>
25
+ - !ruby/object:Gem::Version
26
+ version: '1.3'
27
+ - !ruby/object:Gem::Dependency
28
+ name: coveralls
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - '>='
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - '>='
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rake
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - '>='
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - '>='
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: rspec
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - '>='
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - '>='
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
69
+ description:
70
+ email:
71
+ - hello@yuanqing.sg
72
+ executables: []
73
+ extensions: []
74
+ extra_rdoc_files: []
75
+ files:
76
+ - README.md
77
+ - lib/tmplt.rb
78
+ - spec/spec_helper.rb
79
+ - spec/tmplt_spec.rb
80
+ homepage: https://github.com/yuanqing/tmplt
81
+ licenses:
82
+ - MIT
83
+ metadata: {}
84
+ post_install_message:
85
+ rdoc_options: []
86
+ require_paths:
87
+ - lib
88
+ required_ruby_version: !ruby/object:Gem::Requirement
89
+ requirements:
90
+ - - '>='
91
+ - !ruby/object:Gem::Version
92
+ version: '0'
93
+ required_rubygems_version: !ruby/object:Gem::Requirement
94
+ requirements:
95
+ - - '>='
96
+ - !ruby/object:Gem::Version
97
+ version: '0'
98
+ requirements: []
99
+ rubyforge_project:
100
+ rubygems_version: 2.1.11
101
+ signing_key:
102
+ specification_version: 4
103
+ summary: Interpolate values from a hash into a template string.
104
+ test_files:
105
+ - spec/spec_helper.rb
106
+ - spec/tmplt_spec.rb