braai 1.2.2 → 1.3.0
Sign up to get free protection for your applications and to get access to all the features.
- data/Gemfile +1 -1
- data/Gemfile.lock +6 -12
- data/README.md +4 -4
- data/Rakefile +9 -4
- data/lib/braai/matchers.rb +16 -3
- data/lib/braai/version.rb +1 -1
- data/spec/braai/context_spec.rb +5 -2
- data/spec/braai/matchers_spec.rb +15 -15
- data/spec/braai/readme_spec.rb +11 -26
- data/spec/braai/template_spec.rb +48 -32
- data/spec/spec_helper.rb +6 -3
- metadata +3 -3
data/Gemfile
CHANGED
data/Gemfile.lock
CHANGED
@@ -1,30 +1,24 @@
|
|
1
1
|
PATH
|
2
2
|
remote: .
|
3
3
|
specs:
|
4
|
-
braai (1.
|
4
|
+
braai (1.3.0)
|
5
5
|
activesupport
|
6
6
|
|
7
7
|
GEM
|
8
8
|
remote: https://rubygems.org/
|
9
9
|
specs:
|
10
|
-
activesupport (3.2.
|
10
|
+
activesupport (3.2.11)
|
11
11
|
i18n (~> 0.6)
|
12
12
|
multi_json (~> 1.0)
|
13
|
-
diff-lcs (1.1.3)
|
14
13
|
i18n (0.6.1)
|
14
|
+
minitest (2.12.1)
|
15
|
+
minitest-colorize (0.0.4)
|
16
|
+
minitest (~> 2.0)
|
15
17
|
multi_json (1.5.0)
|
16
|
-
rspec (2.11.0)
|
17
|
-
rspec-core (~> 2.11.0)
|
18
|
-
rspec-expectations (~> 2.11.0)
|
19
|
-
rspec-mocks (~> 2.11.0)
|
20
|
-
rspec-core (2.11.1)
|
21
|
-
rspec-expectations (2.11.2)
|
22
|
-
diff-lcs (~> 1.1.3)
|
23
|
-
rspec-mocks (2.11.2)
|
24
18
|
|
25
19
|
PLATFORMS
|
26
20
|
ruby
|
27
21
|
|
28
22
|
DEPENDENCIES
|
29
23
|
braai!
|
30
|
-
|
24
|
+
minitest-colorize
|
data/README.md
CHANGED
@@ -28,7 +28,7 @@ The first matcher is a simple <code>to_s</code> matcher. It will match a single
|
|
28
28
|
```ruby
|
29
29
|
template = "Hi {{ name }}!"
|
30
30
|
response = Braai::Template.new(template).render(name: "Mark")
|
31
|
-
response.
|
31
|
+
response.must_equal "Hi Mark!"
|
32
32
|
```
|
33
33
|
|
34
34
|
The second matcher will call a method on the variable.
|
@@ -36,7 +36,7 @@ The second matcher will call a method on the variable.
|
|
36
36
|
```ruby
|
37
37
|
template = "Hi {{ name.upcase }}!"
|
38
38
|
response = Braai::Template.new(template).render(name: "Mark")
|
39
|
-
response.
|
39
|
+
response.must_equal "Hi MARK!"
|
40
40
|
```
|
41
41
|
|
42
42
|
### Custom Matchers
|
@@ -54,7 +54,7 @@ Braai::Template.map(/({{\s*name\s*}})/i) do |template, key, matches|
|
|
54
54
|
end
|
55
55
|
|
56
56
|
response = Braai::Template.new(template).render(name: "mark")
|
57
|
-
response.
|
57
|
+
response.must_equal "I'm MARK and Damn, I love BBQ!!"
|
58
58
|
```
|
59
59
|
|
60
60
|
### For Loops
|
@@ -98,5 +98,5 @@ res.should match("<h2>MARK</h2>")
|
|
98
98
|
## Contributers
|
99
99
|
|
100
100
|
* Mark Bates
|
101
|
-
* Kevin Incorvia
|
102
101
|
* Nick Plante
|
102
|
+
* Kevin Incorvia
|
data/Rakefile
CHANGED
@@ -1,7 +1,12 @@
|
|
1
1
|
require 'bundler'
|
2
2
|
Bundler::GemHelper.install_tasks
|
3
3
|
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
|
4
|
+
require 'rake/testtask'
|
5
|
+
|
6
|
+
Rake::TestTask.new(:default) do |t|
|
7
|
+
t.libs << "spec"
|
8
|
+
t.pattern = "spec/**/*_spec.rb"
|
9
|
+
end
|
10
|
+
|
11
|
+
task :spec => :default
|
12
|
+
task :test => :default
|
data/lib/braai/matchers.rb
CHANGED
@@ -32,9 +32,22 @@ module Braai::Matchers
|
|
32
32
|
res.join("\n")
|
33
33
|
}
|
34
34
|
|
35
|
-
@matchers[/({{\s*([\w
|
36
|
-
|
37
|
-
|
35
|
+
@matchers[/({{\s*([\w\.]+)\s*}})/i] = ->(template, key, matches) {
|
36
|
+
chain = matches[1].split('.')
|
37
|
+
value = template.attributes[chain.shift]
|
38
|
+
return nil unless value
|
39
|
+
|
40
|
+
chain.each do |a|
|
41
|
+
if value.respond_to?(a.to_sym)
|
42
|
+
value = value.send(a)
|
43
|
+
elsif value.is_a?(Hash)
|
44
|
+
value = value[a.to_sym] || value[a.to_s]
|
45
|
+
else
|
46
|
+
return nil
|
47
|
+
end
|
48
|
+
end
|
49
|
+
|
50
|
+
value
|
38
51
|
}
|
39
52
|
|
40
53
|
@matchers[/({{\s*([\w]+)\s*}})/i] = ->(template, key, matches) {
|
data/lib/braai/version.rb
CHANGED
data/spec/braai/context_spec.rb
CHANGED
@@ -8,8 +8,11 @@ describe Braai::Context do
|
|
8
8
|
|
9
9
|
it 'allows a nested hash' do
|
10
10
|
context = Braai::Context.new("template", "matchers", { hash: { foo: 'bar' } })
|
11
|
-
context.attributes[:hash].
|
11
|
+
context.attributes[:hash].must_equal("foo" => 'bar')
|
12
12
|
end
|
13
|
+
|
13
14
|
end
|
15
|
+
|
14
16
|
end
|
15
|
-
|
17
|
+
|
18
|
+
end
|
data/spec/braai/matchers_spec.rb
CHANGED
@@ -2,40 +2,40 @@ require 'spec_helper'
|
|
2
2
|
|
3
3
|
describe Braai::Matchers do
|
4
4
|
include Braai::Matchers
|
5
|
-
|
5
|
+
|
6
6
|
describe 'map' do
|
7
7
|
|
8
8
|
it "maps a new matcher" do
|
9
9
|
map("foo") {}
|
10
|
-
matchers.
|
10
|
+
matchers.must_include("foo")
|
11
11
|
end
|
12
12
|
|
13
13
|
it "makes the latest matcher the first matcher in the list" do
|
14
14
|
map("foo") {}
|
15
15
|
map("bar") {}
|
16
|
-
matchers.keys.first.
|
16
|
+
matchers.keys.first.must_equal("bar")
|
17
17
|
end
|
18
|
-
|
18
|
+
|
19
19
|
end
|
20
20
|
|
21
21
|
describe 'unmap' do
|
22
|
-
|
22
|
+
|
23
23
|
it "unmaps a matcher" do
|
24
24
|
map("foo") {}
|
25
|
-
matchers.
|
25
|
+
matchers.must_include("foo")
|
26
26
|
unmap("foo")
|
27
|
-
matchers.
|
27
|
+
matchers.wont_include("foo")
|
28
28
|
end
|
29
29
|
|
30
30
|
end
|
31
31
|
|
32
32
|
describe 'clear!' do
|
33
|
-
|
33
|
+
|
34
34
|
it "removes all of the matchers" do
|
35
35
|
map("foo") {}
|
36
|
-
matchers.
|
36
|
+
matchers.wont_be_empty
|
37
37
|
clear!
|
38
|
-
matchers.
|
38
|
+
matchers.must_be_empty
|
39
39
|
end
|
40
40
|
|
41
41
|
end
|
@@ -43,11 +43,11 @@ describe Braai::Matchers do
|
|
43
43
|
describe 'reset!' do
|
44
44
|
|
45
45
|
it "resets the matchers to their original state" do
|
46
|
-
matchers.
|
46
|
+
matchers.size.must_equal(3)
|
47
47
|
map("foo") {}
|
48
|
-
matchers.
|
48
|
+
matchers.size.must_equal(4)
|
49
49
|
reset!
|
50
|
-
matchers.
|
50
|
+
matchers.size.must_equal(3)
|
51
51
|
end
|
52
52
|
|
53
53
|
end
|
@@ -57,9 +57,9 @@ describe Braai::Matchers do
|
|
57
57
|
it "installs the three base matchers" do
|
58
58
|
clear!
|
59
59
|
map("foo") {}
|
60
|
-
matchers.
|
60
|
+
matchers.size.must_equal(1)
|
61
61
|
set_defaults
|
62
|
-
matchers.
|
62
|
+
matchers.size.must_equal(4)
|
63
63
|
end
|
64
64
|
|
65
65
|
end
|
data/spec/braai/readme_spec.rb
CHANGED
@@ -5,13 +5,13 @@ describe 'README' do
|
|
5
5
|
it "simple example" do
|
6
6
|
template = "Hi {{ name }}!"
|
7
7
|
response = Braai::Template.new(template).render(name: "Mark")
|
8
|
-
response.
|
8
|
+
response.must_equal "Hi Mark!"
|
9
9
|
end
|
10
10
|
|
11
11
|
it "simple method call example" do
|
12
12
|
template = "Hi {{ name.upcase }}!"
|
13
13
|
response = Braai::Template.new(template).render(name: "Mark")
|
14
|
-
response.
|
14
|
+
response.must_equal "Hi MARK!"
|
15
15
|
end
|
16
16
|
|
17
17
|
it "custom matcher example" do
|
@@ -25,7 +25,7 @@ describe 'README' do
|
|
25
25
|
end
|
26
26
|
|
27
27
|
response = Braai::Template.new(template).render(name: "mark")
|
28
|
-
response.
|
28
|
+
response.must_equal "I'm MARK and Damn, I love BBQ!!"
|
29
29
|
end
|
30
30
|
|
31
31
|
it "for loop example" do
|
@@ -44,29 +44,14 @@ describe 'README' do
|
|
44
44
|
<h2>{{greet.upcase}}</h2>
|
45
45
|
EOF
|
46
46
|
|
47
|
-
|
48
|
-
|
49
|
-
|
50
|
-
|
51
|
-
|
52
|
-
|
53
|
-
|
54
|
-
|
47
|
+
res = Braai::Template.new(template).render(greet: "mark", products: %w{car boat truck}, foods: %w{apple orange})
|
48
|
+
res.must_match("<h1>mark</h1>")
|
49
|
+
res.must_match("<li>car</li>")
|
50
|
+
res.must_match("<li>boat</li>")
|
51
|
+
res.must_match("<li>truck</li>")
|
52
|
+
res.must_match("<p>apple</p>")
|
53
|
+
res.must_match("<p>orange</p>")
|
54
|
+
res.must_match("<h2>MARK</h2>")
|
55
55
|
end
|
56
56
|
|
57
|
-
# it "description" do
|
58
|
-
# template = "I'm {{ name }} and {{ mmm... bbq }}!"
|
59
|
-
# Braai::Template.map(/mmm\.\.\. bbq/i) do |template, key, matches|
|
60
|
-
# puts "key: #{key.inspect}"
|
61
|
-
# puts "matches: #{matches.inspect}"
|
62
|
-
# "Damn, I love BBQ!"
|
63
|
-
# end
|
64
|
-
|
65
|
-
# Braai::Template.map(/name/i) do |template, key, matches|
|
66
|
-
# template.attributes[matches.first].upcase
|
67
|
-
# end
|
68
|
-
|
69
|
-
# puts Braai::Template.new(template).render(name: "Mark")
|
70
|
-
# end
|
71
|
-
|
72
57
|
end
|
data/spec/braai/template_spec.rb
CHANGED
@@ -1,4 +1,5 @@
|
|
1
1
|
require 'spec_helper'
|
2
|
+
require 'ostruct'
|
2
3
|
|
3
4
|
describe Braai::Template do
|
4
5
|
|
@@ -17,45 +18,45 @@ describe Braai::Template do
|
|
17
18
|
|
18
19
|
describe '.map' do
|
19
20
|
|
20
|
-
it "
|
21
|
-
Braai::Template.matchers.
|
21
|
+
it "lets you register a matcher" do
|
22
|
+
Braai::Template.matchers.wont_include(greet_regex.to_s)
|
22
23
|
Braai::Template.map(greet_regex, &greet_matcher)
|
23
|
-
Braai::Template.matchers.
|
24
|
+
Braai::Template.matchers.must_include(greet_regex.to_s)
|
24
25
|
end
|
25
26
|
|
26
27
|
it "takes a class that responds to call" do
|
27
28
|
Braai::Template.map(greet_regex, GreetHandler)
|
28
|
-
Braai::Template.matchers.
|
29
|
+
Braai::Template.matchers.must_include(greet_regex.to_s)
|
29
30
|
end
|
30
31
|
|
31
32
|
end
|
32
33
|
|
33
34
|
describe '#render' do
|
34
35
|
|
35
|
-
before
|
36
|
+
before do
|
36
37
|
Braai::Template.map(greet_regex, &greet_matcher)
|
37
38
|
Braai::Template.map(/{{ greetings }}/i, GreetHandler)
|
38
39
|
end
|
39
40
|
|
40
41
|
it "renders a simple template using a block" do
|
41
42
|
res = Braai::Template.new("{{ greet }}").render(greet: "Hi Mark")
|
42
|
-
res.
|
43
|
+
res.must_equal("Hi Mark")
|
43
44
|
end
|
44
45
|
|
45
46
|
it "renders a simple template using a handler class" do
|
46
47
|
res = Braai::Template.new("{{ greetings }}").render(greet: "Hi Mark")
|
47
|
-
res.
|
48
|
+
res.must_equal("HI MARK")
|
48
49
|
end
|
49
50
|
|
50
51
|
it "doesn't care about spaces" do
|
51
52
|
template = "<h1>{{greet}}</h1><h2>{{ greet }}</h2>"
|
52
53
|
res = Braai::Template.new(template).render(greet: "Hi Mark")
|
53
|
-
res.
|
54
|
+
res.must_equal("<h1>Hi Mark</h1><h2>Hi Mark</h2>")
|
54
55
|
end
|
55
56
|
|
56
|
-
|
57
|
+
describe 'matches' do
|
57
58
|
|
58
|
-
|
59
|
+
describe 'multimatches' do
|
59
60
|
|
60
61
|
let(:multi_regex) { /({{\s*(greet)\s*(mark)?\s*}})/ }
|
61
62
|
let(:multi_matcher) do
|
@@ -64,21 +65,21 @@ describe Braai::Template do
|
|
64
65
|
}
|
65
66
|
end
|
66
67
|
|
67
|
-
before
|
68
|
+
before do
|
68
69
|
Braai::Template.map(multi_regex, &multi_matcher)
|
69
70
|
end
|
70
71
|
|
71
72
|
it "allows nil matches" do
|
72
73
|
template = "<h1>{{ greet }}</h1><h2>{{ greet mark }}</h2>"
|
73
74
|
res = Braai::Template.new(template).render
|
74
|
-
res.
|
75
|
+
res.must_equal("<h1>greet</h1><h2>greetmark</h2>")
|
75
76
|
end
|
76
77
|
|
77
78
|
end
|
78
79
|
|
79
80
|
end
|
80
81
|
|
81
|
-
|
82
|
+
describe "for loops" do
|
82
83
|
|
83
84
|
let(:template) do
|
84
85
|
<<-EOF
|
@@ -99,36 +100,47 @@ EOF
|
|
99
100
|
|
100
101
|
it "renders the loop" do
|
101
102
|
res = Braai::Template.new(template).render(greet: "mark", products: %w{car boat truck}, foods: %w{apple orange})
|
102
|
-
res.
|
103
|
-
res.
|
104
|
-
res.
|
105
|
-
res.
|
106
|
-
res.
|
107
|
-
res.
|
108
|
-
res.
|
103
|
+
res.must_match("<h1>mark</h1>")
|
104
|
+
res.must_match("<li>car</li>")
|
105
|
+
res.must_match("<li>boat</li>")
|
106
|
+
res.must_match("<li>truck</li>")
|
107
|
+
res.must_match("<p>apple</p>")
|
108
|
+
res.must_match("<p>orange</p>")
|
109
|
+
res.must_match("<h2>MARK</h2>")
|
109
110
|
end
|
110
111
|
|
111
112
|
end
|
112
113
|
|
113
|
-
|
114
|
+
describe "default matcher" do
|
114
115
|
|
115
116
|
let(:template) { "{{ greet }} {{ name.upcase }}" }
|
117
|
+
let(:adv_template) { "{{ greet }} my name is {{ name.full_name.upcase }}" }
|
116
118
|
|
117
119
|
it "uses the default matcher to render" do
|
118
120
|
res = Braai::Template.new(template).render(greet: "Hi", name: "mark")
|
119
|
-
res.
|
121
|
+
res.must_equal("Hi MARK")
|
120
122
|
end
|
121
123
|
|
122
124
|
it "handles the attribute not being there" do
|
123
125
|
res = Braai::Template.new(template).render(greet: "Hi")
|
124
|
-
res.
|
126
|
+
res.must_equal("Hi {{ name.upcase }}")
|
127
|
+
end
|
128
|
+
|
129
|
+
it "handles deeply nested attributes" do
|
130
|
+
res = Braai::Template.new(adv_template).render(greet: "Hello", name: OpenStruct.new(full_name: 'inigo montoya'))
|
131
|
+
res.must_equal("Hello my name is INIGO MONTOYA")
|
132
|
+
end
|
133
|
+
|
134
|
+
it "handles nested attribute hashes" do
|
135
|
+
res = Braai::Template.new(adv_template).render(greet: "Hello", name: { full_name: 'inigo montoya' })
|
136
|
+
res.must_equal("Hello my name is INIGO MONTOYA")
|
125
137
|
end
|
126
138
|
|
127
139
|
end
|
128
140
|
|
129
|
-
|
141
|
+
describe "matcher errors" do
|
130
142
|
|
131
|
-
before
|
143
|
+
before do
|
132
144
|
Braai::Template.map(/foo/) do |view, key, matches|
|
133
145
|
raise ArgumentError
|
134
146
|
end
|
@@ -136,26 +148,30 @@ EOF
|
|
136
148
|
|
137
149
|
let(:template) { "foo" }
|
138
150
|
|
139
|
-
|
151
|
+
describe "swallow_matcher_errors is true" do
|
152
|
+
|
153
|
+
before do
|
154
|
+
Braai.config.swallow_matcher_errors = true
|
155
|
+
end
|
140
156
|
|
141
157
|
it "swallows errors in the matcher" do
|
142
|
-
|
158
|
+
-> {
|
143
159
|
res = Braai::Template.new(template).render()
|
144
|
-
}
|
160
|
+
}
|
145
161
|
end
|
146
162
|
|
147
163
|
end
|
148
164
|
|
149
|
-
|
165
|
+
describe "swallow_matcher_errors is false" do
|
150
166
|
|
151
|
-
before
|
167
|
+
before do
|
152
168
|
Braai.config.swallow_matcher_errors = false
|
153
169
|
end
|
154
170
|
|
155
171
|
it "raises the errors from the matcher" do
|
156
|
-
|
172
|
+
-> {
|
157
173
|
res = Braai::Template.new(template).render
|
158
|
-
}.
|
174
|
+
}.must_raise(ArgumentError)
|
159
175
|
end
|
160
176
|
|
161
177
|
end
|
data/spec/spec_helper.rb
CHANGED
@@ -2,12 +2,15 @@ require 'bundler/setup'
|
|
2
2
|
|
3
3
|
require 'braai' # and any other gems you need
|
4
4
|
|
5
|
+
require 'minitest/autorun'
|
6
|
+
require "minitest-colorize"
|
7
|
+
|
5
8
|
Braai.config.logger = Logger.new(StringIO.new)
|
6
9
|
|
7
|
-
|
10
|
+
class MiniTest::Spec
|
8
11
|
|
9
|
-
|
12
|
+
before do
|
10
13
|
Braai::Template.reset!
|
11
14
|
end
|
12
15
|
|
13
|
-
end
|
16
|
+
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: braai
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.
|
4
|
+
version: 1.3.0
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -9,7 +9,7 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date:
|
12
|
+
date: 2013-01-28 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: activesupport
|
@@ -72,7 +72,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
72
72
|
version: '0'
|
73
73
|
requirements: []
|
74
74
|
rubyforge_project:
|
75
|
-
rubygems_version: 1.8.
|
75
|
+
rubygems_version: 1.8.24
|
76
76
|
signing_key:
|
77
77
|
specification_version: 3
|
78
78
|
summary: Fully extensible templating system.
|