upholsterer 0.3.1 → 0.4.0
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 +4 -4
- data/.rspec +1 -0
- data/Gemfile.lock +1 -1
- data/lib/upholsterer/base.rb +12 -7
- data/lib/upholsterer/json_presenter.rb +16 -0
- data/lib/upholsterer/namespace.rb +5 -4
- data/lib/upholsterer/version.rb +2 -2
- data/spec/base_spec.rb +65 -55
- data/spec/spec_helper.rb +4 -4
- data/spec/upholsterer_spec.rb +1 -3
- metadata +2 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 478d75b5589a44d477e5fd3cf3d007770219f490
|
4
|
+
data.tar.gz: 5c7112be19940a322ec497e6a3977e4ac91a96ba
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 2cfd9f8a9940c57298b129c1dacd18dd0a14ff68cce2d5d68d6300ba6563671243108c54db865ea862849d246ffd6ff2d313ce64603bd9b57310b0bbe6346a2d
|
7
|
+
data.tar.gz: d3d362a3aca7bbed2508ed16a23cfb5a97c4b8a76f590c31b459e5cadd61ed33df4b19a2c0c5dda4de5247179dddde35d781686b805af9ea18c2a70477326889
|
data/.rspec
CHANGED
data/Gemfile.lock
CHANGED
data/lib/upholsterer/base.rb
CHANGED
@@ -12,6 +12,10 @@ module Upholsterer
|
|
12
12
|
# expose :title, :with => :post # will expose post.title through CommentPresenter#post_title
|
13
13
|
# end
|
14
14
|
#
|
15
|
+
# class CommentPresenter < Presenter
|
16
|
+
# expose_all
|
17
|
+
# end
|
18
|
+
#
|
15
19
|
# Subjects can be accessed by a private name with the same name.
|
16
20
|
# So the above subjects can be accessed internally by the methods +comment+ and +post+.
|
17
21
|
#
|
@@ -26,6 +30,7 @@ module Upholsterer
|
|
26
30
|
unless names.empty?
|
27
31
|
@subjects = names
|
28
32
|
attr_reader *names
|
33
|
+
private *names
|
29
34
|
end
|
30
35
|
|
31
36
|
@subjects
|
@@ -115,13 +120,13 @@ module Upholsterer
|
|
115
120
|
|
116
121
|
def self.expose_all
|
117
122
|
class_eval <<-RUBY, __FILE__, __LINE__ + 1
|
118
|
-
|
119
|
-
|
120
|
-
|
121
|
-
|
122
|
-
|
123
|
-
|
124
|
-
|
123
|
+
def method_missing(name, *params)
|
124
|
+
@subject.send(name, *params)
|
125
|
+
end
|
126
|
+
|
127
|
+
def respond_to?(name)
|
128
|
+
super || @subject.respond_to?(name)
|
129
|
+
end
|
125
130
|
RUBY
|
126
131
|
end
|
127
132
|
|
@@ -0,0 +1,16 @@
|
|
1
|
+
module Upholsterer
|
2
|
+
class Base
|
3
|
+
def to_hash
|
4
|
+
Hash[public_methods(false).collect do |field|
|
5
|
+
[field, public_send(field)]
|
6
|
+
end]
|
7
|
+
end
|
8
|
+
|
9
|
+
def to_json(*args)
|
10
|
+
to_hash.to_json(*args)
|
11
|
+
end
|
12
|
+
|
13
|
+
alias :to_h :to_hash
|
14
|
+
alias :as_json :to_json
|
15
|
+
end
|
16
|
+
end
|
@@ -1,6 +1,7 @@
|
|
1
1
|
module Upholsterer
|
2
|
-
require
|
3
|
-
require
|
4
|
-
require
|
5
|
-
require
|
2
|
+
require 'upholsterer/base'
|
3
|
+
require 'upholsterer/version'
|
4
|
+
require 'upholsterer/url_methods'
|
5
|
+
require 'upholsterer/json_presenter'
|
6
|
+
require 'upholsterer/rails' if defined?(Rails)
|
6
7
|
end
|
data/lib/upholsterer/version.rb
CHANGED
data/spec/base_spec.rb
CHANGED
@@ -1,8 +1,6 @@
|
|
1
|
-
require "spec_helper"
|
2
|
-
|
3
1
|
describe Upholsterer::Base do
|
4
|
-
describe
|
5
|
-
context
|
2
|
+
describe '.expose' do
|
3
|
+
context 'not using :with option' do
|
6
4
|
subject { UserPresenter.new }
|
7
5
|
|
8
6
|
it { should respond_to(:name) }
|
@@ -11,26 +9,26 @@ describe Upholsterer::Base do
|
|
11
9
|
it { should_not respond_to(:password_salt) }
|
12
10
|
end
|
13
11
|
|
14
|
-
context
|
12
|
+
context 'using :with option' do
|
15
13
|
subject { CommentPresenter.new }
|
16
14
|
|
17
15
|
it { should respond_to(:user_name) }
|
18
16
|
end
|
19
17
|
|
20
|
-
context
|
21
|
-
let(:site) { OpenStruct.new(:
|
18
|
+
context 'using :as option' do
|
19
|
+
let(:site) { OpenStruct.new(site: 'http://example.org') }
|
22
20
|
subject { AliasPresenter.new(site) }
|
23
21
|
|
24
22
|
it { should respond_to(:url) }
|
25
|
-
it { expect(subject.url).to eql(
|
23
|
+
it { expect(subject.url).to eql('http://example.org') }
|
26
24
|
end
|
27
25
|
|
28
|
-
context
|
26
|
+
context 'exposing iterators' do
|
29
27
|
subject { IteratorPresenter.new([1, 2, 3]) }
|
30
28
|
|
31
29
|
its(:each) { should be_a(Enumerator) }
|
32
30
|
|
33
|
-
it
|
31
|
+
it 'uses provided block' do
|
34
32
|
numbers = []
|
35
33
|
subject.each {|n| numbers << n}
|
36
34
|
expect(numbers).to eql([1, 2, 3])
|
@@ -38,8 +36,8 @@ describe Upholsterer::Base do
|
|
38
36
|
end
|
39
37
|
end
|
40
38
|
|
41
|
-
describe
|
42
|
-
context
|
39
|
+
describe '.attributes' do
|
40
|
+
context 'using defaults' do
|
43
41
|
let(:presenter) { UserPresenter }
|
44
42
|
subject { presenter.attributes }
|
45
43
|
|
@@ -48,7 +46,7 @@ describe Upholsterer::Base do
|
|
48
46
|
its([:email]) { should eql([:email, {}]) }
|
49
47
|
end
|
50
48
|
|
51
|
-
context
|
49
|
+
context 'using provided options' do
|
52
50
|
let(:presenter) { CommentPresenter }
|
53
51
|
subject { presenter.attributes }
|
54
52
|
|
@@ -58,101 +56,101 @@ describe Upholsterer::Base do
|
|
58
56
|
end
|
59
57
|
end
|
60
58
|
|
61
|
-
describe
|
62
|
-
it
|
63
|
-
thing = double(
|
59
|
+
describe '.subjects' do
|
60
|
+
it 'is aliased as .subject' do
|
61
|
+
thing = double('Thing')
|
64
62
|
presenter_class = Class.new(Presenter)
|
65
63
|
presenter_class.subject :thing
|
66
64
|
presenter = presenter_class.new(thing)
|
67
65
|
|
68
|
-
expect(presenter.instance_variable_get(
|
66
|
+
expect(presenter.instance_variable_get('@thing')).to eql(thing)
|
69
67
|
end
|
70
68
|
|
71
|
-
context
|
72
|
-
let(:user) { double :
|
69
|
+
context 'using defaults' do
|
70
|
+
let(:user) { double name: 'John Doe', email: 'john@doe.com' }
|
73
71
|
subject { UserPresenter.new(user) }
|
74
72
|
|
75
|
-
its(:name) { should ==
|
76
|
-
its(:email) { should ==
|
73
|
+
its(:name) { should == 'John Doe' }
|
74
|
+
its(:email) { should == 'john@doe.com' }
|
77
75
|
|
78
|
-
it
|
79
|
-
expect(subject.
|
76
|
+
it 'responds to private subject method' do
|
77
|
+
expect(subject.private_methods).to include(:subject)
|
80
78
|
end
|
81
79
|
|
82
|
-
it
|
80
|
+
it 'returns subject' do
|
83
81
|
expect(subject.send(:subject)).to eql(user)
|
84
82
|
end
|
85
83
|
end
|
86
84
|
|
87
|
-
context
|
88
|
-
let(:user) { double :
|
89
|
-
let(:comment) { double :
|
90
|
-
let(:post) { double :
|
85
|
+
context 'specifying several subjects' do
|
86
|
+
let(:user) { double name: 'John Doe' }
|
87
|
+
let(:comment) { double body: 'Some comment', user: user }
|
88
|
+
let(:post) { double title: 'Some post' }
|
91
89
|
subject { CommentPresenter.new(comment, post) }
|
92
90
|
|
93
|
-
its(:body) { should ==
|
94
|
-
its(:post_title) { should ==
|
95
|
-
its(:user_name) { should ==
|
91
|
+
its(:body) { should == 'Some comment' }
|
92
|
+
its(:post_title) { should == 'Some post' }
|
93
|
+
its(:user_name) { should == 'John Doe' }
|
96
94
|
|
97
|
-
it
|
98
|
-
expect(subject.
|
95
|
+
it 'responds to private comment method' do
|
96
|
+
expect(subject.private_methods).to include(:comment)
|
99
97
|
end
|
100
98
|
|
101
|
-
it
|
102
|
-
expect(subject.
|
99
|
+
it 'responds to private post method' do
|
100
|
+
expect(subject.private_methods).to include(:post)
|
103
101
|
end
|
104
102
|
|
105
|
-
it
|
103
|
+
it 'returns comment subject' do
|
106
104
|
expect(subject.send(:comment)).to eql(comment)
|
107
105
|
end
|
108
106
|
|
109
|
-
it
|
107
|
+
it 'returns post subject' do
|
110
108
|
expect(subject.send(:post)).to eql(post)
|
111
109
|
end
|
112
110
|
end
|
113
111
|
|
114
|
-
context
|
115
|
-
let(:comment) { double :
|
112
|
+
context 'when subjects are nil' do
|
113
|
+
let(:comment) { double body: 'Some comment' }
|
116
114
|
subject { CommentPresenter.new(comment, nil) }
|
117
115
|
|
118
116
|
its(:post_title) { should be_nil }
|
119
117
|
end
|
120
118
|
end
|
121
119
|
|
122
|
-
describe
|
123
|
-
context
|
124
|
-
let(:user) { double :
|
120
|
+
describe '.map' do
|
121
|
+
context 'wraps a single subject' do
|
122
|
+
let(:user) { double name: 'John Doe' }
|
125
123
|
subject { UserPresenter.map([user])[0] }
|
126
124
|
|
127
125
|
it { should be_a(UserPresenter) }
|
128
|
-
its(:name) { should ==
|
126
|
+
its(:name) { should == 'John Doe' }
|
129
127
|
end
|
130
128
|
|
131
|
-
context
|
132
|
-
let(:comment) { double :
|
133
|
-
let(:post) { double :
|
134
|
-
let(:user) { double :
|
129
|
+
context 'wraps several subjects' do
|
130
|
+
let(:comment) { double body: 'Some comment' }
|
131
|
+
let(:post) { double title: 'Some post' }
|
132
|
+
let(:user) { double name: 'John Doe' }
|
135
133
|
subject { CommentPresenter.map([comment], post)[0] }
|
136
134
|
|
137
135
|
it { should be_a(CommentPresenter) }
|
138
|
-
its(:body) { should ==
|
139
|
-
its(:post_title) { should ==
|
136
|
+
its(:body) { should == 'Some comment' }
|
137
|
+
its(:post_title) { should == 'Some post' }
|
140
138
|
end
|
141
139
|
end
|
142
140
|
|
143
|
-
describe
|
141
|
+
describe '#initialize' do
|
144
142
|
let(:user) { double }
|
145
143
|
subject { UserPresenter.new(user) }
|
146
144
|
|
147
|
-
it
|
148
|
-
expect(subject.instance_variable_get(
|
145
|
+
it 'assigns the subject' do
|
146
|
+
expect(subject.instance_variable_get('@subject')).to eql(user)
|
149
147
|
end
|
150
148
|
end
|
151
149
|
|
152
|
-
describe
|
150
|
+
describe 'inherited presenter' do
|
153
151
|
let(:presenter) { Class.new(CommentPresenter) }
|
154
152
|
|
155
|
-
context
|
153
|
+
context 'subjects' do
|
156
154
|
subject { presenter.subjects }
|
157
155
|
|
158
156
|
specify { expect(subject).to have(2).items }
|
@@ -160,7 +158,7 @@ describe Upholsterer::Base do
|
|
160
158
|
specify { expect(subject.last).to eql(:post) }
|
161
159
|
end
|
162
160
|
|
163
|
-
context
|
161
|
+
context 'attributes' do
|
164
162
|
subject { presenter.attributes }
|
165
163
|
|
166
164
|
it { should have(3).items }
|
@@ -168,4 +166,16 @@ describe Upholsterer::Base do
|
|
168
166
|
its([:post_title]) { should eql([:title, {with: :post}]) }
|
169
167
|
end
|
170
168
|
end
|
169
|
+
|
170
|
+
describe 'as json' do
|
171
|
+
let(:user) { double name: 'John Doe' }
|
172
|
+
let(:comment) { double body: 'Some comment', user: user }
|
173
|
+
let(:post) { double title: 'Some post' }
|
174
|
+
subject { CommentPresenter.new(comment, post).to_hash }
|
175
|
+
|
176
|
+
its(:keys) { should match_array [:body, :user_name, :post_title] }
|
177
|
+
its([:body]) { should eq 'Some comment' }
|
178
|
+
its([:user_name]) { should eq 'John Doe'}
|
179
|
+
its([:post_title]) { should eq 'Some post'}
|
180
|
+
end
|
171
181
|
end
|
data/spec/spec_helper.rb
CHANGED
@@ -1,9 +1,9 @@
|
|
1
|
-
require
|
1
|
+
require 'bundler/setup'
|
2
2
|
Bundler.require(:default, :development)
|
3
3
|
|
4
|
-
require
|
5
|
-
require
|
4
|
+
require 'upholsterer'
|
5
|
+
require 'ostruct'
|
6
6
|
|
7
|
-
Dir[File.dirname(__FILE__) +
|
7
|
+
Dir[File.dirname(__FILE__) + '/support/**/*.rb'].each do |file|
|
8
8
|
require file
|
9
9
|
end
|
data/spec/upholsterer_spec.rb
CHANGED
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
|
+
version: 0.4.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Nando Vieira
|
@@ -70,6 +70,7 @@ files:
|
|
70
70
|
- Rakefile
|
71
71
|
- lib/upholsterer.rb
|
72
72
|
- lib/upholsterer/base.rb
|
73
|
+
- lib/upholsterer/json_presenter.rb
|
73
74
|
- lib/upholsterer/namespace.rb
|
74
75
|
- lib/upholsterer/rails.rb
|
75
76
|
- lib/upholsterer/url_methods.rb
|