upholsterer 0.4.1 → 0.4.2

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: 2a785aab5485f2ce77dab6936674769fac4826e6
4
- data.tar.gz: 7078a020e6053b70e45e52a660ade06265fbdefd
3
+ metadata.gz: 951c39ccba5741d0f3addf2eef2352f3271733bf
4
+ data.tar.gz: 5c6b534c4197de44df7f0a52a426b903defa9739
5
5
  SHA512:
6
- metadata.gz: 24cd41a535c28fd31a1627bd9c12e47640724558131b0b931c41b226ee2b824100ffd7455de7cd6a1c3d17f3081aa0b55d4fb100ec72a0d6c111d8a50534c529
7
- data.tar.gz: 567357bb89d0b137f1f3d123e1fa57694ef3c53e2055df247300621bcf7c4a510a46d593fb83d59ef092820a8379af747128d06d6a0b7ef64b56d48397ae6316
6
+ metadata.gz: 9ff954b798cd7f48f522e9c55ffa85d602a067699d090d642685d6dbbcb624258ea46e8ac539761f16df1f8414bae1dd91f42f23a353fa51864e4a10e9867c14
7
+ data.tar.gz: 16ec28b9b63b9aa81b7a10b1fd33cc6658907b1b70e441f349b57895890192f2934c7ce53f43fce095b211ae5c99378a9777cc6ccb12135790fcf658255b0f78
data/Gemfile CHANGED
@@ -1,2 +1,4 @@
1
- source "https://rubygems.org"
1
+ source 'https://rubygems.org'
2
2
  gemspec
3
+
4
+ gem 'activesupport', require: 'active_support/all'
data/Gemfile.lock CHANGED
@@ -1,15 +1,25 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- upholsterer (0.4.1)
4
+ upholsterer (0.4.2)
5
+ activesupport
5
6
 
6
7
  GEM
7
8
  remote: https://rubygems.org/
8
9
  specs:
10
+ activesupport (4.1.0)
11
+ i18n (~> 0.6, >= 0.6.9)
12
+ json (~> 1.7, >= 1.7.7)
13
+ minitest (~> 5.1)
14
+ thread_safe (~> 0.1)
15
+ tzinfo (~> 1.1)
9
16
  awesome_print (1.2.0)
10
17
  coderay (1.0.9)
11
18
  diff-lcs (1.2.4)
19
+ i18n (0.6.11)
20
+ json (1.8.1)
12
21
  method_source (0.8.2)
22
+ minitest (5.4.2)
13
23
  pry (0.9.12.2)
14
24
  coderay (~> 1.0.5)
15
25
  method_source (~> 0.8)
@@ -34,11 +44,15 @@ GEM
34
44
  diff-lcs (>= 1.1.3, < 2.0)
35
45
  rspec-mocks (2.14.4)
36
46
  slop (3.4.6)
47
+ thread_safe (0.3.4)
48
+ tzinfo (1.2.2)
49
+ thread_safe (~> 0.1)
37
50
 
38
51
  PLATFORMS
39
52
  ruby
40
53
 
41
54
  DEPENDENCIES
55
+ activesupport
42
56
  pry-meta
43
57
  rake
44
58
  rspec
@@ -97,25 +97,53 @@ module Upholsterer
97
97
  # The presenter above will expose the methods +body+, +created_at+, and +user_name+.
98
98
  # Additionaly, it will create an alias called +author+ to the +name+ attribute.
99
99
  #
100
- def self.expose(*attrs)
100
+ def self.expose(*attrs, &block)
101
101
  options = attrs.pop if attrs.last.kind_of?(Hash)
102
102
  options ||= {}
103
103
 
104
- attrs.each do |attr_name|
105
- subject = options.fetch(:with, nil)
104
+ container = options.fetch(:with, nil)
105
+ method_prefix = container if options.fetch(:prefix, true)
106
+
107
+ if block_given? and container.present?
108
+ wrapper_method = "#{ attrs.join('_') }_wrapper"
109
+ wrapper_instance_variable = "@#{ wrapper_method }"
110
+
111
+ define_method wrapper_method do
112
+ unless instance_variable_defined?(wrapper_instance_variable)
113
+ if respond_to?(container)
114
+ object_container = public_send(container)
115
+ else
116
+ object_container = subject.public_send(container)
117
+ end
106
118
 
119
+ wrapper = instance_eval(&block).new(object_container)
120
+
121
+ instance_variable_set(wrapper_instance_variable, wrapper)
122
+ end
123
+
124
+ instance_variable_get(wrapper_instance_variable)
125
+ end
126
+
127
+ private wrapper_method
128
+ end
129
+
130
+ attrs.each do |attr_name|
107
131
  method_name = [
108
- subject,
132
+ method_prefix,
109
133
  options.fetch(:as, attr_name)
110
- ].compact.join("_")
134
+ ].compact.join('_')
111
135
 
112
136
  attributes[method_name.to_sym] = [attr_name, options]
113
137
 
