stach 1.0.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/README.md +107 -0
- data/Rakefile +12 -0
- data/lib/stach.rb +14 -0
- data/lib/stach/version.rb +3 -0
- data/lib/stach/version_spec.rb +7 -0
- data/lib/stach_spec.rb +42 -0
- metadata +120 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: af2a5896e907dfe22340689a572dfd579232e616
|
4
|
+
data.tar.gz: b8b5306fa106e99c79ac2e58155f5b44e3a5e7bf
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: b11776e29917648a5b2bb262bf279e901a60893df1411e7880fb517514f75e1ff8888e1772b204ecb87897529a5be9ad33aa42aa90ae7d5bbf7d4b8552c85f1c
|
7
|
+
data.tar.gz: ca18eade8aa5ee51d531a5b053d61950594ea35e00b734f07efa3b127bfee84ed0e4ab07d95862cc558c4eea0c7150fb16090386387819cf319c564f80698e68
|
data/README.md
ADDED
@@ -0,0 +1,107 @@
|
|
1
|
+
# stach
|
2
|
+
|
3
|
+
- [![Build](http://img.shields.io/travis-ci/krainboltgreene/stach.svg?style=flat-square)](https://travis-ci.org/krainboltgreene/stach)
|
4
|
+
- [![Downloads](http://img.shields.io/gem/dtv/stach.svg?style=flat-square)](https://rubygems.org/gems/stach)
|
5
|
+
- [![Version](http://img.shields.io/gem/v/stach.svg?style=flat-square)](https://rubygems.org/gems/stach)
|
6
|
+
|
7
|
+
|
8
|
+
A micro-mustache rendering engine.
|
9
|
+
|
10
|
+
|
11
|
+
## Using
|
12
|
+
|
13
|
+
First require the library if you need to:
|
14
|
+
|
15
|
+
``` ruby
|
16
|
+
require "stach"
|
17
|
+
```
|
18
|
+
|
19
|
+
Now have a interpolation string ready:
|
20
|
+
|
21
|
+
``` ruby
|
22
|
+
content = "Hello {{name}}, my name is computer. I see that you are {{age}} years old!"
|
23
|
+
```
|
24
|
+
|
25
|
+
And now some data:
|
26
|
+
|
27
|
+
``` ruby
|
28
|
+
data = {name: "Kurtis Rainbolt-Greene", age: 30}
|
29
|
+
```
|
30
|
+
|
31
|
+
Lets setup our render:
|
32
|
+
|
33
|
+
``` ruby
|
34
|
+
renderer = Stach.new(content, data)
|
35
|
+
```
|
36
|
+
|
37
|
+
That alone is not enough, we need to realize it:
|
38
|
+
|
39
|
+
``` ruby
|
40
|
+
renderer.to_s
|
41
|
+
```
|
42
|
+
|
43
|
+
That would return the below string:
|
44
|
+
|
45
|
+
``` ruby
|
46
|
+
"Hello Kurtis Rainbolt-Greene, my name is computer. I see you are 30 years old!"
|
47
|
+
```
|
48
|
+
|
49
|
+
Since it's all about calling `to_s` you can also do this:
|
50
|
+
|
51
|
+
|
52
|
+
``` ruby
|
53
|
+
puts "Johny the robot says, '#{renderer}'"
|
54
|
+
```
|
55
|
+
|
56
|
+
or this:
|
57
|
+
|
58
|
+
``` ruby
|
59
|
+
puts "Johny the robot says, '#{Stach.new(content, data)}'"
|
60
|
+
```
|
61
|
+
|
62
|
+
Which would output:
|
63
|
+
|
64
|
+
```
|
65
|
+
Johny the robot says, 'Hello Kurtis Rainbolt-Greene, my name is computer. I see you are 30 years old!'
|
66
|
+
```
|
67
|
+
|
68
|
+
Here's how this all looks together:
|
69
|
+
|
70
|
+
``` ruby
|
71
|
+
require "stach"
|
72
|
+
|
73
|
+
content = "Hello {{name}}, my name is computer. I see that you are {{age}} years old!"
|
74
|
+
|
75
|
+
data = {name: "Kurtis Rainbolt-Greene", age: 30}
|
76
|
+
|
77
|
+
renderer = Stach.new(content, data)
|
78
|
+
|
79
|
+
puts "Johny the robot says, '#{renderer}'"
|
80
|
+
|
81
|
+
puts "Johny the robot says, '#{Stach.new(content, data)}'"
|
82
|
+
```
|
83
|
+
|
84
|
+
|
85
|
+
## Installing
|
86
|
+
|
87
|
+
Add this line to your application's Gemfile:
|
88
|
+
|
89
|
+
gem "stach", "1.0.0"
|
90
|
+
|
91
|
+
And then execute:
|
92
|
+
|
93
|
+
$ bundle
|
94
|
+
|
95
|
+
Or install it yourself with:
|
96
|
+
|
97
|
+
$ gem install stach
|
98
|
+
|
99
|
+
|
100
|
+
## Contributing
|
101
|
+
|
102
|
+
1. Read the [Code of Conduct](/CONDUCT.md)
|
103
|
+
2. Fork it
|
104
|
+
3. Create your feature branch (`git checkout -b my-new-feature`)
|
105
|
+
4. Commit your changes (`git commit -am 'Add some feature'`)
|
106
|
+
5. Push to the branch (`git push origin my-new-feature`)
|
107
|
+
6. Create new Pull Request
|
data/Rakefile
ADDED
@@ -0,0 +1,12 @@
|
|
1
|
+
#!/usr/bin/env rake
|
2
|
+
|
3
|
+
require "bundler/gem_tasks"
|
4
|
+
require "rspec/core/rake_task"
|
5
|
+
|
6
|
+
desc "Run all the tests in spec"
|
7
|
+
RSpec::Core::RakeTask.new(:spec) do |let|
|
8
|
+
let.pattern = "lib/**{,/*/**}/*_spec.rb"
|
9
|
+
end
|
10
|
+
|
11
|
+
desc "Default: run tests"
|
12
|
+
task default: :spec
|
data/lib/stach.rb
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
class Stach
|
2
|
+
require_relative "stach/version"
|
3
|
+
|
4
|
+
def initialize(template = "", record = {})
|
5
|
+
@template = template || ""
|
6
|
+
@record = record || {}
|
7
|
+
end
|
8
|
+
|
9
|
+
def to_s
|
10
|
+
@record.reduce(@template) do |content, (key, value)|
|
11
|
+
content.gsub(/\{\{#{key}\}\}/, value)
|
12
|
+
end
|
13
|
+
end
|
14
|
+
end
|
data/lib/stach_spec.rb
ADDED
@@ -0,0 +1,42 @@
|
|
1
|
+
require "spec_helper"
|
2
|
+
|
3
|
+
RSpec.describe Stach do
|
4
|
+
let(:renderer) { Stach.new(template, record) }
|
5
|
+
let(:render) { renderer.to_s }
|
6
|
+
|
7
|
+
context "when record is empty and the template has interpolation markers" do
|
8
|
+
let(:template) { "Hello {{world}}" }
|
9
|
+
let(:record) { {} }
|
10
|
+
|
11
|
+
it "returns an un-changed string" do
|
12
|
+
expect(render).to eq(template)
|
13
|
+
end
|
14
|
+
end
|
15
|
+
|
16
|
+
context "when record has data and the template has no interpolation markers" do
|
17
|
+
let(:template) { "Hello world" }
|
18
|
+
let(:record) { {world: "test"} }
|
19
|
+
|
20
|
+
it "returns an un-changed string" do
|
21
|
+
expect(render).to eq(template)
|
22
|
+
end
|
23
|
+
end
|
24
|
+
|
25
|
+
context "when record has data and template has matching interpolation keys" do
|
26
|
+
let(:template) { "Hello {{world}}" }
|
27
|
+
let(:record) { {world: "test"} }
|
28
|
+
|
29
|
+
it "returns a changed string" do
|
30
|
+
expect(render).to eq("Hello test")
|
31
|
+
end
|
32
|
+
end
|
33
|
+
|
34
|
+
context "when record has data and template has non-matching interpolation keys" do
|
35
|
+
let(:template) { "Hello {{world}} {{x}}" }
|
36
|
+
let(:record) { {world: "test"} }
|
37
|
+
|
38
|
+
it "returns a partially changed string" do
|
39
|
+
expect(render).to eq("Hello test {{x}}")
|
40
|
+
end
|
41
|
+
end
|
42
|
+
end
|
metadata
ADDED
@@ -0,0 +1,120 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: stach
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 1.0.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Kurtis Rainbolt-Greene
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2017-11-13 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.15.4
|
20
|
+
type: :development
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - '='
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: 1.15.4
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: rspec
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - '='
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: 3.7.0
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - '='
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: 3.7.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: 12.2.1
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - '='
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: 12.2.1
|
55
|
+
- !ruby/object:Gem::Dependency
|
56
|
+
name: pry
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - '='
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: 0.11.2
|
62
|
+
type: :development
|
63
|
+
prerelease: false
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - '='
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: 0.11.2
|
69
|
+
- !ruby/object:Gem::Dependency
|
70
|
+
name: pry-doc
|
71
|
+
requirement: !ruby/object:Gem::Requirement
|
72
|
+
requirements:
|
73
|
+
- - '='
|
74
|
+
- !ruby/object:Gem::Version
|
75
|
+
version: 0.11.1
|
76
|
+
type: :development
|
77
|
+
prerelease: false
|
78
|
+
version_requirements: !ruby/object:Gem::Requirement
|
79
|
+
requirements:
|
80
|
+
- - '='
|
81
|
+
- !ruby/object:Gem::Version
|
82
|
+
version: 0.11.1
|
83
|
+
description: A micro-mustache rendering engine.
|
84
|
+
email:
|
85
|
+
- kurtis@rainbolt-greene.online
|
86
|
+
executables: []
|
87
|
+
extensions: []
|
88
|
+
extra_rdoc_files: []
|
89
|
+
files:
|
90
|
+
- README.md
|
91
|
+
- Rakefile
|
92
|
+
- lib/stach.rb
|
93
|
+
- lib/stach/version.rb
|
94
|
+
- lib/stach/version_spec.rb
|
95
|
+
- lib/stach_spec.rb
|
96
|
+
homepage: http://krainboltgreene.github.io/stach
|
97
|
+
licenses:
|
98
|
+
- ISC
|
99
|
+
metadata: {}
|
100
|
+
post_install_message:
|
101
|
+
rdoc_options: []
|
102
|
+
require_paths:
|
103
|
+
- lib
|
104
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
105
|
+
requirements:
|
106
|
+
- - ">="
|
107
|
+
- !ruby/object:Gem::Version
|
108
|
+
version: '0'
|
109
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
110
|
+
requirements:
|
111
|
+
- - ">="
|
112
|
+
- !ruby/object:Gem::Version
|
113
|
+
version: '0'
|
114
|
+
requirements: []
|
115
|
+
rubyforge_project:
|
116
|
+
rubygems_version: 2.6.13
|
117
|
+
signing_key:
|
118
|
+
specification_version: 4
|
119
|
+
summary: A micro-mustache rendering engine.
|
120
|
+
test_files: []
|