bourgeois 1.0 → 1.0.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: f2bc10e6f30ee29a0b8bb2fd612ef97f8f67cf49
4
- data.tar.gz: c31c4dba94b71fcf88b06523325523644863f633
3
+ metadata.gz: c9b43bb505e57adbf599131cc6bdd4cad655630c
4
+ data.tar.gz: 06e720c2028ef5ba363f670106a820ea6057d2d7
5
5
  SHA512:
6
- metadata.gz: 8f635a2839bf1ad9d0a3a1b2825103675cd284062b080a4eb774df234fbbf9803333f2ba27d0706b1cbbaba11dc618ea2c8849c5a6c699ae7f911b75706dd8b1
7
- data.tar.gz: 56923d9ed6669f71491150fb08bd65885e9363cb57e2d62f057ab9f48ba8c2dd0d736d012e840fcbcf7932dc23f414b6f02cfd67f084c4071eee460a0b0f4783
6
+ metadata.gz: ee3c41269548658fc037843c9c57a94edf01ce021e03a8c7d6f60b0d21b9a0b097bf1b245689f22288213a27b64e88ac7ce0ed6c15db4114b5b2c3e64f50d760
7
+ data.tar.gz: ce66af27e40e000b56f78b68ebf22d8c7f23ca4a23449317e78abc6c22931af644cfcaf464d08dcae6bf105076f5933e9e41a97b57735326454ae926777dc35a
data/.rubocop.yml ADDED
@@ -0,0 +1,42 @@
1
+ AllCops:
2
+ Includes:
3
+ - Gemfile
4
+ - Rakefile
5
+
6
+ Documentation:
7
+ Enabled: false
8
+
9
+ Encoding:
10
+ Enabled: false
11
+
12
+ LineLength:
13
+ Max: 200
14
+
15
+ AccessModifierIndentation:
16
+ EnforcedStyle: outdent
17
+
18
+ IfUnlessModifier:
19
+ Enabled: false
20
+
21
+ CaseIndentation:
22
+ IndentWhenRelativeTo: case
23
+ IndentOneStep: true
24
+
25
+ MethodLength:
26
+ CountComments: false
27
+ Max: 20
28
+
29
+ SignalException:
30
+ Enabled: false
31
+
32
+ ColonMethodCall:
33
+ Enabled: false
34
+
35
+ AsciiComments:
36
+ Enabled: false
37
+
38
+ Lambda:
39
+ Enabled: false
40
+
41
+ RegexpLiteral:
42
+ Enabled: false
data/.travis.yml CHANGED
@@ -9,3 +9,11 @@ gemfile:
9
9
  - gemfiles/Gemfile.activemodel-3.2.x
10
10
 
11
11
  script: "echo 'DO IT' && bundle exec rake spec"