114
- class_eval <<-RUBY, __FILE__, __LINE__ + 1
115
- def #{method_name}(&block) # def user_name(&block)
116
- proxy_message(#{subject.inspect}, "#{attr_name}", &block) # proxy_message("user", "name")
117
- end # end
118
- RUBY
138
+ if block_given? and container.present?
139
+ delegate attr_name, to: wrapper_method, prefix: !!method_prefix
140
+ else
141
+ class_eval <<-RUBY, __FILE__, __LINE__ + 1
142
+ def #{method_name}(&block) # def user_name(&block)
143
+ proxy_message(#{container.inspect}, "#{attr_name}", &block) # proxy_message("user", "name")
144
+ end # end
145
+ RUBY
146
+ end
119
147
  end
120
148
  end
121
149
 
@@ -2,7 +2,7 @@ module Upholsterer
2
2
  module Version
3
3
  MAJOR = 0
4
4
  MINOR = 4
5
- PATCH = 1
5
+ PATCH = 2
6
6
  STRING = "#{MAJOR}.#{MINOR}.#{PATCH}"
7
7
  end
8
8
  end
@@ -20,4 +20,5 @@ Gem::Specification.new do |s|
20
20
  s.add_development_dependency 'rake'
21
21
  s.add_development_dependency 'rspec'
22
22
  s.add_development_dependency 'pry-meta'
23
+ s.add_dependency 'activesupport'
23
24
  end
data/spec/base_spec.rb CHANGED
@@ -178,4 +178,30 @@ describe Upholsterer::Base do
178
178
  its([:user_name]) { should eq 'John Doe'}
179
179
  its([:post_title]) { should eq 'Some post'}
180
180
  end
181
+
182
+ describe 'expose with block' do
183
+ let(:project) { double id: 1, description: 'description', type: 'type' }
184
+ subject { CollectPresenter.new(entity) }
185
+
186
+ context 'Test presenter' do
187
+ let(:entity) { double name: 'Test', project: project, email: 'foo@bar.com' }
188
+
189
+ its(:id) { should eq 1 }
190
+ its(:name) { should eq 'Test' }
191
+ its(:email) { should eq 'foo@bar.com' }
192
+ its(:type) { should eq 'test_type' }
193
+ its(:description) { should eq 'test_description' }
194
+ its(:to_json) { should eq '{"name":"Test","email":"foo@bar.com","id":1,"description":"test_description","type":"test_type"}'}
195
+ end
196
+
197
+ context 'Real presenter' do
198
+ let(:entity) { double name: 'Real', project: project, email: 'foo@bar.com' }
199
+
200
+ its(:name) { should eq 'Real' }
201
+ its(:email) { should eq 'foo@bar.com' }
202
+ its(:type) { should eq 'real_type' }
203
+ its(:description) { should eq 'real_description' }
204
+ its(:to_json) { should eq '{"name":"Real","email":"foo@bar.com","id":1,"description":"real_description","type":"real_type"}'}
205
+ end
206
+ end
181
207
  end
@@ -0,0 +1,35 @@
1
+ class CollectPresenter < Presenter
2
+ expose :name, :email
3
+ expose :id, with: :project, prefix: false
4
+ expose :description, :type, with: :project, prefix: false do
5
+ "#{ name }ProjectPresenter".constantize
6
+ end
7
+ end
8
+
9
+ class TestProjectPresenter
10
+ def initialize(project)
11
+ @project = project
12
+ end
13
+
14
+ def description
15
+ "test_#{ @project.description}"
16
+ end
17
+
18
+ def type
19
+ "test_#{ @project.type }"
20
+ end
21
+ end
22
+
23
+ class RealProjectPresenter
24
+ def initialize(project)
25
+ @project = project
26
+ end
27
+
28
+ def description
29
+ "real_#{ @project.description}"
30
+ end
31
+
32
+ def type
33
+ "real_#{ @project.type }"
34
+ end
35
+ end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: upholsterer
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.4.1
4
+ version: 0.4.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Nando Vieira
@@ -10,7 +10,7 @@ authors:
10
10
  autorequire:
11
11
  bindir: bin
12
12
  cert_chain: []
13
- date: 2014-12-05 00:00:00.000000000 Z
13
+ date: 2014-12-06 00:00:00.000000000 Z
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency
16
16
  name: rake
@@ -54,6 +54,20 @@ dependencies:
54
54
  - - ">="
55
55
  - !ruby/object:Gem::Version
56
56
  version: '0'
57
+ - !ruby/object:Gem::Dependency
58
+ name: activesupport
59
+ requirement: !ruby/object:Gem::Requirement
60
+ requirements:
61
+ - - ">="
62
+ - !ruby/object:Gem::Version
63
+ version: '0'
64
+ type: :runtime
65
+ prerelease: false
66
+ version_requirements: !ruby/object:Gem::Requirement
67
+ requirements:
68
+ - - ">="
69
+ - !ruby/object:Gem::Version
70
+ version: '0'
57
71
  description: A simple presenter/facade/decorator/whatever implementation.
58
72
  email:
59
73
  - fnando.vieira@gmail.com
@@ -79,6 +93,7 @@ files:
79
93
  - spec/base_spec.rb
80
94
  - spec/spec_helper.rb
81
95
  - spec/support/alias_presenter.rb
96
+ - spec/support/collect_presenter.rb
82
97
  - spec/support/comment.rb
83
98
  - spec/support/comment_presenter.rb
84
99
  - spec/support/iterator_presenter.rb