braai 1.6.1 → 1.6.2

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- braai (1.6.1)
4
+ braai (1.6.2)
5
5
  activesupport
6
6
 
7
7
  GEM
data/README.md CHANGED
@@ -57,9 +57,22 @@ response = Braai::Template.new(template).render(name: "mark")
57
57
  response.must_equal "I'm MARK and Damn, I love BBQ!!"
58
58
  ```
59
59
 
60
+ ### Conditional Logic
61
+
62
+ Braai supports conditional logic with simple if statements (no support for comparisons yet).
63
+
64
+ ```ruby
65
+ template = '{{ product.name }}{{ if product.featured }}***{{ /if }}'
66
+ featured_product = OpenStruct.new(name: 'Special Product', featured: true)
67
+ regular_product = OpenStruct.new(name: 'Regular Product', featured: false)
68
+
69
+ Braai::Template.new(template).render(product: featured_product).must_equal("Special Product***")
70
+ Braai::Template.new(template).render(product: regular_product).must_equal("Regular Product")
71
+ ```
72
+
60
73
  ### For Loops
61
74
 
62
- Braai supports looping right out of the box.
75
+ Braai also supports looping right out of the box.
63
76
 
64
77
  ```ruby
65
78
  template = <<-EOF
@@ -100,4 +113,4 @@ res.should match("<h2>MARK</h2>")
100
113
  * Mark Bates
101
114
  * Nick Plante
102
115
  * Sam Beam
103
- * Kevin Incorvia
116
+ * Kevin Incorvia
@@ -2,6 +2,7 @@ require 'logger'
2
2
  require "active_support/core_ext/hash/indifferent_access"
3
3
  require "braai/version"
4
4
  require "braai/configuration"
5
+ require "braai/helpers"
5
6
  require "braai/handlers"
6
7
  require "braai/matchers"
7
8
  require "braai/template"
@@ -1,10 +1,11 @@
1
1
  require 'braai/handlers/base'
2
2
  require 'braai/handlers/default'
3
3
  require 'braai/handlers/iteration'
4
+ require 'braai/handlers/conditional'
4
5
 
5
6
  module Braai
6
7
  module Handlers
7
-
8
+
8
9
  class << self
9
10
  def rescue_from(klass, handler)
10
11
  self.rescuers.unshift({klass: klass, handler: handler})
@@ -0,0 +1,14 @@
1
+ module Braai::Handlers
2
+ class Conditional < Base
3
+ include Braai::Helpers
4
+
5
+ def perform
6
+ res = resolve_variable_chain_value(matches[1])
7
+ if res && res != ""
8
+ Braai::Context.new(matches[2], template, template.attributes).render
9
+ else
10
+ ""
11
+ end
12
+ end
13
+ end
14
+ end
@@ -1,30 +1,9 @@
1
1
  module Braai::Handlers
2
2
  class Default < Base
3
- def perform
4
- value = nil
5
- chain = matches[1].split('.')
6
-
7
- if template.attributes.has_key?(chain.first)
8
- value = template.attributes[chain.shift] || ''
9
- else
10
- return nil unless value
11
- end
3
+ include Braai::Helpers
12
4
 
13
- chain.each do |a|
14
- if value.respond_to?(a.to_sym)
15
- value = value.send(a) || ''
16
- elsif value.is_a?(Hash)
17
- if value.has_key?(a.to_sym) || value.has_key?(a.to_s)
18
- value = value[a.to_sym] || value[a.to_s] || ''
19
- else
20
- return nil
21
- end
22
- else
23
- return nil
24
- end
25
- end
26
-
27
- value
5
+ def perform
6
+ resolve_variable_chain_value(matches[1])
28
7
  end
29
8
  end
30
9
  end
