braai 1.0.1 → 1.1.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 CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- braai (0.0.1)
4
+ braai (1.0.1)
5
5
  activesupport
6
6
 
7
7
  GEM
data/README.md CHANGED
@@ -23,12 +23,12 @@ Or install it yourself as:
23
23
 
24
24
  Braai comes shipped with two simple matchers for you, but you can easily add your own.
25
25
 
26
- The first matcher is a simple <code>to_s</code> handler. It will match a single variable and then call <code>to_s</code> on it:
26
+ The first matcher is a simple <code>to_s</code> matcher. It will match a single variable and then call <code>to_s</code> on it:
27
27
 
28
28
  <pre><code>
29
29
  template = "Hi {{ name }}!"
30
30
  response = Braai::Template.new(template).render(name: "Mark")
31
- response # => "Hi Mark!"
31
+ response.should eql "Hi Mark!"
32
32
  </code></pre>
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
  <pre><code>
37
37
  template = "Hi {{ name.upcase }}!"
38
38
  response = Braai::Template.new(template).render(name: "Mark")
39
- response # => "Hi MARK!"
39
+ response.should eql "Hi MARK!"
40
40
  </code></pre>
41
41
 
42
42
  ### Custom Matchers
@@ -45,15 +45,16 @@ Braai let's you easily define your own matchers to do whatever you would like to
45
45
 
46
46
  <pre><code>
47
47
  template = "I'm {{ name }} and {{ mmm... bbq }}!"
48
- Braai::Template.map(/mmm\.\.\. bbq/i) do |template, key, matches|
48
+ Braai::Template.map(/({{\s*mmm\.\.\. bbq\s*}})/i) do |template, key, matches|
49
49
  "Damn, I love BBQ!"
50
50
  end
51
51
 
52
- Braai::Template.map(/name/i) do |template, key, matches|
53
- template.attributes[matches.first].upcase
52
+ Braai::Template.map(/({{\s*name\s*}})/i) do |template, key, matches|
53
+ template.attributes[:name].upcase
54
54
  end
55
55
 
56
- Braai::Template.new(template).render # => "I'm MARK and Damn, I love BBQ!!"
56
+ response = Braai::Template.new(template).render(name: "mark")
57
+ response.should eql "I'm MARK and Damn, I love BBQ!!"
57
58
  </code></pre>
58
59
 
59
60
  ### For Loops
@@ -61,7 +62,7 @@ Braai::Template.new(template).render # => "I'm MARK and Damn, I love BBQ!!"
61
62
  Braai supports looping right out of the box.
62
63
 
63
64
  <pre><code>
64
- template = <<-EOF
65
+ template = &lt;&lt;-EOF
65
66
  &lt;h1>{{ greet }}&lt;/h1>
66
67
  &lt;ul>
67
68
  {{ for product in products }}
@@ -76,19 +77,14 @@ template = <<-EOF
76
77
  &lt;h2>{{greet.upcase}}&lt;/h2>
77
78
  EOF
78
79
 
79
- Braai::Template.new(template).render(greet: "mark", products: %w{car boat truck}, foods: %w{apple orange})
80
- # =>
81
- "&lt;h1>mark&lt;/h1>
82
- &lt;ul>
83
- &lt;li>car&lt;/li>
84
- &lt;li>boat&lt;/li>
85
- &lt;li>truck&lt;/li>
86
- &lt;/ul>
87
- &lt;div>
88
- &lt;p>apple&lt;/p>
89
- &lt;p>orange&lt;/p>
90
- &lt;/div>
91
- &lt;h2>MARK&lt;/h2>"
80
+ res = Braai::Template.new(template).render(greet: "mark", products: %w{car boat truck}, foods: %w{apple orange})
81
+ res.should match("&lt;h1>mark&lt;/h1>")
82
+ res.should match("&lt;li>car&lt;/li>")
83
+ res.should match("&lt;li>boat&lt;/li>")
84
+ res.should match("&lt;li>truck&lt;/li>")
85
+ res.should match("&lt;p>apple&lt;/p>")
86
+ res.should match("&lt;p>orange&lt;/p>")
87
+ res.should match("&lt;h2>MARK&lt;/h2>")
92
88
  </code></pre>
