rest-in-peace 4.0.0 → 4.1.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.
data/.gitignore CHANGED
@@ -1,2 +1,3 @@
1
1
  Gemfile.lock
2
2
  coverage
3
+ *.gem
data/VERSION CHANGED
@@ -1 +1 @@
1
- 4.0.0
1
+ 4.1.0
@@ -18,6 +18,8 @@ module RESTinPeace
18
18
 
19
19
  base.send(:alias_method, :save_without_dirty_tracking, :save)
20
20
  base.send(:alias_method, :save, :save_with_dirty_tracking)
21
+ base.send(:alias_method, :create_without_dirty_tracking, :create)
22
+ base.send(:alias_method, :create, :create_with_dirty_tracking)
21
23
 
22
24
  def base.human_attribute_name(attr, options = {})
23
25
  attr.to_s
@@ -35,8 +37,20 @@ module RESTinPeace
35
37
 
36
38
  def save_with_dirty_tracking
37
39
  save_without_dirty_tracking.tap do
38
- clear_changes
40
+ clear_changes if valid?
39
41
  end
42
+ valid?
43
+ end
44
+
45
+ def create_with_dirty_tracking
46
+ create_without_dirty_tracking.tap do
47
+ clear_changes if valid?
48
+ end
49
+ valid?
50
+ end
51
+
52
+ def valid?
53
+ !errors.any?
40
54
  end
41
55
 
42
56
  def persisted?
@@ -8,6 +8,7 @@ module RESTinPeace
8
8
  @url_template = url_template
9
9
  @klass = klass
10
10
  @params = params
11
+ @attributes = {}
11
12
  end
12
13
 
13
14
  def get
@@ -43,8 +44,12 @@ module RESTinPeace
43
44
  sanitizer.leftover_params
44
45
  end
45
46
 
47
+ def attributes
48
+ @attributes = @klass.to_h if @klass.respond_to?(:to_h)
49
+ end
50
+
46
51
  def sanitizer
47
- @sanitizer ||= RESTinPeace::TemplateSanitizer.new(@url_template, @params)
52
+ @sanitizer ||= RESTinPeace::TemplateSanitizer.new(@url_template, @params, attributes)
48
53
  end
49
54
 
50
55
  def convert_response(response)
@@ -5,9 +5,10 @@ module RESTinPeace
5
5
 
6
6
  class IncompleteParams < RESTinPeace::DefaultError; end
7
7
 
8
- def initialize(url_template, params)
8
+ def initialize(url_template, params, attributes)
9
9
  @url_template = url_template
10
10
  @params = params.dup
11
+ @attributes = attributes
11
12
  @url = nil
12
13
  end
13
14
 
@@ -16,6 +17,7 @@ module RESTinPeace
16
17
  @url = @url_template.dup
17
18
  tokens.each do |token|
18
19
  param = @params.delete(token.to_sym)
20
+ param ||= @attributes[token.to_sym]
19
21
  raise IncompleteParams, "Unknown parameter for token :#{token} found" unless param
20
22
  @url.sub!(%r{:#{token}}, param.to_s)
21
23
  end
@@ -124,7 +124,7 @@ describe RESTinPeace do
124
124
  let(:response) { OpenStruct.new(body: { name: 'new name from api' }) }
125
125
 
126
126
  specify { expect { instance.save }.to_not raise_error }
127
- specify { expect(instance.save.object_id).to eq(instance.object_id) }
127
+ specify { expect(instance.save).to eq(true) }
128
128
  specify { expect { instance.save }.to change(instance, :name) }
129
129
  end
130
130
 
@@ -137,6 +137,7 @@ describe RESTinPeace do
137
137
  specify { expect { instance.save }.to change { instance.errors.any? } }
138
138
  specify { expect { instance.save }.to_not change { instance.description } }
139
139
  specify { expect { instance.save }.to_not change { instance.name } }
140
+ specify { expect(instance.save).to eq(false) }
140
141
  end
141
142
  end
142
143
 
@@ -145,7 +146,7 @@ describe RESTinPeace do
145
146
  let(:response) { OpenStruct.new(body: { name: 'new name from api' }) }
146
147
 
147
148
  specify { expect { instance.create }.to_not raise_error }
148
- specify { expect(instance.create.object_id).to eq(instance.object_id) }
149
+ specify { expect(instance.create).to eq(true) }
149
150
  specify { expect { instance.create }.to change(instance, :name) }
150
151
  end
151
152
 
@@ -158,11 +159,13 @@ describe RESTinPeace do
158
159
  specify { expect { instance.create }.to change { instance.errors.any? } }
159
160
  specify { expect { instance.create }.to_not change { instance.description } }
160
161
  specify { expect { instance.create }.to_not change { instance.name } }
162
+ specify { expect(instance.save).to eq(false) }
161
163
  end
162
164
  end
163
165
 
164
166
  describe 'validation handling' do
165
167
  let(:description) { 'desc' }
168
+ let(:errors) { { description: ['must not be empty'] } }
166
169
 
167
170
  specify { expect(instance).to respond_to(:read_attribute_for_validation).with(1).argument }
168
171
  specify { expect(instance.read_attribute_for_validation(:description)).to eq('desc') }
@@ -172,9 +175,22 @@ describe RESTinPeace do
172
175
  end
173
176
 
174
177
  describe '#errors=' do
175
- let(:errors) { { description: ['must not be empty'] } }
176
178
  specify { expect { instance.errors = errors }.to change { instance.errors.count } }
177
179
  end
180
+
181
+ describe '#valid?' do
182
+ context 'without errors' do
183
+ specify { expect(instance.valid?).to eq(true) }
184
+ end
185
+
186
+ context 'with errors' do
187
+ before do
188
+ instance.errors = errors
189
+ end
190
+
191
+ specify { expect(instance.valid?).to eq(false) }
192
+ end
193
+ end
178
194
  end
179
195
  end
180
196
  end
@@ -1,8 +1,8 @@
1
1
  require 'rest_in_peace/template_sanitizer'
2
2
 
3
3
  describe RESTinPeace::TemplateSanitizer do
4
-
5
- let(:template_sanitizer) { RESTinPeace::TemplateSanitizer.new(url_template, params) }
4
+ let(:template_sanitizer) { RESTinPeace::TemplateSanitizer.new(url_template, params, attributes) }
5
+ let(:attributes) { {} }
6
6
 
7
7
  describe '#url' do
8
8
  subject { template_sanitizer.url }
@@ -48,6 +48,20 @@ describe RESTinPeace::TemplateSanitizer do
48
48
  let(:url_template) { '/a/:id' }
49
49
  specify { expect { subject }.to_not change { params } }
50
50
  end
51
+
52
+ context 'tokens from attributes' do
53
+ let(:params) { { id: 1 } }
54
+ let(:attributes) { { a_id: 2 } }
55
+ let(:url_template) { '/a/:a_id/b/:id' }
56
+ specify { expect(subject).to eq('/a/2/b/1') }
57
+ end
58
+
59
+ context 'incomplete params and attributes' do
60
+ let(:params) { { id: 1 } }
61
+ let(:attributes) { {} }
62
+ let(:url_template) { '/a/:a_id/b/:id' }
63
+ specify { expect { subject }.to raise_error(RESTinPeace::TemplateSanitizer::IncompleteParams) }
64
+ end
51
65
  end
52
66
 
53
67
  describe '#tokens' do
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: rest-in-peace
3
3
  version: !ruby/object:Gem::Version
4
- version: 4.0.0
4
+ version: 4.1.0
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2015-02-05 00:00:00.000000000 Z
12
+ date: 2015-06-05 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: activemodel