@@ -0,0 +1,30 @@
1
+ module Braai
2
+ module Helpers
3
+ def resolve_variable_chain_value(str)
4
+ value = nil
5
+ chain = str.split('.')
6
+
7
+ if template.attributes.has_key?(chain.first)
8
+ value = template.attributes[chain.shift] || ''
9
+ else
10
+ return nil unless value
11
+ end
12
+
13
+ chain.each do |a|
14
+ if value.respond_to?(a.to_sym)
15
+ value = value.send(a) || ''
16
+ elsif value.is_a?(Hash)
17
+ if value.has_key?(a.to_sym) || value.has_key?(a.to_s)
18
+ value = value[a.to_sym] || value[a.to_s] || ''
19
+ else
20
+ return nil
21
+ end
22
+ else
23
+ return nil
24
+ end
25
+ end
26
+
27
+ value
28
+ end
29
+ end
30
+ end
@@ -2,9 +2,10 @@ module Braai::Matchers
2
2
 
3
3
  attr_accessor :fallback
4
4
 
5
- IterationMatcher = /({{\s*for (\w+) in (\w+)\s*}}(.+?){{\s*\/for\s*}})/im
6
- DefaultMatcher = /({{\s*([\w\.]+)\s*}})/i
7
- RegionMatcher = /({{\s*([\w]+)\s*}}(.*){{\s*\/([\w]+)\s*}})/mi
5
+ IterationMatcher = /({{\s*for (\w+) in (\w+)\s*}}(.+?){{\s*\/for\s*}})/im
6
+ ConditionalMatcher = /({{\s*if\s*([\w\.]+)\s*}}(.*){{\s*\/if\s*}})/mi
7
+ DefaultMatcher = /({{\s*([\w\.]+)\s*}})/i
8
+ RegionMatcher = /({{\s*([\w]+)\s*}}(.*){{\s*\/([\w]+)\s*}})/mi
8
9
 
9
10
  def matchers
10
11
  @matchers ||= reset!
@@ -34,6 +35,7 @@ module Braai::Matchers
34
35
 
35
36
  def set_defaults
36
37
  map(IterationMatcher, Braai::Handlers::Iteration)
38
+ map(ConditionalMatcher, Braai::Handlers::Conditional)
37
39
  map(DefaultMatcher, Braai::Handlers::Default)
38
40
  end
39
41
 
@@ -1,3 +1,3 @@
1
1
  module Braai
2
- VERSION = "1.6.1"
2
+ VERSION = "1.6.2"
3
3
  end
@@ -0,0 +1,60 @@
1
+ require 'spec_helper'
2
+
3
+ describe Braai::Handlers::Conditional do
4
+ include Mocha::Integration::MiniTest
5
+ include Braai::Matchers
6
+
7
+ let(:template) do
8
+ <<-EOF
9
+ <ul>
10
+ {{ if product.active }}
11
+ <li>
12
+ <h1>{{ product.name }}</h1>
13
+ {{ if product.description }}<h2>{{ product.description }}</h2>{{ /if }}
14
+ </li>
15
+ {{ /if }}
16
+ </ul>
17
+ EOF
18
+ end
19
+
20
+ let(:inactive_product) do
21
+ OpenStruct.new(name: 'Inactive Thing', active: false, description: 'This is a thing.')
22
+ end
23
+
24
+ let(:active_product) do
25
+ OpenStruct.new(name: 'Active Thing', active: true, description: 'This is a thing.')
26
+ end
27
+
28
+ let(:missing_attribute_product) do
29
+ OpenStruct.new(name: 'No Description', active: true)
30
+ end
31
+
32
+ let(:empty_attribute_product) do
33
+ OpenStruct.new(name: 'Empty Description', active: true, description: '')
34
+ end
35
+
36
+ it "does not render conditional items that fail the test" do
37
+ res = Braai::Template.new(template).render(product: inactive_product)
38
+ res.gsub(/\s+/, '').must_match("<ul></ul>")
39
+ end
40
+
41
+ it "renders conditional items that pass the test" do
42
+ res = Braai::Template.new(template).render(product: active_product)
43
+ res.must_match(/Active Thing/)
44
+ end
45
+
46
+ it "renders the conditional string if present" do
47
+ res = Braai::Template.new(template).render(product: active_product)
48
+ res.must_match(/This is a thing/)
49
+ end
50
+
51
+ it "does not render conditional string when the value is empty" do
52
+ res = Braai::Template.new(template).render(product: empty_attribute_product)
53
+ res.wont_match("h2")
54
+ end
55
+
56
+ it "does not render conditional string when the attribute is missing" do
57
+ res = Braai::Template.new(template).render(product: missing_attribute_product)
58
+ res.wont_match("h2")
59
+ end
60
+ 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.size.must_equal(2)
47
- map("foo") {}
48
46
  matchers.size.must_equal(3)