93
89
 
94
90
  ## Contributing
@@ -1,16 +1,10 @@
1
1
  class Braai::Configuration
2
2
 
3
3
  attr_accessor :logger
4
- attr_accessor :raise_on_missing_handler
5
- attr_accessor :swallow_handler_errors
6
- attr_accessor :for_loop_regex
7
- attr_accessor :handler_regex
4
+ attr_accessor :swallow_matcher_errors
8
5
 
9
6
  def initialize
10
- self.raise_on_missing_handler = false
11
- self.swallow_handler_errors = true
12
- self.handler_regex = /{{\s*[^}]+\s*}}/i
13
- self.for_loop_regex = /({{\s*for (\w+) in (\w+)\s*}}(.+?){{\s*\/for\s*}})/im
7
+ self.swallow_matcher_errors = true
14
8
  end
15
9
 
16
10
  def logger
data/lib/braai/context.rb CHANGED
@@ -2,65 +2,30 @@ class Braai::Context
2
2
 
3
3
  attr_accessor :attributes
4
4
  attr_accessor :template
5
- attr_accessor :handlers
5
+ attr_accessor :matchers
6
6
 
7
- def initialize(template, handlers, attributes = {})
7
+ def initialize(template, matchers, attributes = {})
8
8
  self.attributes = HashWithIndifferentAccess.new(attributes)
9
9
  self.template = template.dup
10
- self.handlers = handlers
10
+ self.matchers = matchers
11
11
  end
12
12
 
13
13
  def render
14
14
  begin
15
- self.render!
16
- rescue Exception => e
17
- Braai.logger.error(e)
18
- raise e
19
- end
20
- end
21
-
22
- def render!
23
- self.process_loops
24
- self.process_keys
25
- return self.template
26
- end
27
-
28
- protected
29
- def process_loops
30
- loops = self.template.scan(Braai.config.for_loop_regex)
31
- loops.each do |loop|
32
- res = []
33
- self.attributes[loop[2]].each do |val|
34
- res << Braai::Context.new(loop[3], self.handlers, self.attributes.merge(loop[1] => val)).render!
35
- end
36
- self.template.gsub!(loop[0], res.join("\n"))
37
- end
38
- end
39
-
40
- def process_keys
41
- keys = self.template.scan(Braai.config.handler_regex).flatten.uniq
42
- keys.each do |key|
43
- self.handle_key(key)
44
- end
45
- end
46
-
47
- def handle_key(key)
48
- stripped_key = key.gsub(/({|})/, "").strip
49
- matched = false
50
- self.handlers.each do |regex, handler|
51
- regex = Regexp.new(regex)
52
- if regex.match(stripped_key)
53
- begin
54
- val = handler.call(self, stripped_key, stripped_key.scan(regex).flatten)
55
- self.template.gsub!(key, val.to_s) if val
56
- rescue Exception => e
57
- raise e unless Braai.config.swallow_handler_errors
15
+ self.matchers.each do |regex, matcher|
16
+ regex = Regexp.new(regex)
17
+ matches = self.template.scan(regex)
18
+ matches.each do |set|
19
+ val = matcher.call(self, set[0], set)
20
+ self.template.gsub!(set[0], val.to_s) if val
58
21
  end
59
- matched = true
60
- break
61
22
  end
23
+ return self.template
24
+ rescue Exception => e
25
+ puts e.inspect
26
+ Braai.logger.error(e)
27
+ raise e unless Braai.config.swallow_matcher_errors
62
28
  end
63
- raise Braai::MissingHandlerError.new(stripped_key) if !matched && Braai.config.raise_on_missing_handler
64
29
  end
65
30
 
66
31
  end
