braai 1.5.0 → 1.6.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.
- data/Gemfile.lock +4 -4
- data/lib/braai/context.rb +25 -15
- data/lib/braai/handlers/base.rb +1 -1
- data/lib/braai/handlers/iteration.rb +1 -1
- data/lib/braai/handlers.rb +1 -1
- data/lib/braai/matchers.rb +7 -0
- data/lib/braai/template.rb +4 -4
- data/lib/braai/version.rb +1 -1
- data/spec/braai/context_spec.rb +3 -2
- data/spec/braai/handlers/base_spec.rb +3 -3
- data/spec/braai/template_spec.rb +26 -0
- metadata +3 -3
data/Gemfile.lock
CHANGED
@@ -1,23 +1,23 @@
|
|
1
1
|
PATH
|
2
2
|
remote: .
|
3
3
|
specs:
|
4
|
-
braai (1.
|
4
|
+
braai (1.6.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.12)
|
11
11
|
i18n (~> 0.6)
|
12
12
|
multi_json (~> 1.0)
|
13
|
-
i18n (0.6.
|
13
|
+
i18n (0.6.4)
|
14
14
|
metaclass (0.0.1)
|
15
15
|
minitest (2.12.1)
|
16
16
|
minitest-colorize (0.0.4)
|
17
17
|
minitest (~> 2.0)
|
18
18
|
mocha (0.12.4)
|
19
19
|
metaclass (~> 0.0.1)
|
20
|
-
multi_json (1.
|
20
|
+
multi_json (1.6.1)
|
21
21
|
|
22
22
|
PLATFORMS
|
23
23
|
ruby
|
data/lib/braai/context.rb
CHANGED
@@ -1,32 +1,42 @@
|
|
1
1
|
class Braai::Context
|
2
2
|
|
3
|
-
attr_accessor :attributes
|
4
|
-
attr_accessor :template
|
5
|
-
attr_accessor :matchers
|
3
|
+
attr_accessor :attributes, :matchers, :fallback, :substrate
|
6
4
|
|
7
|
-
def initialize(
|
5
|
+
def initialize(substrate, template, attributes = {})
|
8
6
|
self.attributes = HashWithIndifferentAccess.new(attributes)
|
9
|
-
self.
|
10
|
-
self.matchers = matchers
|
7
|
+
self.substrate = substrate.dup
|
8
|
+
self.matchers = template.matchers
|
9
|
+
self.fallback = template.fallback
|
11
10
|
end
|
12
11
|
|
13
12
|
def render
|
14
13
|
begin
|
14
|
+
|
15
15
|
self.matchers.each do |regex, matcher|
|
16
|
-
regex
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
self.template.gsub!(set[0], val.to_s) if val
|
22
|
-
end
|
16
|
+
substitute(regex, matcher)
|
17
|
+
end
|
18
|
+
|
19
|
+
if self.fallback
|
20
|
+
substitute(self.fallback[:regex], self.fallback[:handler])
|
23
21
|
end
|
24
|
-
|
22
|
+
|
23
|
+
return self.substrate
|
25
24
|
rescue Exception => e
|
26
|
-
puts e.inspect
|
27
25
|
Braai.logger.error(e)
|
28
26
|
raise e unless Braai.config.swallow_matcher_errors
|
29
27
|
end
|
30
28
|
end
|
31
29
|
|
30
|
+
private
|
31
|
+
|
32
|
+
def substitute(regex, matcher)
|
33
|
+
regex = Regexp.new(regex)
|
34
|
+
matches = self.substrate.scan(regex)
|
35
|
+
matches.each do |set|
|
36
|
+
set = [set].flatten.map {|m| m.strip unless m.nil? }
|
37
|
+
val = matcher.call(self, set[0], set)
|
38
|
+
self.substrate.gsub!(set[0], val.to_s) if val
|
39
|
+
end
|
40
|
+
end
|
41
|
+
|
32
42
|
end
|
data/lib/braai/handlers/base.rb
CHANGED
@@ -3,7 +3,7 @@ module Braai::Handlers
|
|
3
3
|
def perform
|
4
4
|
res = []
|
5
5
|
template.attributes[matches[2]].each do |val|
|
6
|
-
res << Braai::Context.new(matches[3], template
|
6
|
+
res << Braai::Context.new(matches[3], template, template.attributes.merge(matches[1] => val)).render
|
7
7
|
end
|
8
8
|
res.join("\n")
|
9
9
|
end
|
data/lib/braai/handlers.rb
CHANGED
data/lib/braai/matchers.rb
CHANGED
@@ -1,5 +1,7 @@
|
|
1
1
|
module Braai::Matchers
|
2
2
|
|
3
|
+
attr_accessor :fallback
|
4
|
+
|
3
5
|
IterationMatcher = /({{\s*for (\w+) in (\w+)\s*}}(.+?){{\s*\/for\s*}})/im
|
4
6
|
DefaultMatcher = /({{\s*([\w\.]+)\s*}})/i
|
5
7
|
RegionMatcher = /({{\s*([\w]+)\s*}}(.*){{\s*\/([\w]+)\s*}})/mi
|
@@ -12,12 +14,17 @@ module Braai::Matchers
|
|
12
14
|
@matchers = {regex.to_s => handler || block}.merge(self.matchers)
|
13
15
|
end
|
14
16
|
|
17
|
+
def add_fallback(regex, handler=nil, &block)
|
18
|
+
@fallback = { :regex => regex.to_s, :handler => handler || block }
|
19
|
+
end
|
20
|
+
|
15
21
|
def unmap(regex)
|
16
22
|
self.matchers.delete(regex.to_s)
|
17
23
|
end
|
18
24
|
|
19
25
|
def clear!
|
20
26
|
self.matchers.clear
|
27
|
+
self.fallback = nil
|
21
28
|
end
|
22
29
|
|
23
30
|
def reset!
|
data/lib/braai/template.rb
CHANGED
@@ -2,17 +2,17 @@ class Braai::Template
|
|
2
2
|
extend Braai::Matchers
|
3
3
|
include Braai::Matchers
|
4
4
|
|
5
|
-
attr_accessor :attributes
|
6
5
|
attr_accessor :template
|
7
6
|
|
8
7
|
def initialize(template, matchers = {})
|
9
|
-
self.template = template
|
10
8
|
@matchers = self.class.matchers.merge(matchers)
|
9
|
+
@template = template
|
10
|
+
@fallback = self.class.fallback
|
11
11
|
end
|
12
12
|
|
13
13
|
def render(attributes = {})
|
14
|
-
context = Braai::Context.new(
|
14
|
+
context = Braai::Context.new(@template, self, attributes)
|
15
15
|
context.render
|
16
16
|
end
|
17
17
|
|
18
|
-
end
|
18
|
+
end
|
data/lib/braai/version.rb
CHANGED
data/spec/braai/context_spec.rb
CHANGED
@@ -7,7 +7,8 @@ describe Braai::Context do
|
|
7
7
|
describe 'attributes arguments' do
|
8
8
|
|
9
9
|
it 'allows a nested hash' do
|
10
|
-
|
10
|
+
template = mock(:matchers => true, :fallback => true)
|
11
|
+
context = Braai::Context.new("some {{stuff}}", template, { hash: { foo: 'bar' } })
|
11
12
|
context.attributes[:hash].must_equal("foo" => 'bar')
|
12
13
|
end
|
13
14
|
|
@@ -15,4 +16,4 @@ describe Braai::Context do
|
|
15
16
|
|
16
17
|
end
|
17
18
|
|
18
|
-
end
|
19
|
+
end
|
@@ -59,7 +59,7 @@ describe Braai::Handlers::Base do
|
|
59
59
|
describe '#rescue_from_error' do
|
60
60
|
|
61
61
|
it 'calls a user-defined error handler if specified' do
|
62
|
-
Braai::Handlers.rescue_from ArgumentError, ->(e) { "<!-- #{key} -->" }
|
62
|
+
Braai::Handlers.rescue_from ArgumentError, ->(handler, e) { "<!-- #{key} -->" }
|
63
63
|
handler.rescue_from_error(ArgumentError.new).must_equal('<!-- {{ person.name }} -->')
|
64
64
|
end
|
65
65
|
|
@@ -70,8 +70,8 @@ describe Braai::Handlers::Base do
|
|
70
70
|
end
|
71
71
|
|
72
72
|
it "stops after the first matching rescuer" do
|
73
|
-
Braai::Handlers.rescue_from Exception, ->(e) { "!!! #{key} !!!" }
|
74
|
-
Braai::Handlers.rescue_from ArgumentError, ->(e) { "<!-- #{key} -->" }
|
73
|
+
Braai::Handlers.rescue_from Exception, ->(handler, e) { "!!! #{key} !!!" }
|
74
|
+
Braai::Handlers.rescue_from ArgumentError, ->(handler, e) { "<!-- #{key} -->" }
|
75
75
|
handler.rescue_from_error(ArgumentError.new).must_equal('<!-- {{ person.name }} -->')
|
76
76
|
end
|
77
77
|
|
data/spec/braai/template_spec.rb
CHANGED
@@ -3,6 +3,7 @@ require 'ostruct'
|
|
3
3
|
|
4
4
|
describe Braai::Template do
|
5
5
|
|
6
|
+
|
6
7
|
class GreetHandler
|
7
8
|
def self.call(template, key, matches)
|
8
9
|
template.attributes[:greet].upcase
|
@@ -59,6 +60,7 @@ describe Braai::Template do
|
|
59
60
|
res.must_equal("<h1>Hi Mark</h1><h2>Hi Mark</h2>")
|
60
61
|
end
|
61
62
|
|
63
|
+
|
62
64
|
describe 'matches' do
|
63
65
|
|
64
66
|
describe 'multimatches' do
|
@@ -71,6 +73,7 @@ describe Braai::Template do
|
|
71
73
|
end
|
72
74
|
|
73
75
|
before do
|
76
|
+
Braai::Template.clear!
|
74
77
|
Braai::Template.map(multi_regex, &multi_matcher)
|
75
78
|
end
|
76
79
|
|
@@ -90,6 +93,29 @@ describe Braai::Template do
|
|
90
93
|
|
91
94
|
end
|
92
95
|
|
96
|
+
|
97
|
+
describe "fallback matcher" do
|
98
|
+
|
99
|
+
before do
|
100
|
+
Braai::Template.map(/({{ yummy_(\w+) }})/, ->(view, key, matches) { "#{matches[1].upcase}: #{view.attributes[matches[1]]}" })
|
101
|
+
Braai::Template.add_fallback(/({{ (\w+) }})/, ->(view, key, matches) { "UNMATCHED_TAG" })
|
102
|
+
end
|
103
|
+
|
104
|
+
it "is called" do
|
105
|
+
template = "<h2>{{ bango }}</h2>"
|
106
|
+
res = Braai::Template.new(template).render
|
107
|
+
res.must_equal("<h2>UNMATCHED_TAG</h2>")
|
108
|
+
end
|
109
|
+
|
110
|
+
it "always comes last" do
|
111
|
+
template = "<h2>{{ bango }}</h2><p>{{ yummy_food }}</p><p>{{ yummy_drink }}</p> {{ blaz }}"
|
112
|
+
|
113
|
+
res = Braai::Template.new(template).render(food: 'pizza', drink: 'beer')
|
114
|
+
res.must_equal("<h2>UNMATCHED_TAG</h2><p>FOOD: pizza</p><p>DRINK: beer</p> UNMATCHED_TAG")
|
115
|
+
end
|
116
|
+
|
117
|
+
end
|
118
|
+
|
93
119
|
describe "matcher errors" do
|
94
120
|
|
95
121
|
before do
|
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.6.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: 2013-
|
12
|
+
date: 2013-03-08 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: activesupport
|
@@ -95,7 +95,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
95
95
|
version: '0'
|
96
96
|
requirements: []
|
97
97
|
rubyforge_project:
|
98
|
-
rubygems_version: 1.8.
|
98
|
+
rubygems_version: 1.8.25
|
99
99
|
signing_key:
|
100
100
|
specification_version: 3
|
101
101
|
summary: Fully extensible templating system.
|