47
+ map("foo") {}
48
+ matchers.size.must_equal(4)
49
49
  reset!
50
- matchers.size.must_equal(2)
50
+ matchers.size.must_equal(3)
51
51
  end
52
52
 
53
53
  end
@@ -59,7 +59,7 @@ describe Braai::Matchers do
59
59
  map("foo") {}
60
60
  matchers.size.must_equal(1)
61
61
  set_defaults
62
- matchers.size.must_equal(3)
62
+ matchers.size.must_equal(4)
63
63
  end
64
64
 
65
65
  end
@@ -28,6 +28,14 @@ describe 'README' do
28
28
  response.must_equal "I'm MARK and Damn, I love BBQ!!"
29
29
  end
30
30
 
31
+ it "if statement example" do
32
+ template = '{{ product.name }}{{ if product.featured }}***{{ /if }}'
33
+ featured_product = OpenStruct.new(name: 'Special Product', featured: true)
34
+ regular_product = OpenStruct.new(name: 'Regular Product', featured: false)
35
+ Braai::Template.new(template).render(product: featured_product).must_equal("Special Product***")
36
+ Braai::Template.new(template).render(product: regular_product).must_equal("Regular Product")
37
+ end
38
+
31
39
  it "for loop example" do
32
40
  template = <<-EOF
33
41
  <h1>{{ greet }}</h1>
@@ -54,4 +62,4 @@ EOF
54
62
  res.must_match("<h2>MARK</h2>")
55
63
  end
56
64
 
57
- end
65
+ 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.6.1
4
+ version: 1.6.2
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-03-11 00:00:00.000000000 Z
12
+ date: 2013-03-12 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: activesupport
@@ -62,13 +62,16 @@ files:
62
62
  - lib/braai/context.rb
63
63
  - lib/braai/handlers.rb
64
64
  - lib/braai/handlers/base.rb
65
+ - lib/braai/handlers/conditional.rb
65
66
  - lib/braai/handlers/default.rb
66
67
  - lib/braai/handlers/iteration.rb
68
+ - lib/braai/helpers.rb
67
69
  - lib/braai/matchers.rb
68
70
  - lib/braai/template.rb
69
71
  - lib/braai/version.rb
70
72
  - spec/braai/context_spec.rb
71
73
  - spec/braai/handlers/base_spec.rb
74
+ - spec/braai/handlers/conditional_spec.rb
72
75
  - spec/braai/handlers/default_spec.rb
73
76
  - spec/braai/handlers/iteration_spec.rb
74
77
  - spec/braai/matchers_spec.rb
@@ -89,7 +92,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
89
92
  version: '0'
90
93
  segments:
91
94
  - 0
92
- hash: -348262951032349536
95
+ hash: -2452820398062054300
93
96
  required_rubygems_version: !ruby/object:Gem::Requirement
94
97
  none: false
95
98
  requirements:
@@ -98,7 +101,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
98
101
  version: '0'
99
102
  segments:
100
103
  - 0
101
- hash: -348262951032349536
104
+ hash: -2452820398062054300
102
105
  requirements: []
103
106
  rubyforge_project:
104
107
  rubygems_version: 1.8.25
@@ -108,6 +111,7 @@ summary: Fully extensible templating system.
108
111
  test_files:
109
112
  - spec/braai/context_spec.rb
110
113
  - spec/braai/handlers/base_spec.rb
114
+ - spec/braai/handlers/conditional_spec.rb
111
115
  - spec/braai/handlers/default_spec.rb
112
116
  - spec/braai/handlers/iteration_spec.rb
113
117
  - spec/braai/matchers_spec.rb