@@ -0,0 +1,40 @@
1
+ module Braai::Matchers
2
+
3
+ def matchers
4
+ @matchers ||= reset!
5
+ end
6
+
7
+ def map(regex, &block)
8
+ @matchers = {regex.to_s => block}.merge(self.matchers)
9
+ end
10
+
11
+ def unmap(regex)
12
+ self.matchers.delete(regex.to_s)
13
+ end
14
+
15
+ def clear!
16
+ self.matchers.clear
17
+ end
18
+
19
+ def reset!
20
+ @matchers = {
21
+ /({{\s*for (\w+) in (\w+)\s*}}(.+?){{\s*\/for\s*}})/im => ->(template, key, matches) {
22
+ res = []
23
+ template.attributes[matches[2]].each do |val|
24
+ res << Braai::Context.new(matches[3], template.matchers, template.attributes.merge(matches[1] => val)).render
25
+ end
26
+ res.join("\n")
27
+ },
28
+ /({{\s*([\w]+)\.([\w]+)\s*}})/i => ->(template, key, matches) {
29
+ attr = template.attributes[matches[1]]
30
+ attr ? attr.send(matches[2]) : nil
31
+ },
32
+ /({{\s*([\w]+)\s*}})/i => ->(template, key, matches) {
33
+ attr = template.attributes[matches.last]
34
+ attr ? attr.to_s : nil
35
+ }
36
+ }
37
+ return @matchers
38
+ end
39
+
40
+ end
@@ -1,27 +1,18 @@
1
1
  class Braai::Template
2
- extend Braai::Handlers
3
- include Braai::Handlers
2
+ extend Braai::Matchers
3
+ include Braai::Matchers
4
4
 
5
5
  attr_accessor :attributes
6
6
  attr_accessor :template
7
7
 
8
- def initialize(template, handlers = {})
8
+ def initialize(template, matchers = {})
9
9
  self.template = template
10
- @handlers = self.class.handlers.merge(handlers)
10
+ @matchers = self.class.matchers.merge(matchers)
11
11
  end
12
12
 
13
13
  def render(attributes = {})
14
- begin
15
- return self.render!(attributes)
16
- rescue Exception => e
17
- Braai.logger.error(e)
18
- raise e
19
- end
20
- end
21
-
22
- def render!(attributes)
23
- context = Braai::Context.new(self.template, self.handlers, attributes)
24
- context.render!
14
+ context = Braai::Context.new(self.template, self.matchers, attributes)
15
+ context.render
25
16
  end
26
17
 
27
18
  end
data/lib/braai/version.rb CHANGED
@@ -1,3 +1,3 @@
1
1
  module Braai
2
- VERSION = "1.0.1"
2
+ VERSION = "1.1.0"
3
3
  end
data/lib/braai.rb CHANGED
@@ -2,8 +2,7 @@ require 'logger'
2
2
  require 'active_support/hash_with_indifferent_access'
3
3
  require "braai/version"
4
4
  require "braai/configuration"
5
- require "braai/errors"
6
- require "braai/handlers"
5
+ require "braai/matchers"
7
6
  require "braai/template"
8
7
  require "braai/context"
9
8
 
