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 CHANGED
@@ -3,4 +3,4 @@ source 'https://rubygems.org'
3
3
  # Specify your gem's dependencies in braai.gemspec
4
4
  gemspec
5
5
 
6
- gem "rspec"
6
+ gem "minitest-colorize"
data/Gemfile.lock CHANGED
@@ -1,30 +1,24 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- braai (1.2.2)
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.9)
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
- rspec
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.should eql "Hi Mark!"
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.should eql "Hi MARK!"
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.should eql "I'm MARK and Damn, I love BBQ!!"
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
- desc "Run tests"
5
- task :default do
6
- system "bundle exec rspec"
7
- end
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
@@ -32,9 +32,22 @@ module Braai::Matchers
32
32
  res.join("\n")
33
33
  }
34
34
 
35
- @matchers[/({{\s*([\w]+)\.([\w]+)\s*}})/i] = ->(template, key, matches) {
36
- attr = template.attributes[matches[1]]
37
- attr ? attr.send(matches[2]) : nil
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
@@ -1,3 +1,3 @@
1
1
  module Braai
2
- VERSION = "1.2.2"
2
+ VERSION = "1.3.0"
3
3
  end
@@ -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].should eql("foo" => 'bar')
11
+ context.attributes[:hash].must_equal("foo" => 'bar')
12
12
  end
13
+
13
14
  end
15
+
14
16
  end
15
- end
17
+
18
+ end
@@ -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.should include("foo")
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.should eql("bar")
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.should include("foo")
25
+ matchers.must_include("foo")
26
26
  unmap("foo")
27
- matchers.should_not include("foo")
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.should_not be_empty
36
+ matchers.wont_be_empty
37
37
  clear!
38
- matchers.should be_empty
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.should have(3).matchers
46
+ matchers.size.must_equal(3)
47
47
  map("foo") {}
48
- matchers.should have(4).matchers
48
+ matchers.size.must_equal(4)
49
49
  reset!
50
- matchers.should have(3).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.should have(1).matchers
60
+ matchers.size.must_equal(1)
61
61
  set_defaults
62
- matchers.should have(4).matchers
62
+ matchers.size.must_equal(4)
63
63
  end
64
64
 
65
65
  end
@@ -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.should eql "Hi Mark!"
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.should eql "Hi MARK!"
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.should eql "I'm MARK and Damn, I love BBQ!!"
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
- 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>")
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
@@ -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 "let's you register a matcher" do
21
- Braai::Template.matchers.should_not have_key(greet_regex.to_s)
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.should have_key(greet_regex.to_s)
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.should have_key(greet_regex.to_s)
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(:each) do
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.should eql("Hi Mark")
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.should eql("HI MARK")
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.should eql("<h1>Hi Mark</h1><h2>Hi Mark</h2>")
54
+ res.must_equal("<h1>Hi Mark</h1><h2>Hi Mark</h2>")
54
55
  end
55
56
 
56
- context 'matches' do
57
+ describe 'matches' do
57
58
 
58
- context 'multimatches' do
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(:each) do
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.should eql("<h1>greet</h1><h2>greetmark</h2>")
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
- context "for loops" do
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.should match("<h1>mark</h1>")
103
- res.should match("<li>car</li>")
104
- res.should match("<li>boat</li>")
105
- res.should match("<li>truck</li>")
106
- res.should match("<p>apple</p>")
107
- res.should match("<p>orange</p>")
108
- res.should match("<h2>MARK</h2>")
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
- context "default matcher" do
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.should eql("Hi MARK")
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.should eql("Hi {{ name.upcase }}")
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
- context "matcher errors" do
141
+ describe "matcher errors" do
130
142
 
131
- before(:each) do
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
- context "swallow_matcher_errors is true" do
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
- expect {
158
+ -> {
143
159
  res = Braai::Template.new(template).render()
144
- }.to_not raise_error
160
+ }
145
161
  end
146
162
 
147
163
  end
148
164
 
149
- context "swallow_matcher_errors is false" do
165
+ describe "swallow_matcher_errors is false" do
150
166
 
151
- before(:each) do
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
- expect {
172
+ -> {
157
173
  res = Braai::Template.new(template).render
158
- }.to raise_error(ArgumentError)
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
- RSpec.configure do |config|
10
+ class MiniTest::Spec
8
11
 
9
- config.before do
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.2.2
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: 2012-12-17 00:00:00.000000000 Z
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.23
75
+ rubygems_version: 1.8.24
76
76
  signing_key:
77
77
  specification_version: 3
78
78
  summary: Fully extensible templating system.