12
+
13
+ notifications:
14
+ hipchat:
15
+ rooms:
16
+ secure: "Xo3Uv+mvgPuQjF5J5T05mGoxE8LeoTrBjBRvnFSnX2tmrKvKoBLG0LMIg0A8TpI0usohnk7xF3FbAFq4n3xTnkCsrU3KAWOIw5c7Hh0wVTM67A8qh26+RVJSE/8oSUebCJxdiC2uSQcAsz405RR23FP/z8luKLtc6xKkqNMrJSM="
17
+ template:
18
+ - '%{repository}#%{build_number} (%{branch} - %{commit} : %{author}): %{message} (<a href="%{build_url}">Build</a>/<a href="%{compare_url}">Changes</a>)'
19
+ format: 'html'
data/README.md CHANGED
@@ -98,6 +98,6 @@ Bourgeois was inspired by some code [@rafBM](https://twitter.com/rafBM) wrote fo
98
98
 
99
99
  ## About Mirego
100
100
 
101
- Mirego is a team of passionate people who believe that work is a place where you can innovate and have fun. We proudly build mobile applications for [iPhone](http://mirego.com/en/iphone-app-development/ "iPhone application development"), [iPad](http://mirego.com/en/ipad-app-development/ "iPad application development"), [Android](http://mirego.com/en/android-app-development/ "Android application development"), [Blackberry](http://mirego.com/en/blackberry-app-development/ "Blackberry application development"), [Windows Phone](http://mirego.com/en/windows-phone-app-development/ "Windows Phone application development") and [Windows 8](http://mirego.com/en/windows-8-app-development/ "Windows 8 application development") in beautiful Quebec City.
101
+ [Mirego](http://mirego.com) is a team of passionate people who believe that work is a place where you can innovate and have fun. We're a team of [talented people](http://life.mirego.com) who imagine and build beautiful Web and mobile applications. We come together to share ideas and [change the world](http://mirego.org).
102
102
 
103
- We also love [open-source software](http://open.mirego.com/) and we try to extract as much code as possible from our projects to give back to the community.
103
+ We also [love open-source software](http://open.mirego.com) and we try to give back to the community as much as we can.
data/bourgeois.gemspec CHANGED
@@ -25,4 +25,5 @@ Gem::Specification.new do |spec|
25
25
  spec.add_development_dependency 'bundler', '~> 1.3'
26
26
  spec.add_development_dependency 'rake'
27
27
  spec.add_development_dependency 'coveralls'
28
+ spec.add_development_dependency 'rubocop'
28
29
  end
data/lib/bourgeois.rb CHANGED
@@ -10,7 +10,7 @@ require 'bourgeois/view_helper'
10
10
 
11
11
  module Bourgeois
12
12
  def self.inject_into_action_view
13
- @inject_into_action_view ||= Proc.new do
13
+ @inject_into_action_view ||= proc do
14
14
  ActionView::Base.send(:include, ViewHelper)
15
15
  end
16
16
  end
@@ -1,5 +1,8 @@
1
1
  module Bourgeois
2
2
  class Presenter < ::SimpleDelegator
3
+ # Return the original delegated object
4
+ attr_reader :object
5
+
3
6
  def initialize(object, view = nil)
4
7
  @view = view
5
8
  super(@object = object)
@@ -40,36 +43,37 @@ module Bourgeois
40
43
  # end
41
44
  def self.helper(name, opts = {})
42
45
  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
46
+ execute_helper(block, opts)
54
47
  end
55
48
  end
56
49
 
57
50
  private
58
51
 
59
52
  # Return the view from where the presenter was created
60
- def view
61
- @view
62
- end
63
-
64
- # Return the original delegated object
65
- def object
66
- @object
67
- end
53
+ attr_reader :view
68
54
 
69
55
  # Return the original object class based on the presenter class name
70
56
  # We would be able to use `@object.class` but we need this in class methods
71
57
  def self.klass
72
- @klass ||= self.name.split(/Presenter$/).first.constantize
58
+ @klass ||= name.split(/Presenter$/).first.constantize
59
+ end
60
+
61
+ # Execute a helper block if it matches conditions
62
+ def execute_helper(block, opts)
63
+ if_condition = execute_helper_condition(opts[:if])
64
+ unless_condition = !execute_helper_condition(opts[:unless], false)
65
+
66
+ block.call if if_condition && unless_condition
67
+ end
68
+
69
+ # Execute a block within the context of the instance and return
70
+ # the result. Return a default value if the passed block is blank
71
+ def execute_helper_condition(block, default = true)
72
+ if block.blank?
73
+ default
74
+ else
75
+ instance_exec(&block)
76
+ end
73
77
  end
74
78
  end
75
79
  end
@@ -1,3 +1,3 @@
1
1
  module Bourgeois
2
- VERSION = '1.0'
2
+ VERSION = '1.0.1'
3
3
  end
@@ -8,19 +8,13 @@ module Bourgeois
8
8
  # puts user.name # => Remi
9
9
  # end
10
10
  def present(object, klass = nil, &blk)
11
- return object.map { |o| present(o, klass, &blk) } if object.respond_to?(:to_a)
11
+ return if object.nil?
12
+ return object.map { |o| present(o, klass, &blk) } if object.respond_to?(:to_a) && !object.is_a?(Struct)
12
13
 
13
- if klass.blank?
14
- if object.is_a?(Bourgeois::Presenter)
15
- presenter = object
16
- else
17
- begin
18
- klass_name = "#{object.class}Presenter"
19
- klass = klass_name.constantize
20
- rescue ::NameError
21
- raise UnknownPresenter.new(klass_name)
22
- end
23
- end
14
+ if object.is_a?(Bourgeois::Presenter)
15
+ presenter = object
16
+ else
17
+ klass ||= ViewHelper.presenter_class(object)
24
18
  end
25
19
 
26
20
  presenter ||= klass.new(object, self)
@@ -28,5 +22,12 @@ module Bourgeois
28
22
 
29
23
  presenter
30
24
  end
25
+
26
+ def self.presenter_class(object)
27
+ klass_name = "#{object.class}Presenter"
28
+ klass_name.constantize
29
+ rescue ::NameError
30
+ raise UnknownPresenter, klass_name
31
+ end
31
32
  end
32
33
  end
@@ -34,7 +34,7 @@ describe Bourgeois::Presenter do
34
34
  class User < OpenStruct; end
35
35
  end
36
36
 
37
- it { expect{ UserPresenter.new(user) }.to_not raise_error }
37
+ it { expect { UserPresenter.new(user) }.to_not raise_error }
38
38
  end
39
39
 
40
40
  describe :view do
@@ -14,6 +14,22 @@ describe Bourgeois::ViewHelper do
14
14
  class User < OpenStruct; end
15
15
  end
16
16
 
17
+ context 'on a Nil object' do
18
+ context 'without a block' do
19
+ it { expect { view.present(nil) }.not_to raise_error }
20
+ end
21
+
22
+ context 'with a block' do
23
+ before { UserPresenter.any_instance.should_receive(:formatted_name).never }
24
+
25
+ specify do
26
+ expect do
27
+ view.present(nil) { |obj| obj.formatted_name }
28
+ end.not_to raise_error
29
+ end
30
+ end
31
+ end
32
+
17
33
  context 'on a single resource' do
18
34
  let(:user) { User.new first_name: 'Patrick', last_name: 'Bourgeois' }
19
35
 
@@ -83,5 +99,60 @@ describe Bourgeois::ViewHelper do
83
99
  let(:project) { Project.new name: 'Les B.B.' }
84
100
  it { expect { view.present(project) }.to raise_error(Bourgeois::UnknownPresenter, 'unknown presenter class ProjectPresenter') }
85
101
  end
102
+
103
+ context 'on a resource with a custom presenter class' do
104
+ before do
105
+ class Article < OpenStruct; end
106
+ class CustomArticlePresenter < Bourgeois::Presenter
107
+ def name
108
+ super.upcase
109
+ end
110
+ end
111
+ end
112
+
113
+ let(:article) { Article.new name: 'Les B.B.' }
114
+ let(:presented_article) { view.present(article, CustomArticlePresenter) }
115
+
116
+ it { expect { presented_article }.not_to raise_error }
117
+ it { expect(presented_article.name).to eql 'LES B.B.' }
118
+ end
119
+
120
+ context 'on a Struct-based resource' do
121
+ before do
122
+ class Band < Struct.new(:name)
123
+ end
124
+
125
+ class BandPresenter < Bourgeois::Presenter
126
+ def name
127
+ super.upcase
128
+ end
129
+ end
130
+ end
131
+
132
+ let(:band) { Band.new('Les B.B.') }
133
+ let(:presented_band) { view.present(band) }
134
+
135
+ it { expect(presented_band.name).to eql 'LES B.B.' }
136
+ end
137
+
138
+ context 'on a collection of resources with a custom presenter class' do
139
+ before do
140
+ class Article < OpenStruct; end
141
+ class CustomArticlePresenter < Bourgeois::Presenter
142
+ def name
143
+ super.upcase
144
+ end
145
+ end
146
+ end
147
+
148
+ let(:articles) { [Article.new(name: 'Les B.B.'), Article.new(name: 'Rock et Belles Oreilles')] }
149
+
150
+ specify do
151
+ output = []
152
+ view.present(articles, CustomArticlePresenter) { |u| output << u.name }
153
+
154
+ expect(output).to eql ['LES B.B.', 'ROCK ET BELLES OREILLES']
155
+ end
156
+ end
86
157
  end
87
158
  end
data/spec/spec_helper.rb CHANGED
@@ -1,4 +1,4 @@
1
- $:.unshift File.expand_path('../../lib', __FILE__)
1
+ $LOAD_PATH.unshift File.expand_path('../../lib', __FILE__)
2
2
 
3
3
  require 'coveralls'
4
4
  Coveralls.wear!
metadata CHANGED
@@ -1,97 +1,111 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: bourgeois
3
3
  version: !ruby/object:Gem::Version
4
- version: '1.0'
4
+ version: 1.0.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Rémi Prévost
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2013-12-13 00:00:00.000000000 Z
11
+ date: 2014-06-19 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activemodel
15
15
  requirement: !ruby/object:Gem::Requirement
16
16
  requirements:
17
- - - '>='
17
+ - - ">="
18
18
  - !ruby/object:Gem::Version
19
19
  version: 3.0.0
20
20
  type: :runtime
21
21
  prerelease: false
22
22
  version_requirements: !ruby/object:Gem::Requirement
23
23
  requirements:
24
- - - '>='
24
+ - - ">="
25
25
  - !ruby/object:Gem::Version
26
26
  version: 3.0.0
27
27
  - !ruby/object:Gem::Dependency
28
28
  name: actionpack
29
29
  requirement: !ruby/object:Gem::Requirement
30
30
  requirements:
31
- - - '>='
31
+ - - ">="
32
32
  - !ruby/object:Gem::Version
33
33
  version: 3.0.0
34
34
  type: :runtime
35
35
  prerelease: false
36
36
  version_requirements: !ruby/object:Gem::Requirement
37
37
  requirements:
38
- - - '>='
38
+ - - ">="
39
39
  - !ruby/object:Gem::Version
40
40
  version: 3.0.0
41
41
  - !ruby/object:Gem::Dependency
42
42
  name: rspec
43
43
  requirement: !ruby/object:Gem::Requirement
44
44
  requirements:
45
- - - ~>
45
+ - - "~>"
46
46
  - !ruby/object:Gem::Version
47
47
  version: '2.13'
48
48
  type: :development
49
49
  prerelease: false
50
50
  version_requirements: !ruby/object:Gem::Requirement
51
51
  requirements:
52
- - - ~>
52
+ - - "~>"
53
53
  - !ruby/object:Gem::Version
54
54
  version: '2.13'
55
55
  - !ruby/object:Gem::Dependency
56
56
  name: bundler
57
57
  requirement: !ruby/object:Gem::Requirement
58
58
  requirements:
59
- - - ~>
59
+ - - "~>"
60
60
  - !ruby/object:Gem::Version
61
61
  version: '1.3'
62
62
  type: :development
63
63
  prerelease: false
64
64
  version_requirements: !ruby/object:Gem::Requirement
65
65
  requirements:
66
- - - ~>
66
+ - - "~>"
67
67
  - !ruby/object:Gem::Version
68
68
  version: '1.3'
69
69
  - !ruby/object:Gem::Dependency
70
70
  name: rake
71
71
  requirement: !ruby/object:Gem::Requirement
72
72
  requirements:
73
- - - '>='
73
+ - - ">="
74
74
  - !ruby/object:Gem::Version
75
75
  version: '0'
76
76
  type: :development
77
77
  prerelease: false
78
78
  version_requirements: !ruby/object:Gem::Requirement
79
79
  requirements:
80
- - - '>='
80
+ - - ">="
81
81
  - !ruby/object:Gem::Version
82
82
  version: '0'
83
83
  - !ruby/object:Gem::Dependency
84
84
  name: coveralls
85
85
  requirement: !ruby/object:Gem::Requirement
86
86
  requirements:
87
- - - '>='
87
+ - - ">="
88
88
  - !ruby/object:Gem::Version
89
89
  version: '0'
90
90
  type: :development
91
91
  prerelease: false
92
92
  version_requirements: !ruby/object:Gem::Requirement
93
93
  requirements:
94
- - - '>='
94
+ - - ">="
95
+ - !ruby/object:Gem::Version
96
+ version: '0'
97
+ - !ruby/object:Gem::Dependency
98
+ name: rubocop
99
+ requirement: !ruby/object:Gem::Requirement
100
+ requirements:
101
+ - - ">="
102
+ - !ruby/object:Gem::Version
103
+ version: '0'
104
+ type: :development
105
+ prerelease: false
106
+ version_requirements: !ruby/object:Gem::Requirement
107
+ requirements:
108
+ - - ">="
95
109
  - !ruby/object:Gem::Version
96
110
  version: '0'
97
111
  description: Bourgeois is a Ruby library that makes using presenters a very simple
@@ -102,9 +116,10 @@ executables: []
102
116
  extensions: []
103
117
  extra_rdoc_files: []
104
118
  files:
105
- - .gitignore
106
- - .rspec
107
- - .travis.yml
119
+ - ".gitignore"
120
+ - ".rspec"
121
+ - ".rubocop.yml"
122
+ - ".travis.yml"
108
123
  - Gemfile
109
124
  - LICENSE.md
110
125
  - README.md
@@ -131,17 +146,17 @@ require_paths:
131
146
  - lib
132
147
  required_ruby_version: !ruby/object:Gem::Requirement
133
148
  requirements:
134
- - - '>='
149
+ - - ">="
135
150
  - !ruby/object:Gem::Version
136
151
  version: '0'
137
152
  required_rubygems_version: !ruby/object:Gem::Requirement
138
153
  requirements:
139
- - - '>='
154
+ - - ">="
140
155
  - !ruby/object:Gem::Version
141
156
  version: '0'
142
157
  requirements: []
143
158
  rubyforge_project:
144
- rubygems_version: 2.1.0
159
+ rubygems_version: 2.2.2
145
160
  signing_key:
146
161
  specification_version: 4
147
162
  summary: Bourgeois is a Ruby library that makes using presenters a very simple thing.