tiny_template 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 +7 -0
- data/.gitignore +14 -0
- data/Gemfile +4 -0
- data/LICENSE.txt +22 -0
- data/README.md +63 -0
- data/Rakefile +2 -0
- data/lib/tiny_template/core_ext.rb +13 -0
- data/lib/tiny_template/version.rb +3 -0
- data/lib/tiny_template.rb +53 -0
- data/spec/spec_helper.rb +6 -0
- data/spec/tiny_template_spec.rb +57 -0
- data/tiny_template.gemspec +27 -0
- metadata +126 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: e5ede3dedd284a99cf689b33fd15498ba93343b3
|
4
|
+
data.tar.gz: 0193eb19023980520cad7bd2e0c817ee5217c3d6
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 9909cd0fd8bb5ca4c44ae0db9d39405d467eeb18fa1f3c94c48c8bb7d5678b1488a947df4ffd925ed52bb79b6d0bfe70bd08d4e0f45f966ecb2b9c943a256fd3
|
7
|
+
data.tar.gz: d2d80338256cc5a0b041b5d4c69210c4fb040739c5bc7c6c02c8168aa1bf61a210d2b6b5f27b84027b2395980e1ea8cb9e37fa44593467586f489548ee499607
|
data/.gitignore
ADDED
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2015 TODO: Write your name
|
2
|
+
|
3
|
+
MIT License
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
6
|
+
a copy of this software and associated documentation files (the
|
7
|
+
"Software"), to deal in the Software without restriction, including
|
8
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
9
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
10
|
+
permit persons to whom the Software is furnished to do so, subject to
|
11
|
+
the following conditions:
|
12
|
+
|
13
|
+
The above copyright notice and this permission notice shall be
|
14
|
+
included in all copies or substantial portions of the Software.
|
15
|
+
|
16
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
17
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
18
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
19
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
20
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
21
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
22
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,63 @@
|
|
1
|
+
# TinyTemplate
|
2
|
+
|
3
|
+
This is a very simple template engine that allows you to interpolate method chains without passing it any data. TinyTemplate will use the `self` of the caller when no context is passed.
|
4
|
+
|
5
|
+
## Installation
|
6
|
+
|
7
|
+
Add this line to your application's Gemfile:
|
8
|
+
|
9
|
+
```ruby
|
10
|
+
gem 'tiny_template'
|
11
|
+
```
|
12
|
+
|
13
|
+
And then execute:
|
14
|
+
|
15
|
+
$ bundle
|
16
|
+
|
17
|
+
Or install it yourself as:
|
18
|
+
|
19
|
+
$ gem install tiny_template
|
20
|
+
|
21
|
+
## Usage
|
22
|
+
|
23
|
+
You just have to prepend your strings with the ~ operator to parse them with the methods of the current self :
|
24
|
+
|
25
|
+
```ruby
|
26
|
+
class Greeting
|
27
|
+
def hello
|
28
|
+
~"Hello {{client.name.upcase}} and welcome to {{configuration.title.capitalize}}.
|
29
|
+
Your email is {{client.email.downcase.strip}}."
|
30
|
+
end
|
31
|
+
|
32
|
+
def client
|
33
|
+
OpenStruct.new(name: 'John Doe', email: 'John@doe.com')
|
34
|
+
end
|
35
|
+
|
36
|
+
def configuration
|
37
|
+
OpenStruct.new(title: 'my fancy website')
|
38
|
+
end
|
39
|
+
end
|
40
|
+
|
41
|
+
Greeting.new.hello
|
42
|
+
|
43
|
+
# => "Hello JOHN DOE and welcome to My fancy website. Your email is john@doe.com."
|
44
|
+
```
|
45
|
+
|
46
|
+
|
47
|
+
|
48
|
+
If you don't like the binding magic used here, you can use TinyTemplate with the classic syntax :
|
49
|
+
|
50
|
+
```ruby
|
51
|
+
template = "Hello {{client.name.upcase}} and welcome to {{configuration.title.capitalize}}.
|
52
|
+
Your email is {{client.email.downcase.strip}}."
|
53
|
+
|
54
|
+
TinyTemplate.parse(template, self)
|
55
|
+
````
|
56
|
+
|
57
|
+
## Contributing
|
58
|
+
|
59
|
+
1. Fork it ( https://github.com/[my-github-username]/tiny_template/fork )
|
60
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
61
|
+
3. Commit your changes (`git commit -am 'Add some feature'`)
|
62
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
63
|
+
5. Create a new Pull Request
|
data/Rakefile
ADDED
@@ -0,0 +1,53 @@
|
|
1
|
+
require "tiny_template/version"
|
2
|
+
require 'debug_inspector'
|
3
|
+
require "tiny_template/core_ext"
|
4
|
+
|
5
|
+
class TinyTemplate
|
6
|
+
# You can construct by your sulf this class, but you wont benefict from the magic
|
7
|
+
# of the context being automaticaly calculated with debug_inspector
|
8
|
+
def initialize(str)
|
9
|
+
self.str = str
|
10
|
+
end
|
11
|
+
|
12
|
+
# Context can be any object that you expect it will respond to methods on the template passed
|
13
|
+
# to the constructor
|
14
|
+
def parse(context)
|
15
|
+
str.gsub(/\{\{\w+(\.\w+)*\}\}/) do |match|
|
16
|
+
interpolate(match, context)
|
17
|
+
end
|
18
|
+
end
|
19
|
+
|
20
|
+
# This is a facility class method aimed to simplify the construction
|
21
|
+
def self.parse(str, context)
|
22
|
+
new(str).parse(context)
|
23
|
+
end
|
24
|
+
|
25
|
+
private
|
26
|
+
attr_accessor :str
|
27
|
+
|
28
|
+
# Private method used to interpolate one single expression at a time
|
29
|
+
def interpolate(match, context)
|
30
|
+
match.gsub(/\{|\}/, '')
|
31
|
+
.split('.')
|
32
|
+
.inject(context){ |result, e| result.public_send(e) }
|
33
|
+
|
34
|
+
rescue
|
35
|
+
match
|
36
|
+
end
|
37
|
+
|
38
|
+
module Utils
|
39
|
+
# Call this method with or without context. If no context is passed, the context of the caller
|
40
|
+
# will be computed and passed to a new instance of the class TinyTemplate
|
41
|
+
def TinyTemplate(str, context = nil)
|
42
|
+
unless context
|
43
|
+
RubyVM::DebugInspector.open do |inspector|
|
44
|
+
context = eval('self', inspector.frame_binding(2))
|
45
|
+
end
|
46
|
+
end
|
47
|
+
|
48
|
+
TinyTemplate.parse(str, context)
|
49
|
+
end
|
50
|
+
end
|
51
|
+
end
|
52
|
+
|
53
|
+
include TinyTemplate::Utils
|
data/spec/spec_helper.rb
ADDED
@@ -0,0 +1,57 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
require 'ostruct'
|
3
|
+
|
4
|
+
RSpec.describe TinyTemplate do
|
5
|
+
before(:each) do
|
6
|
+
@klass = Class.new do
|
7
|
+
def hello
|
8
|
+
TinyTemplate.parse(template, self)
|
9
|
+
end
|
10
|
+
|
11
|
+
def template
|
12
|
+
"Hello {{client.name.upcase}} and welcome to {{configuration.title.capitalize}}. Your email is {{client.email.downcase.strip}}."
|
13
|
+
end
|
14
|
+
|
15
|
+
def client
|
16
|
+
OpenStruct.new(name: 'John Doe', email: ' John@doe.com ')
|
17
|
+
end
|
18
|
+
|
19
|
+
def configuration
|
20
|
+
OpenStruct.new(title: 'my Fancy website')
|
21
|
+
end
|
22
|
+
end
|
23
|
+
end
|
24
|
+
|
25
|
+
it "should interpolate strings correctly" do
|
26
|
+
result = @klass.new.hello
|
27
|
+
|
28
|
+
expect(result).to match(/JOHN DOE/)
|
29
|
+
expect(result).to match(/My fancy website/)
|
30
|
+
expect(result).to match(/john@doe.com/)
|
31
|
+
end
|
32
|
+
|
33
|
+
it "should ignore non existing methods" do
|
34
|
+
@klass.class_eval do
|
35
|
+
def template
|
36
|
+
"Hello {{client.name.inexisting_method}}"
|
37
|
+
end
|
38
|
+
end
|
39
|
+
|
40
|
+
result = @klass.new.hello
|
41
|
+
|
42
|
+
expect(result).to match(/\{\{client\.name\.inexisting_method\}\}/)
|
43
|
+
end
|
44
|
+
|
45
|
+
it "should find the context of caller when no context is passed" do
|
46
|
+
@klass.class_eval do
|
47
|
+
def hello
|
48
|
+
TinyTemplate(template)
|
49
|
+
end
|
50
|
+
end
|
51
|
+
|
52
|
+
result = @klass.new.hello
|
53
|
+
|
54
|
+
expect(result).to match(/JOHN DOE/)
|
55
|
+
end
|
56
|
+
end
|
57
|
+
|
@@ -0,0 +1,27 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'tiny_template/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = "tiny_template"
|
8
|
+
spec.version = TinyTemplate::VERSION
|
9
|
+
spec.authors = ["Ihcène Medjber (ihcene)"]
|
10
|
+
spec.email = ["i@aritylabs.com"]
|
11
|
+
spec.summary = %q{Magical and secure string interpolation.}
|
12
|
+
spec.description = %q{A very simple template engine that allows you to interpolate method chains without passing it any data}
|
13
|
+
spec.homepage = "http://github.com/ihcene/tiny_template"
|
14
|
+
spec.license = "MIT"
|
15
|
+
spec.required_ruby_version = '>= 2.0.0'
|
16
|
+
|
17
|
+
spec.files = `git ls-files -z`.split("\x0")
|
18
|
+
spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
|
19
|
+
spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
|
20
|
+
spec.require_paths = ["lib"]
|
21
|
+
|
22
|
+
spec.add_runtime_dependency "debug_inspector", '~> 0.0', '>= 0.0.2'
|
23
|
+
|
24
|
+
spec.add_development_dependency "bundler", "~> 1.7"
|
25
|
+
spec.add_development_dependency "rake", "~> 10.0"
|
26
|
+
spec.add_development_dependency 'rspec', '~> 3.2', '>= 3.2.0'
|
27
|
+
end
|
metadata
ADDED
@@ -0,0 +1,126 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: tiny_template
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Ihcène Medjber (ihcene)
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2015-03-22 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: debug_inspector
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - "~>"
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '0.0'
|
20
|
+
- - ">="
|
21
|
+
- !ruby/object:Gem::Version
|
22
|
+
version: 0.0.2
|
23
|
+
type: :runtime
|
24
|
+
prerelease: false
|
25
|
+
version_requirements: !ruby/object:Gem::Requirement
|
26
|
+
requirements:
|
27
|
+
- - "~>"
|
28
|
+
- !ruby/object:Gem::Version
|
29
|
+
version: '0.0'
|
30
|
+
- - ">="
|
31
|
+
- !ruby/object:Gem::Version
|
32
|
+
version: 0.0.2
|
33
|
+
- !ruby/object:Gem::Dependency
|
34
|
+
name: bundler
|
35
|
+
requirement: !ruby/object:Gem::Requirement
|
36
|
+
requirements:
|
37
|
+
- - "~>"
|
38
|
+
- !ruby/object:Gem::Version
|
39
|
+
version: '1.7'
|
40
|
+
type: :development
|
41
|
+
prerelease: false
|
42
|
+
version_requirements: !ruby/object:Gem::Requirement
|
43
|
+
requirements:
|
44
|
+
- - "~>"
|
45
|
+
- !ruby/object:Gem::Version
|
46
|
+
version: '1.7'
|
47
|
+
- !ruby/object:Gem::Dependency
|
48
|
+
name: rake
|
49
|
+
requirement: !ruby/object:Gem::Requirement
|
50
|
+
requirements:
|
51
|
+
- - "~>"
|
52
|
+
- !ruby/object:Gem::Version
|
53
|
+
version: '10.0'
|
54
|
+
type: :development
|
55
|
+
prerelease: false
|
56
|
+
version_requirements: !ruby/object:Gem::Requirement
|
57
|
+
requirements:
|
58
|
+
- - "~>"
|
59
|
+
- !ruby/object:Gem::Version
|
60
|
+
version: '10.0'
|
61
|
+
- !ruby/object:Gem::Dependency
|
62
|
+
name: rspec
|
63
|
+
requirement: !ruby/object:Gem::Requirement
|
64
|
+
requirements:
|
65
|
+
- - "~>"
|
66
|
+
- !ruby/object:Gem::Version
|
67
|
+
version: '3.2'
|
68
|
+
- - ">="
|
69
|
+
- !ruby/object:Gem::Version
|
70
|
+
version: 3.2.0
|
71
|
+
type: :development
|
72
|
+
prerelease: false
|
73
|
+
version_requirements: !ruby/object:Gem::Requirement
|
74
|
+
requirements:
|
75
|
+
- - "~>"
|
76
|
+
- !ruby/object:Gem::Version
|
77
|
+
version: '3.2'
|
78
|
+
- - ">="
|
79
|
+
- !ruby/object:Gem::Version
|
80
|
+
version: 3.2.0
|
81
|
+
description: A very simple template engine that allows you to interpolate method chains
|
82
|
+
without passing it any data
|
83
|
+
email:
|
84
|
+
- i@aritylabs.com
|
85
|
+
executables: []
|
86
|
+
extensions: []
|
87
|
+
extra_rdoc_files: []
|
88
|
+
files:
|
89
|
+
- ".gitignore"
|
90
|
+
- Gemfile
|
91
|
+
- LICENSE.txt
|
92
|
+
- README.md
|
93
|
+
- Rakefile
|
94
|
+
- lib/tiny_template.rb
|
95
|
+
- lib/tiny_template/core_ext.rb
|
96
|
+
- lib/tiny_template/version.rb
|
97
|
+
- spec/spec_helper.rb
|
98
|
+
- spec/tiny_template_spec.rb
|
99
|
+
- tiny_template.gemspec
|
100
|
+
homepage: http://github.com/ihcene/tiny_template
|
101
|
+
licenses:
|
102
|
+
- MIT
|
103
|
+
metadata: {}
|
104
|
+
post_install_message:
|
105
|
+
rdoc_options: []
|
106
|
+
require_paths:
|
107
|
+
- lib
|
108
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
109
|
+
requirements:
|
110
|
+
- - ">="
|
111
|
+
- !ruby/object:Gem::Version
|
112
|
+
version: 2.0.0
|
113
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
114
|
+
requirements:
|
115
|
+
- - ">="
|
116
|
+
- !ruby/object:Gem::Version
|
117
|
+
version: '0'
|
118
|
+
requirements: []
|
119
|
+
rubyforge_project:
|
120
|
+
rubygems_version: 2.2.2
|
121
|
+
signing_key:
|
122
|
+
specification_version: 4
|
123
|
+
summary: Magical and secure string interpolation.
|
124
|
+
test_files:
|
125
|
+
- spec/spec_helper.rb
|
126
|
+
- spec/tiny_template_spec.rb
|