upholsterer 1.0.0 → 1.1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/Gemfile.lock +1 -1
- data/README.rdoc +3 -3
- data/lib/upholsterer/base.rb +17 -4
- data/lib/upholsterer/version.rb +1 -1
- data/spec/base_spec.rb +30 -0
- data/spec/support/expose_with_other_presenter.rb +16 -0
- data/upholsterer.gemspec +1 -1
- metadata +4 -3
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 11648613c825e5c1cf104ce4f6b77605b5474876
|
4
|
+
data.tar.gz: 1255ad6efa4bbd9c6fb33ecaec1fcd69792033ac
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 1e3f4c361612f89d9eb1eb4556c13fc9670341a182fbd369c213db6ca2030374068854b68109c3c9612ecffa5744b8335ad4c13d39f11edf5ea1d017a78e8fe5
|
7
|
+
data.tar.gz: 5eedee0c00428232b37e33a13336fd0de020c5ff2b7d6c229e6c6a51f20d853e1456cc1e1b56e5f672dc6a4336ead0d88661a5e3e091511b7a47820916f47d24
|
data/Gemfile.lock
CHANGED
data/README.rdoc
CHANGED
@@ -2,9 +2,9 @@
|
|
2
2
|
|
3
3
|
Based on SimplePresenter (https://github.com/fnando/simple_presenter)
|
4
4
|
|
5
|
-
{<img src="https://codeclimate.com/github/
|
6
|
-
{<img src="https://hakiri.io/github/
|
7
|
-
{<img src="https://travis-ci.org/
|
5
|
+
{<img src="https://codeclimate.com/github/llxff/upholsterer/badges/gpa.svg" />}[https://codeclimate.com/github/llxff/upholsterer]
|
6
|
+
{<img src="https://hakiri.io/github/llxff/upholsterer/master.svg" alt="security" />}[https://hakiri.io/github/llxff/upholsterer/master]
|
7
|
+
{<img src="https://travis-ci.org/llxff/upholsterer.svg" />}[https://travis-ci.org/llxff/upholsterer]
|
8
8
|
|
9
9
|
== Installation
|
10
10
|
|
data/lib/upholsterer/base.rb
CHANGED
@@ -103,6 +103,7 @@ module Upholsterer
|
|
103
103
|
|
104
104
|
container = options.fetch(:with, nil)
|
105
105
|
method_prefix = container if options.fetch(:prefix, true)
|
106
|
+
presenter = options.fetch(:presenter, nil)
|
106
107
|
|
107
108
|
if block_given? and container.present?
|
108
109
|
wrapper_method = "#{ attrs.join('_') }_wrapper"
|
@@ -121,7 +122,10 @@ module Upholsterer
|
|
121
122
|
instance_variable_set(wrapper_instance_variable, wrapper)
|
122
123
|
end
|
123
124
|
|
124
|
-
|
125
|
+
|
126
|
+
|
127
|
+
value = instance_variable_get(wrapper_instance_variable)
|
128
|
+
decorate_with_presenter(value, presenter)
|
125
129
|
end
|
126
130
|
|
127
131
|
private wrapper_method
|
@@ -142,9 +146,10 @@ module Upholsterer
|
|
142
146
|
delegate attr_name, to: wrapper_method, prefix: !!method_prefix
|
143
147
|
else
|
144
148
|
class_eval <<-RUBY, __FILE__, __LINE__ + 1
|
145
|
-
def #{method_name}(&block)
|
146
|
-
proxy_message(#{container.inspect}, "#{attr_name}", &block)
|
147
|
-
|
149
|
+
def #{method_name}(&block)
|
150
|
+
value = proxy_message(#{container.inspect}, "#{attr_name}", &block)
|
151
|
+
decorate_with_presenter(value, #{ presenter.inspect })
|
152
|
+
end
|
148
153
|
RUBY
|
149
154
|
end
|
150
155
|
end
|
@@ -197,5 +202,13 @@ module Upholsterer
|
|
197
202
|
subject = instance_variable_get("@#{self.class.subjects.first}").__send__(subject_name) unless subject || self.class.subjects.include?(subject_name)
|
198
203
|
subject.respond_to?(method) ? subject.__send__(method, &block) : nil
|
199
204
|
end
|
205
|
+
|
206
|
+
def decorate_with_presenter(value, presenter)
|
207
|
+
if value.present? and presenter
|
208
|
+
presenter.new(value)
|
209
|
+
else
|
210
|
+
value
|
211
|
+
end
|
212
|
+
end
|
200
213
|
end
|
201
214
|
end
|
data/lib/upholsterer/version.rb
CHANGED
data/spec/base_spec.rb
CHANGED
@@ -219,4 +219,34 @@ describe Upholsterer::Base do
|
|
219
219
|
its(:to_json) { should be_json_with(name: 'Real', email: 'foo@bar.com', id: 1, description: 'real_description', type: 'real_type') }
|
220
220
|
end
|
221
221
|
end
|
222
|
+
|
223
|
+
describe 'expose with other presenter' do
|
224
|
+
context 'with several subjects' do
|
225
|
+
let(:user) { double name: 'Peter', email: 'peter@email.com' }
|
226
|
+
let(:comment) { double(user: double(name: 'Steve', email: 'steve@email.com')) }
|
227
|
+
|
228
|
+
subject { ExposeWithOtherPresenter.new(user, comment) }
|
229
|
+
|
230
|
+
its(:user_name) { should eq 'Peter' }
|
231
|
+
|
232
|
+
specify { expect(subject.creator.name).to eq 'Steve' }
|
233
|
+
specify { expect(subject.creator.email).to eq 'steve@email.com' }
|
234
|
+
its(:to_json) { should be_json_with(user_name: 'Peter', creator: { name: 'Steve', email: 'steve@email.com'}) }
|
235
|
+
end
|
236
|
+
|
237
|
+
context 'with one subject' do
|
238
|
+
let(:user) { double name: 'Peter', email: 'peter@email.com' }
|
239
|
+
let(:post) { double user: user, comment: nil, id: 1}
|
240
|
+
|
241
|
+
subject { ExposeWithOneSubjectPresenter.new(post) }
|
242
|
+
|
243
|
+
its(:id) { should eq 1 }
|
244
|
+
its(:comment) { should be_nil }
|
245
|
+
|
246
|
+
specify { expect(subject.user.name).to eq 'Peter' }
|
247
|
+
specify { expect(subject.user.email).to eq 'peter@email.com' }
|
248
|
+
|
249
|
+
its(:to_json) { should be_json_with(id: 1, user: { name: 'Peter', email: 'peter@email.com'}, comment: nil) }
|
250
|
+
end
|
251
|
+
end
|
222
252
|
end
|
@@ -0,0 +1,16 @@
|
|
1
|
+
class SimplePresenter < Presenter
|
2
|
+
expose :name, :email
|
3
|
+
end
|
4
|
+
|
5
|
+
class ExposeWithOtherPresenter < Presenter
|
6
|
+
subjects :user, :comment
|
7
|
+
|
8
|
+
expose :name, with: :user
|
9
|
+
expose :user, presenter: SimplePresenter, with: :comment, as: :creator, prefix: false
|
10
|
+
end
|
11
|
+
|
12
|
+
class ExposeWithOneSubjectPresenter < Presenter
|
13
|
+
expose :id
|
14
|
+
expose :user, presenter: SimplePresenter
|
15
|
+
expose :comment, presenter: SimplePresenter
|
16
|
+
end
|
data/upholsterer.gemspec
CHANGED
@@ -8,7 +8,7 @@ Gem::Specification.new do |s|
|
|
8
8
|
s.platform = Gem::Platform::RUBY
|
9
9
|
s.authors = ['Nando Vieira', 'Aleksandr Fomin', 'Gleb Sinyavsky']
|
10
10
|
s.email = ['fnando.vieira@gmail.com', 'll.wg.bin@gmail.com']
|
11
|
-
s.homepage = 'https://github.com/
|
11
|
+
s.homepage = 'https://github.com/llxff/upholsterer'
|
12
12
|
s.summary = 'A simple presenter/facade/decorator/whatever implementation.'
|
13
13
|
s.description = s.summary
|
14
14
|
|
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: 1.
|
4
|
+
version: 1.1.0
|
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: 2015-
|
13
|
+
date: 2015-09-07 00:00:00.000000000 Z
|
14
14
|
dependencies:
|
15
15
|
- !ruby/object:Gem::Dependency
|
16
16
|
name: rake
|
@@ -96,6 +96,7 @@ files:
|
|
96
96
|
- spec/support/comment.rb
|
97
97
|
- spec/support/comment_presenter.rb
|
98
98
|
- spec/support/expose_all_presenter.rb
|
99
|
+
- spec/support/expose_with_other_presenter.rb
|
99
100
|
- spec/support/iterator_presenter.rb
|
100
101
|
- spec/support/matchers/json_matcher.rb
|
101
102
|
- spec/support/serializable_presenter.rb
|
@@ -103,7 +104,7 @@ files:
|
|
103
104
|
- spec/support/user_presenter.rb
|
104
105
|
- spec/upholsterer_spec.rb
|
105
106
|
- upholsterer.gemspec
|
106
|
-
homepage: https://github.com/
|
107
|
+
homepage: https://github.com/llxff/upholsterer
|
107
108
|
licenses: []
|
108
109
|
metadata: {}
|
109
110
|
post_install_message:
|