upholsterer 0.3.1 → 0.4.0

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: d3dd008b546feae2ddff4f38335af3f31d6c840d
4
- data.tar.gz: f9ea4a88970b4c410317603bb52356926e8bfe3e
3
+ metadata.gz: 478d75b5589a44d477e5fd3cf3d007770219f490
4
+ data.tar.gz: 5c7112be19940a322ec497e6a3977e4ac91a96ba
5
5
  SHA512:
6
- metadata.gz: 41a567e2724c898933ae141d9b63fbaa875d37e3c7a0aadcab3e69914d9a46910750074a053df88adde0828c58d5c8117c44ddf393f537f0fd579f52a0c94561
7
- data.tar.gz: 720f8af029261deb38042c76c951be4a82b92932c956a4b2fc01d084363e87eabcf54d81c56c4bc8577f36c65a594b97dd44226d207996250611c323c0126301
6
+ metadata.gz: 2cfd9f8a9940c57298b129c1dacd18dd0a14ff68cce2d5d68d6300ba6563671243108c54db865ea862849d246ffd6ff2d313ce64603bd9b57310b0bbe6346a2d
7
+ data.tar.gz: d3d362a3aca7bbed2508ed16a23cfb5a97c4b8a76f590c31b459e5cadd61ed33df4b19a2c0c5dda4de5247179dddde35d781686b805af9ea18c2a70477326889
data/.rspec CHANGED
@@ -1 +1,2 @@
1
+ --require spec_helper
1
2
  --color --format documentation
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- upholsterer (0.3.1)
4
+ upholsterer (0.4.0)
5
5
 
6
6
  GEM
7
7
  remote: https://rubygems.org/
@@ -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
- def method_missing(name, *params)
119
- @subject.send(name, *params)
120
- end
121
-
122
- def respond_to?(name)
123
- super || @subject.respond_to?(name)
124
- end
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 "upholsterer/base"
3
- require "upholsterer/version"
4
- require "upholsterer/url_methods"
5
- require "upholsterer/rails" if defined?(Rails)
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
@@ -1,8 +1,8 @@
1
1
  module Upholsterer
2
2
  module Version
3
3
  MAJOR = 0
4
- MINOR = 3
5
- PATCH = 1
4
+ MINOR = 4
5
+ PATCH = 0
6
6
  STRING = "#{MAJOR}.#{MINOR}.#{PATCH}"
7
7
  end
8
8
  end
data/spec/base_spec.rb CHANGED
@@ -1,8 +1,6 @@
1
- require "spec_helper"
2
-
3
1
  describe Upholsterer::Base do
4
- describe ".expose" do
5
- context "not using :with option" do
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 "using :with option" do
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 "using :as option" do
21
- let(:site) { OpenStruct.new(:site => "http://example.org") }
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("http://example.org") }
23
+ it { expect(subject.url).to eql('http://example.org') }
26
24
  end
27
25
 
28
- context "exposing iterators" do
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 "uses provided block" do
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 ".attributes" do
42
- context "using defaults" do
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 "using provided options" do
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 ".subjects" do
62
- it "is aliased as .subject" do
63
- thing = double("Thing")
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("@thing")).to eql(thing)
66
+ expect(presenter.instance_variable_get('@thing')).to eql(thing)
69
67
  end
70
68
 
71
- context "using defaults" do
72
- let(:user) { double :name => "John Doe", :email => "john@doe.com" }
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 == "John Doe" }
76
- its(:email) { should == "john@doe.com" }
73
+ its(:name) { should == 'John Doe' }
74
+ its(:email) { should == 'john@doe.com' }
77
75
 
78
- it "responds to private subject method" do
79
- expect(subject.public_methods).to include(:subject)
76
+ it 'responds to private subject method' do
77
+ expect(subject.private_methods).to include(:subject)
80
78
  end
81
79
 
82
- it "returns subject" do
80
+ it 'returns subject' do
83
81
  expect(subject.send(:subject)).to eql(user)
84
82
  end
85
83
  end
86
84
 
87
- context "specifying several subjects" do
88
- let(:user) { double :name => "John Doe" }
89
- let(:comment) { double :body => "Some comment", :user => user }
90
- let(:post) { double :title => "Some post" }
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 == "Some comment" }
94
- its(:post_title) { should == "Some post" }
95
- its(:user_name) { should == "John Doe" }
91
+ its(:body) { should == 'Some comment' }
92
+ its(:post_title) { should == 'Some post' }
93
+ its(:user_name) { should == 'John Doe' }
96
94
 
97
- it "responds to private comment method" do
98
- expect(subject.public_methods).to include(:comment)
95
+ it 'responds to private comment method' do
96
+ expect(subject.private_methods).to include(:comment)
99
97
  end
100
98
 
101
- it "responds to private post method" do
102
- expect(subject.public_methods).to include(:post)
99
+ it 'responds to private post method' do
100
+ expect(subject.private_methods).to include(:post)
103
101
  end
104
102
 
105
- it "returns comment subject" do
103
+ it 'returns comment subject' do
106
104
  expect(subject.send(:comment)).to eql(comment)
107
105
  end
108
106
 
109
- it "returns post subject" do
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 "when subjects are nil" do
115
- let(:comment) { double :body => "Some comment" }
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 ".map" do
123
- context "wraps a single subject" do
124
- let(:user) { double :name => "John Doe" }
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 == "John Doe" }
126
+ its(:name) { should == 'John Doe' }
129
127
  end
130
128
 
131
- context "wraps several subjects" do
132
- let(:comment) { double :body => "Some comment" }
133
- let(:post) { double :title => "Some post" }
134
- let(:user) { double :name => "John Doe" }
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 == "Some comment" }
139
- its(:post_title) { should == "Some post" }
136
+ its(:body) { should == 'Some comment' }
137
+ its(:post_title) { should == 'Some post' }
140
138
  end
141
139
  end
142
140
 
143
- describe "#initialize" do
141
+ describe '#initialize' do
144
142
  let(:user) { double }
145
143
  subject { UserPresenter.new(user) }
146
144
 
147
- it "assigns the subject" do
148
- expect(subject.instance_variable_get("@subject")).to eql(user)
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 "inherited presenter" do
150
+ describe 'inherited presenter' do
153
151
  let(:presenter) { Class.new(CommentPresenter) }
154
152
 
155
- context "subjects" do
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 "attributes" do
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 "bundler/setup"
1
+ require 'bundler/setup'
2
2
  Bundler.require(:default, :development)
3
3
 
4
- require "upholsterer"
5
- require "ostruct"
4
+ require 'upholsterer'
5
+ require 'ostruct'
6
6
 
7
- Dir[File.dirname(__FILE__) + "/support/**/*.rb"].each do |file|
7
+ Dir[File.dirname(__FILE__) + '/support/**/*.rb'].each do |file|
8
8
  require file
9
9
  end
@@ -1,7 +1,5 @@
1
- require "spec_helper"
2
-
3
1
  describe Upholsterer do
4
- it "assigns Upholsterer::Base" do
2
+ it 'assigns Upholsterer::Base' do
5
3
  expect(::Presenter).to be(Upholsterer::Base)
6
4
  end
7
5
  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.3.1
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