@@ -0,0 +1,55 @@
1
+ require 'spec_helper'
2
+
3
+ describe Braai::Matchers do
4
+ include Braai::Matchers
5
+
6
+ describe 'map' do
7
+
8
+ it "maps a new matcher" do
9
+ map("foo") {}
10
+ matchers.should include("foo")
11
+ end
12
+
13
+ it "makes the latest matcher the first matcher in the list" do
14
+ map("foo") {}
15
+ map("bar") {}
16
+ matchers.keys.first.should eql("bar")
17
+ end
18
+
19
+ end
20
+
21
+ describe 'unmap' do
22
+
23
+ it "unmaps a matcher" do
24
+ map("foo") {}
25
+ matchers.should include("foo")
26
+ unmap("foo")
27
+ matchers.should_not include("foo")
28
+ end
29
+
30
+ end
31
+
32
+ describe 'clear!' do
33
+
34
+ it "removes all of the matchers" do
35
+ map("foo") {}
36
+ matchers.should_not be_empty
37
+ clear!
38
+ matchers.should be_empty
39
+ end
40
+
41
+ end
42
+
43
+ describe 'reset!' do
44
+
45
+ it "resets the matchers to their original state" do
46
+ matchers.should have(3).matchers
47
+ map("foo") {}
48
+ matchers.should have(4).matchers
49
+ reset!
50
+ matchers.should have(3).matchers
51
+ end
52
+
53
+ end
54
+
55
+ end
@@ -0,0 +1,72 @@
1
+ require 'spec_helper'
2
+
3
+ describe 'README' do
4
+
5
+ it "simple example" do
6
+ template = "Hi {{ name }}!"
7
+ response = Braai::Template.new(template).render(name: "Mark")
8
+ response.should eql "Hi Mark!"
9
+ end
10
+
11
+ it "simple method call example" do
12
+ template = "Hi {{ name.upcase }}!"
13
+ response = Braai::Template.new(template).render(name: "Mark")
14
+ response.should eql "Hi MARK!"
15
+ end
16
+
17
+ it "custom matcher example" do
18
+ template = "I'm {{ name }} and {{ mmm... bbq }}!"
19
+ Braai::Template.map(/({{\s*mmm\.\.\. bbq\s*}})/i) do |template, key, matches|
20
+ "Damn, I love BBQ!"
21
+ end
22
+
23
+ Braai::Template.map(/({{\s*name\s*}})/i) do |template, key, matches|
24
+ template.attributes[:name].upcase
25
+ end
26
+
27
+ response = Braai::Template.new(template).render(name: "mark")
28
+ response.should eql "I'm MARK and Damn, I love BBQ!!"
29
+ end
30
+
31
+ it "for loop example" do
32
+ template = <<-EOF
33
+ <h1>{{ greet }}</h1>
34
+ <ul>
35
+ {{ for product in products }}
36
+ <li>{{ product }}</li>
37
+ {{ /for }}
38
+ </ul>
39
+ <div>
40
+ {{ for food in foods }}
41
+ <p>{{ food }}</p>
42
+ {{ /for }}
43
+ </div>
44
+ <h2>{{greet.upcase}}</h2>
45
+ EOF
46
+
47
+ res = Braai::Template.new(template).render(greet: "mark", products: %w{car boat truck}, foods: %w{apple orange})
48
+ res.should match("<h1>mark</h1>")
49
+ res.should match("<li>car</li>")
50
+ res.should match("<li>boat</li>")
51
+ res.should match("<li>truck</li>")
52
+ res.should match("<p>apple</p>")
53
+ res.should match("<p>orange</p>")
54
+ res.should match("<h2>MARK</h2>")
55
+ end
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
+ end
@@ -3,7 +3,7 @@ require 'spec_helper'
3
3
  describe Braai::Template do
4
4
 
5
5
  let(:greet_regex) { /^greet$/i }
6
- let(:greet_handler) do
6
+ let(:greet_matcher) do
7
7
  ->(view, key, matches) {
8
8
  view.attributes[:greet]
9
9
  }
@@ -11,10 +11,10 @@ describe Braai::Template do
11
11
 
12
12
  describe '.map' do
13
13
 
14
- it "let's you register a handler" do
15
- Braai::Template.handlers.should_not have_key(greet_regex.to_s)
16
- Braai::Template.map(greet_regex, &greet_handler)
17
- Braai::Template.handlers.should have_key(greet_regex.to_s)
14
+ it "let's you register a matcher" do
15
+ Braai::Template.matchers.should_not have_key(greet_regex.to_s)
16
+ Braai::Template.map(greet_regex, &greet_matcher)
17
+ Braai::Template.matchers.should have_key(greet_regex.to_s)
18
18
  end
19
19
 
20
20
  end
@@ -22,7 +22,7 @@ describe Braai::Template do
22
22
  describe '#render' do
23
23
 
24
24
  before(:each) do
25
- Braai::Template.map(greet_regex, &greet_handler)
25
+ Braai::Template.map(greet_regex, &greet_matcher)
26
26
  end
