bourgeois 0.1.2 → 0.1.3

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.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: c11f36f9736527cc188110608030455134cddba8
4
+ data.tar.gz: 4c7538b7646ba25cb9bd2507471f3a7c245e25a1
5
+ SHA512:
6
+ metadata.gz: 0a5733b31c578036c402e2faf15f80c1c8d3e25e3c97d1974cddd525e86024f0e39b32fd05f779113973b0b235a83e7be38ab38d4a3431c9ca65b0dcab07d7c9
7
+ data.tar.gz: 47ccaa03c683e442c3c43e23ae0d7bf8aa2024d67db9cdc016fca205e41fa1707cbf0c5a50731cb7190f9b49fa6d83e27d6719df0edf7bc72fc3c7f1aad68827
data/README.md CHANGED
@@ -1,10 +1,18 @@
1
- # Bourgeois
2
-
3
- [![Gem Version](https://badge.fury.io/rb/bourgeois.png)](https://rubygems.org/gems/bourgeois)
4
- [![Code Climate](https://codeclimate.com/github/mirego/bourgeois.png)](https://codeclimate.com/github/mirego/bourgeois)
5
- [![Build Status](https://travis-ci.org/mirego/bourgeois.png?branch=master)](https://travis-ci.org/mirego/bourgeois)
6
-
7
- Bourgeois is a Ruby library that makes using presenters a very simple thing.
1
+ <p align="center">
2
+ <a href="https://github.com/mirego/bourgeois">
3
+ <img src="http://i.imgur.com/Z8ja8Wz.png" alt="Bourgeois" />
4
+ </a>
5
+ <br />
6
+ Bourgeois is a Ruby library that makes using presenters a very simple thing.
7
+ <br /><br />
8
+ <a href="https://rubygems.org/gems/bourgeois"><img src="https://badge.fury.io/rb/bourgeois.png" /></a>
9
+ <a href="https://codeclimate.com/github/mirego/bourgeois"><img src="https://codeclimate.com/github/mirego/bourgeois.png" /></a>
10
+ <a href='https://coveralls.io/r/mirego/bourgeois?branch=master'><img src='https://coveralls.io/repos/mirego/bourgeois/badge.png?branch=master' /></a>
11
+ <a href='https://gemnasium.com/mirego/bourgeois'><img src="https://gemnasium.com/mirego/bourgeois.png" /></a>
12
+ <a href="https://travis-ci.org/mirego/bourgeois"><img src="https://travis-ci.org/mirego/bourgeois.png?branch=master" /></a>
13
+ </p>
14
+
15
+ ---
8
16
 
9
17
  ## Installation
10
18
 
@@ -55,6 +63,31 @@ class UserPresenter < Bourgeois::Presenter
55
63
  end
56
64
  ```
57
65
 
66
+ ### Custom block helpers
67
+
68
+ You can use the simple `helper` DSL to define block helpers that will be executed if certain
69
+ conditions are matched.
70
+
71
+ ```ruby
72
+ class UserPresenter < Bourgeois::Presenter
73
+ helper :with_profile, if: -> { profile.present? && profile.public? }
74
+ end
75
+
76
+ User.first.new = Profile.create(public: true, title: 'Foo', description: 'Bar')
77
+ ```
78
+
79
+ ```erb
80
+ <% present User.first do |user| %>
81
+ <h1><%= user.full_name %></h1>
82
+ <% user.with_profile do %>
83
+ <div class="profile">
84
+ <h2><%= user.profile.title %></h2>
85
+ <%= simple_format(user.profile.description) %>
86
+ </div>
87
+ <% end %>
88
+ <% end %>
89
+ ```
90
+
58
91
  ## Inspiration
59
92
 
60
93
  Bourgeois was inspired by some code [@rafBM](https://twitter.com/rafBM) wrote for [his OpenCode talk](https://github.com/rafBM/opencode12-rails) on May 28th, 2013.
data/bourgeois.gemspec CHANGED
@@ -24,4 +24,5 @@ Gem::Specification.new do |spec|
24
24
  spec.add_development_dependency 'rspec', '~> 2.13'
25
25
  spec.add_development_dependency 'bundler', '~> 1.3'
26
26
  spec.add_development_dependency 'rake'
27
+ spec.add_development_dependency 'coveralls'
27
28
  end
@@ -26,6 +26,34 @@ module Bourgeois
26
26
  klass.human_attribute_name(*args)
27
27
  end
28
28
 
29
+ # Declare a new block helper method
30
+ #
31
+ # @example
32
+ # class UserPresenter < Bourgeois::Presenter
33
+ # helper :with_profile, if: -> { profile.present? }
34
+ # end
35
+ #
36
+ # presenter = UserPresenter.new(User.new(profile: 'Foo'))
37
+ # presenter.with_profile do
38
+ # puts 'User has a profile:'
39
+ # puts presenter.profile
40
+ # end
41
+ def self.helper(name, opts = {})
42
+ define_method(name) do |&block|
43
+ execute = true
44
+
45
+ if opts[:if]
46
+ execute = execute && self.instance_exec(&opts[:if])
47
+ end
48
+
49
+ if opts[:unless]
50
+ execute = execute && !self.instance_exec(&opts[:unless])
51
+ end
52
+
53
+ block.call if execute
54
+ end
55
+ end
56
+
29
57
  private
30
58
 
31
59
  # Return the view from where the presenter was created
@@ -1,3 +1,3 @@
1
1
  module Bourgeois
2
- VERSION = "0.1.2"
2
+ VERSION = '0.1.3'
3
3
  end
@@ -101,6 +101,132 @@ describe Bourgeois::Presenter do
101
101
  end
102
102
 
103
103
  describe :ClassMethods do
104
+ describe :helper do
105
+ before do
106
+ class UserPresenter < Bourgeois::Presenter
107
+ # We need a method to test that our block is executed
108
+ attr_reader :foo
109
+ end
110
+ end
111
+
112
+ let(:call_it!) do
113
+ presenter.send(helper) do
114
+ presenter.foo
115
+ end
116
+ end
117
+
118
+ context 'with helper using only a if condition' do
119
+ before do
120
+ class UserPresenter
121
+ helper :with_profile, if: -> { profile.present? }
122
+ end
123
+ end
124
+
125
+ context 'with matching if condition' do
126
+ let(:user) { User.new profile: 'Je suis Patrick.' }
127
+ let(:helper) { :with_profile }
128
+
129
+ specify do
130
+ presenter.should_receive(:foo).once
131
+ call_it!
132
+ end
133
+ end
134
+
135
+ context 'with non-matching if condition' do
136
+ let(:user) { User.new profile: nil }
137
+ let(:helper) { :with_profile }
138
+
139
+ specify do
140
+ presenter.should_receive(:foo).never
141
+ call_it!
142
+ end
143
+ end
144
+ end
145
+
146
+ context 'with helper using only an unless condition' do
147
+ let(:helper) { :without_name }
148
+ before do
149
+ class UserPresenter
150
+ helper :without_name, unless: -> { full_name.present? }
151
+ end
152
+ end
153
+
154
+ context 'with matching unless condition' do
155
+ let(:user) { User.new full_name: nil }
156
+
157
+ specify do
158
+ presenter.should_receive(:foo).once
159
+ call_it!
160
+ end
161
+ end
162
+
163
+ context 'with non-matching unless condition' do
164
+ let(:user) { User.new full_name: 'Patrick Bourgeois' }
165
+
166
+ specify do
167
+ presenter.should_receive(:foo).never
168
+ call_it!
169
+ end
170
+ end
171
+ end
172
+
173
+ context 'with helper without if nor unless' do
174
+ let(:user) { User.new }
175
+ let(:helper) { :with_something }
176
+ before do
177
+ class UserPresenter
178
+ helper :with_something
179
+ end
180
+ end
181
+
182
+ specify do
183
+ presenter.should_receive(:foo).once
184
+ call_it!
185
+ end
186
+ end
187
+
188
+ context 'with helper using both matching and unless conditions' do
189
+ let(:helper) { :sometimes }
190
+ before do
191
+ class UserPresenter
192
+ helper :sometimes, if: -> { profile.present? }, unless: -> { full_name.present? }
193
+ end
194
+ end
195
+
196
+ context 'with matching if and non-matching unless condition' do
197
+ let(:user) { User.new(profile: true, full_name: 'Patrick Bourgeois') }
198
+ specify do
199
+ presenter.should_receive(:foo).never
200
+ call_it!
201
+ end
202
+ end
203
+
204
+ context 'with non-matching if and non-matching unless condition' do
205
+ let(:user) { User.new(profile: false, full_name: 'Patrick Bourgeois') }
206
+ specify do
207
+ presenter.should_receive(:foo).never
208
+ call_it!
209
+ end
210
+ end
211
+
212
+ context 'with matching if and matching unless condition' do
213
+ let(:user) { User.new(profile: true, full_name: nil) }
214
+ specify do
215
+ presenter.should_receive(:foo).once
216
+ call_it!
217
+ end
218
+ end
219
+
220
+ context 'with non-matching if and matching unless condition' do
221
+ let(:user) { User.new(profile: false, full_name: 'Patrick Bourgeois') }
222
+ specify do
223
+ presenter.should_receive(:foo).never
224
+ call_it!
225
+ end
226
+ end
227
+ end
228
+ end
229
+
104
230
  describe :kind_of? do
105
231
  before do
106
232
  class UserPresenter < Bourgeois::Presenter; end
data/spec/spec_helper.rb CHANGED
@@ -1,5 +1,8 @@
1
1
  $:.unshift File.expand_path('../../lib', __FILE__)
2
2
 
3
+ require 'coveralls'
4
+ Coveralls.wear!
5
+
3
6
  require 'ostruct'
4
7
  require 'rspec'
5
8
  require 'bourgeois'
metadata CHANGED
@@ -1,52 +1,46 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: bourgeois
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.2
5
- prerelease:
4
+ version: 0.1.3
6
5
  platform: ruby
7
6
  authors:
8
7
  - Rémi Prévost
9
8
  autorequire:
10
9
  bindir: bin
11
10
  cert_chain: []
12
- date: 2013-08-14 00:00:00.000000000 Z
11
+ date: 2013-10-22 00:00:00.000000000 Z
13
12
  dependencies:
14
13
  - !ruby/object:Gem::Dependency
15
14
  name: activemodel
16
15
  requirement: !ruby/object:Gem::Requirement
17
- none: false
18
16
  requirements:
19
- - - ! '>='
17
+ - - '>='
20
18
  - !ruby/object:Gem::Version
21
19
  version: 3.0.0
22
20
  type: :runtime
23
21
  prerelease: false
24
22
  version_requirements: !ruby/object:Gem::Requirement
25
- none: false
26
23
  requirements:
27
- - - ! '>='
24
+ - - '>='
28
25
  - !ruby/object:Gem::Version
29
26
  version: 3.0.0
30
27
  - !ruby/object:Gem::Dependency
31
28
  name: actionpack
32
29
  requirement: !ruby/object:Gem::Requirement
33
- none: false
34
30
  requirements:
35
- - - ! '>='
31
+ - - '>='
36
32
  - !ruby/object:Gem::Version
37
33
  version: 3.0.0
38
34
  type: :runtime
39
35
  prerelease: false
40
36
  version_requirements: !ruby/object:Gem::Requirement
41
- none: false
42
37
  requirements:
43
- - - ! '>='
38
+ - - '>='
44
39
  - !ruby/object:Gem::Version
45
40
  version: 3.0.0
46
41
  - !ruby/object:Gem::Dependency
47
42
  name: rspec
48
43
  requirement: !ruby/object:Gem::Requirement
49
- none: false
50
44
  requirements:
51
45
  - - ~>
52
46
  - !ruby/object:Gem::Version
@@ -54,7 +48,6 @@ dependencies:
54
48
  type: :development
55
49
  prerelease: false
56
50
  version_requirements: !ruby/object:Gem::Requirement
57
- none: false
58
51
  requirements:
59
52
  - - ~>
60
53
  - !ruby/object:Gem::Version
@@ -62,7 +55,6 @@ dependencies:
62
55
  - !ruby/object:Gem::Dependency
63
56
  name: bundler
64
57
  requirement: !ruby/object:Gem::Requirement
65
- none: false
66
58
  requirements:
67
59
  - - ~>
68
60
  - !ruby/object:Gem::Version
@@ -70,7 +62,6 @@ dependencies:
70
62
  type: :development
71
63
  prerelease: false
72
64
  version_requirements: !ruby/object:Gem::Requirement
73
- none: false
74
65
  requirements:
75
66
  - - ~>
76
67
  - !ruby/object:Gem::Version
@@ -78,17 +69,29 @@ dependencies:
78
69
  - !ruby/object:Gem::Dependency
79
70
  name: rake
80
71
  requirement: !ruby/object:Gem::Requirement
81
- none: false
82
72
  requirements:
83
- - - ! '>='
73
+ - - '>='
84
74
  - !ruby/object:Gem::Version
85
75
  version: '0'
86
76
  type: :development
87
77
  prerelease: false
88
78
  version_requirements: !ruby/object:Gem::Requirement
89
- none: false
90
79
  requirements:
91
- - - ! '>='
80
+ - - '>='
81
+ - !ruby/object:Gem::Version
82
+ version: '0'
83
+ - !ruby/object:Gem::Dependency
84
+ name: coveralls
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - '>='
88
+ - !ruby/object:Gem::Version
89
+ version: '0'
90
+ type: :development
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - '>='
92
95
  - !ruby/object:Gem::Version
93
96
  version: '0'
94
97
  description: Bourgeois is a Ruby library that makes using presenters a very simple
@@ -121,33 +124,26 @@ files:
121
124
  homepage: https://github.com/mirego/bourgeois
122
125
  licenses:
123
126
  - BSD 3-Clause
127
+ metadata: {}
124
128
  post_install_message:
125
129
  rdoc_options: []
126
130
  require_paths:
127
131
  - lib
128
132
  required_ruby_version: !ruby/object:Gem::Requirement
129
- none: false
130
133
  requirements:
131
- - - ! '>='
134
+ - - '>='
132
135
  - !ruby/object:Gem::Version
133
136
  version: '0'
134
- segments:
135
- - 0
136
- hash: 1321347131962644702
137
137
  required_rubygems_version: !ruby/object:Gem::Requirement
138
- none: false
139
138
  requirements:
140
- - - ! '>='
139
+ - - '>='
141
140
  - !ruby/object:Gem::Version
142
141
  version: '0'
143
- segments:
144
- - 0
145
- hash: 1321347131962644702
146
142
  requirements: []
147
143
  rubyforge_project:
148
- rubygems_version: 1.8.23
144
+ rubygems_version: 2.1.0
149
145
  signing_key:
150
- specification_version: 3
146
+ specification_version: 4
151
147
  summary: Bourgeois is a Ruby library that makes using presenters a very simple thing.
152
148
  test_files:
153
149
  - spec/bourgeois/presenter_spec.rb