27
27
 
28
28
  it "renders a simple template" do
@@ -57,7 +57,6 @@ EOF
57
57
 
58
58
  it "renders the loop" do
59
59
  res = Braai::Template.new(template).render(greet: "mark", products: %w{car boat truck}, foods: %w{apple orange})
60
- puts res
61
60
  res.should match("<h1>mark</h1>")
62
61
  res.should match("<li>car</li>")
63
62
  res.should match("<li>boat</li>")
@@ -67,28 +66,13 @@ EOF
67
66
  res.should match("<h2>MARK</h2>")
68
67
  end
69
68
 
70
- # it "description" do
71
- # template = "I'm {{ name }} and {{ mmm... bbq }}!"
72
- # Braai::Template.map(/mmm\.\.\. bbq/i) do |template, key, matches|
73
- # puts "key: #{key.inspect}"
74
- # puts "matches: #{matches.inspect}"
75
- # "Damn, I love BBQ!"
76
- # end
77
-
78
- # Braai::Template.map(/name/i) do |template, key, matches|
79
- # template.attributes[matches.first].upcase
80
- # end
81
-
82
- # puts Braai::Template.new(template).render(name: "Mark")
83
- # end
84
-
85
69
  end
86
70
 
87
- context "default handler" do
71
+ context "default matcher" do
88
72
 
89
73
  let(:template) { "{{ greet }} {{ name.upcase }}" }
90
74
 
91
- it "uses the default handler to render" do
75
+ it "uses the default matcher to render" do
92
76
  res = Braai::Template.new(template).render(greet: "Hi", name: "mark")
93
77
  res.should eql("Hi MARK")
94
78
  end
@@ -100,73 +84,35 @@ EOF
100
84
 
101
85
  end
102
86
 
103
- context "missing handlers" do
104
-
105
- before(:each) do
106
- Braai::Template.reset!
107
- end
108
-
109
- after(:each) do
110
- Braai.config.raise_on_missing_handler = false
111
- end
112
-
113
- context "raise_on_missing_handler is true" do
114
-
115
- before(:each) do
116
- Braai.config.raise_on_missing_handler = true
117
- end
118
-
119
- it "raises an error" do
120
- expect {
121
- Braai::Template.new("{{ please.greet.me }}").render(greet: "Hi Mark")
122
- }.to raise_error(Braai::MissingHandlerError)
123
- end
124
-
125
- end
126
-
127
- context "raise_on_missing_handler is false" do
128
-
129
- it "does not raise an error" do
130
- expect {
131
- res = Braai::Template.new("{{ greet }}").render(greet: "Hi Mark")
132
- res.should eql("{{ greet }}")
133
- }.to_not raise_error(Braai::MissingHandlerError)
134
- end
135
-
136
- end
137
-
138
- end
139
-
140
- context "handler errors" do
87
+ context "matcher errors" do
141
88
 
142
89
  before(:each) do
143
- Braai::Template.map(/^foo$/) do |view, key, matches|
90
+ Braai::Template.map(/foo/) do |view, key, matches|
144
91
  raise ArgumentError
145
92
  end
146
93
  end
147
94
 
148
- let(:template) { "{{ foo }}" }
95
+ let(:template) { "foo" }
149
96
 
150
- context "swallow_handler_errors is true" do
97
+ context "swallow_matcher_errors is true" do
151
98
 
152
- it "swallows errors in the handler" do
99
+ it "swallows errors in the matcher" do
153
100
  expect {
154
101
  res = Braai::Template.new(template).render()
155
- res.should eql template
156
102
  }.to_not raise_error
157
103
  end
158
104
 
159
105
  end
160
106
 
161
- context "swallow_handler_errors is false" do
107
+ context "swallow_matcher_errors is false" do
162
108
 
163
109
  before(:each) do
164
- Braai.config.swallow_handler_errors = false
110
+ Braai.config.swallow_matcher_errors = false
165
111
  end
166
112
 
167
- it "raises the errors from the handler" do
113
+ it "raises the errors from the matcher" do
168
114
  expect {
169
- Braai::Template.new(template).render()
115
+ res = Braai::Template.new(template).render
170
116
  }.to raise_error(ArgumentError)
171
117
  end
172
118
 
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.0.1
4
+ version: 1.1.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: 2012-08-28 00:00:00.000000000 Z
12
+ date: 2012-08-29 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: activesupport
@@ -44,11 +44,11 @@ files:
44
44
  - lib/braai.rb
45
45
  - lib/braai/configuration.rb
46
46
  - lib/braai/context.rb
47
- - lib/braai/errors.rb
48
- - lib/braai/handlers.rb
47
+ - lib/braai/matchers.rb
49
48
  - lib/braai/template.rb
50
49
  - lib/braai/version.rb
51
- - spec/braai/handlers_spec.rb
50
+ - spec/braai/matchers_spec.rb
51
+ - spec/braai/readme_spec.rb
52
52
  - spec/braai/template_spec.rb
53
53
  - spec/spec_helper.rb
54
54
  homepage: ''
@@ -76,6 +76,7 @@ signing_key:
76
76
  specification_version: 3
77
77
  summary: Fully extensible templating system.
78
78
  test_files:
79
- - spec/braai/handlers_spec.rb
79
+ - spec/braai/matchers_spec.rb
80
+ - spec/braai/readme_spec.rb
80
81
  - spec/braai/template_spec.rb
81
82
  - spec/spec_helper.rb
data/lib/braai/errors.rb DELETED
@@ -1,7 +0,0 @@
1
- module Braai
2
- class MissingHandlerError < StandardError
3
- def initialize(key)
4
- super "#{key} was missing a handler! Please implement one."
5
- end
6
- end
7
- end
@@ -1,33 +0,0 @@
1
- module Braai::Handlers
2
-
3
- def handlers
4
- @handlers ||= reset!
5
- end
6
-
7
- def map(regex, &block)
8
- @handlers = {regex.to_s => block}.merge(self.handlers)
9
- end
10
-
11
- def unmap(regex)
12
- self.handlers.delete(regex.to_s)
13
- end
14
-
15
- def clear!
16
- self.handlers.clear
17
- end
18
-
19
- def reset!
20
- @handlers = {
21
- /^([\w]+)\.([\w]+)$/i => ->(template, key, matches) {
22
- attr = template.attributes[matches.first]
23
- attr ? attr.send(matches.last) : nil
24
- },
25
- /^(\w+)$/i => ->(template, key, matches) {
26
- attr = template.attributes[matches.first]
27
- attr ? attr.to_s : nil
28
- }
29
- }
30
- return @handlers
31
- end
32
-
33
- end
@@ -1,55 +0,0 @@
1
- require 'spec_helper'
2
-
3
- describe Braai::Handlers do
4
- include Braai::Handlers
5
-
6
- describe 'map' do
7
-
8
- it "maps a new handler" do
9
- map("foo") {}
10
- handlers.should include("foo")
11
- end
12
-
13
- it "makes the latest handler the first handler in the list" do
14
- map("foo") {}
15
- map("bar") {}
16
- handlers.keys.first.should eql("bar")
17
- end
18
-
19
- end
20
-
21
- describe 'unmap' do
22
-
23
- it "unmaps a handler" do
24
- map("foo") {}
25
- handlers.should include("foo")
26
- unmap("foo")
27
- handlers.should_not include("foo")
28
- end
29
-
30
- end
31
-
32
- describe 'clear!' do
33
-
34
- it "removes all of the handlers" do
35
- map("foo") {}
36
- handlers.should_not be_empty
37
- clear!
38
- handlers.should be_empty
39
- end
40
-
41
- end
42
-
43
- describe 'reset!' do
44
-
45
- it "resets the handlers to their original state" do
46
- handlers.should have(2).handlers
47
- map("foo") {}
48
- handlers.should have(3).handlers
49
- reset!
50
- handlers.should have(2).handlers
51
- end
52
-
53
- end
54
-
55
- end