her 0.8.1 → 0.8.3

Sign up to get free protection for your applications and to get access to all the features.
@@ -5,22 +5,22 @@ describe Her::Model::Relation do
5
5
  describe :where do
6
6
  context "for base classes" do
7
7
  before do
8
- Her::API.setup :url => "https://api.example.com" do |builder|
8
+ Her::API.setup url: "https://api.example.com" do |builder|
9
9
  builder.use Her::Middleware::FirstLevelParseJSON
10
10
  builder.adapter :test do |stub|
11
- stub.get("/users?foo=1&bar=2") { |env| ok! [{ :id => 2, :fullname => "Tobias Fünke" }] }
12
- stub.get("/users?admin=1") { |env| ok! [{ :id => 1, :fullname => "Tobias Fünke" }] }
11
+ stub.get("/users?foo=1&bar=2") { ok! [{ id: 2, fullname: "Tobias Fünke" }] }
12
+ stub.get("/users?admin=1") { ok! [{ id: 1, fullname: "Tobias Fünke" }] }
13
13
 
14
- stub.get("/users") do |env|
14
+ stub.get("/users") do
15
15
  ok! [
16
- { :id => 1, :fullname => "Tobias Fünke" },
17
- { :id => 2, :fullname => "Lindsay Fünke" },
18
- @created_user,
16
+ { id: 1, fullname: "Tobias Fünke" },
17
+ { id: 2, fullname: "Lindsay Fünke" },
18
+ @created_user
19
19
  ].compact
20
20
  end
21
21
 
22
- stub.post('/users') do |env|
23
- @created_user = { :id => 3, :fullname => 'George Michael Bluth' }
22
+ stub.post("/users") do
23
+ @created_user = { id: 3, fullname: "George Michael Bluth" }
24
24
  ok! @created_user
25
25
  end
26
26
  end
@@ -30,40 +30,40 @@ describe Her::Model::Relation do
30
30
  end
31
31
 
32
32
  it "doesn't fetch the data immediatly" do
33
- Foo::User.should_receive(:request).never
34
- @users = Foo::User.where(:admin => 1)
33
+ expect(Foo::User).to receive(:request).never
34
+ @users = Foo::User.where(admin: 1)
35
35
  end
36
36
 
37
37
  it "fetches the data and passes query parameters" do
38
- Foo::User.should_receive(:request).once.and_call_original
39
- @users = Foo::User.where(:admin => 1)
40
- @users.should respond_to(:length)
41
- @users.size.should eql 1
38
+ expect(Foo::User).to receive(:request).once.and_call_original
39
+ @users = Foo::User.where(admin: 1)
40
+ expect(@users).to respond_to(:length)
41
+ expect(@users.size).to eql 1
42
42
  end
43
43
 
44
44
  it "chains multiple where statements" do
45
- @user = Foo::User.where(:foo => 1).where(:bar => 2).first
46
- @user.id.should == 2
45
+ @user = Foo::User.where(foo: 1).where(bar: 2).first
46
+ expect(@user.id).to eq(2)
47
47
  end
48
48
 
49
49
  it "does not reuse relations" do
50
- Foo::User.all.size.should eql 2
51
- Foo::User.create(:fullname => 'George Michael Bluth').id.should == 3
52
- Foo::User.all.size.should eql 3
50
+ expect(Foo::User.all.size).to eql 2
51
+ expect(Foo::User.create(fullname: "George Michael Bluth").id).to eq(3)
52
+ expect(Foo::User.all.size).to eql 3
53
53
  end
54
54
  end
55
55
 
56
56
  context "for parent class" do
57
57
  before do
58
- Her::API.setup :url => "https://api.example.com" do |builder|
58
+ Her::API.setup url: "https://api.example.com" do |builder|
59
59
  builder.use Her::Middleware::FirstLevelParseJSON
60
60
  builder.adapter :test do |stub|
61
- stub.get("/users?page=2") { |env| ok! [{ :id => 1, :fullname => "Tobias Fünke" }, { :id => 2, :fullname => "Lindsay Fünke" }] }
61
+ stub.get("/users?page=2") { ok! [{ id: 1, fullname: "Tobias Fünke" }, { id: 2, fullname: "Lindsay Fünke" }] }
62
62
  end
63
63
  end
64
64
 
65
65
  spawn_model("Foo::Model") do
66
- scope :page, lambda { |page| where(:page => page) }
66
+ scope :page, ->(page) { where(page: page) }
67
67
  end
68
68
 
69
69
  class User < Foo::Model; end
@@ -72,18 +72,18 @@ describe Her::Model::Relation do
72
72
 
73
73
  it "propagates the scopes through its children" do
74
74
  @users = User.page(2)
75
- @users.length.should == 2
75
+ expect(@users.length).to eq(2)
76
76
  end
77
77
  end
78
78
  end
79
79
 
80
80
  describe :create do
81
81
  before do
82
- Her::API.setup :url => "https://api.example.com" do |builder|
82
+ Her::API.setup url: "https://api.example.com" do |builder|
83
83
  builder.use Her::Middleware::FirstLevelParseJSON
84
84
  builder.use Faraday::Request::UrlEncoded
85
85
  builder.adapter :test do |stub|
86
- stub.post("/users") { |env| ok! :id => 1, :fullname => params(env)[:fullname], :email => params(env)[:email] }
86
+ stub.post("/users") { |env| ok! id: 1, fullname: params(env)[:fullname], email: params(env)[:email] }
87
87
  end
88
88
  end
89
89
 
@@ -92,19 +92,19 @@ describe Her::Model::Relation do
92
92
 
93
93
  context "with a single where call" do
94
94
  it "creates a resource and passes the query parameters" do
95
- @user = Foo::User.where(:fullname => "Tobias Fünke", :email => "tobias@bluth.com").create
96
- @user.id.should == 1
97
- @user.fullname.should == "Tobias Fünke"
98
- @user.email.should == "tobias@bluth.com"
95
+ @user = Foo::User.where(fullname: "Tobias Fünke", email: "tobias@bluth.com").create
96
+ expect(@user.id).to eq(1)
97
+ expect(@user.fullname).to eq("Tobias Fünke")
98
+ expect(@user.email).to eq("tobias@bluth.com")
99
99
  end
100
100
  end
101
101
 
102
102
  context "with multiple where calls" do
103
103
  it "creates a resource and passes the query parameters" do
104
- @user = Foo::User.where(:fullname => "Tobias Fünke").create(:email => "tobias@bluth.com")
105
- @user.id.should == 1
106
- @user.fullname.should == "Tobias Fünke"
107
- @user.email.should == "tobias@bluth.com"
104
+ @user = Foo::User.where(fullname: "Tobias Fünke").create(email: "tobias@bluth.com")
105
+ expect(@user.id).to eq(1)
106
+ expect(@user.fullname).to eq("Tobias Fünke")
107
+ expect(@user.email).to eq("tobias@bluth.com")
108
108
  end
109
109
  end
110
110
  end
@@ -113,114 +113,114 @@ describe Her::Model::Relation do
113
113
  before { spawn_model "Foo::User" }
114
114
 
115
115
  it "handles new resource with build" do
116
- @new_user = Foo::User.where(:fullname => "Tobias Fünke").build
117
- @new_user.new?.should be_truthy
118
- @new_user.fullname.should == "Tobias Fünke"
116
+ @new_user = Foo::User.where(fullname: "Tobias Fünke").build
117
+ expect(@new_user.new?).to be_truthy
118
+ expect(@new_user.fullname).to eq("Tobias Fünke")
119
119
  end
120
120
  end
121
121
 
122
122
  describe :scope do
123
123
  before do
124
- Her::API.setup :url => "https://api.example.com" do |builder|
124
+ Her::API.setup url: "https://api.example.com" do |builder|
125
125
  builder.use Her::Middleware::FirstLevelParseJSON
126
126
  builder.adapter :test do |stub|
127
- stub.get("/users?what=4&where=3") { |env| ok! [{ :id => 3, :fullname => "Maeby Fünke" }] }
128
- stub.get("/users?what=2") { |env| ok! [{ :id => 2, :fullname => "Lindsay Fünke" }] }
129
- stub.get("/users?where=6") { |env| ok! [{ :id => 4, :fullname => "Tobias Fünke" }] }
127
+ stub.get("/users?what=4&where=3") { ok! [{ id: 3, fullname: "Maeby Fünke" }] }
128
+ stub.get("/users?what=2") { ok! [{ id: 2, fullname: "Lindsay Fünke" }] }
129
+ stub.get("/users?where=6") { ok! [{ id: 4, fullname: "Tobias Fünke" }] }
130
130
  end
131
131
  end
132
132
 
133
- spawn_model 'Foo::User' do
134
- scope :foo, lambda { |v| where(:what => v) }
135
- scope :bar, lambda { |v| where(:where => v) }
136
- scope :baz, lambda { bar(6) }
133
+ spawn_model "Foo::User" do
134
+ scope :foo, ->(v) { where(what: v) }
135
+ scope :bar, ->(v) { where(where: v) }
136
+ scope :baz, -> { bar(6) }
137
137
  end
138
138
  end
139
139
 
140
140
  it "passes query parameters" do
141
141
  @user = Foo::User.foo(2).first
142
- @user.id.should == 2
142
+ expect(@user.id).to eq(2)
143
143
  end
144
144
 
145
145
  it "passes multiple query parameters" do
146
146
  @user = Foo::User.foo(4).bar(3).first
147
- @user.id.should == 3
147
+ expect(@user.id).to eq(3)
148
148
  end
149
149
 
150
150
  it "handles embedded scopes" do
151
151
  @user = Foo::User.baz.first
152
- @user.id.should == 4
152
+ expect(@user.id).to eq(4)
153
153
  end
154
154
  end
155
155
 
156
156
  describe :default_scope do
157
157
  context "for new objects" do
158
158
  before do
159
- spawn_model 'Foo::User' do
160
- default_scope lambda { where(:active => true) }
161
- default_scope lambda { where(:admin => true) }
159
+ spawn_model "Foo::User" do
160
+ default_scope -> { where(active: true) }
161
+ default_scope -> { where(admin: true) }
162
162
  end
163
163
  end
164
164
 
165
165
  it "should apply the scope to the attributes" do
166
- Foo::User.new.should be_active
167
- Foo::User.new.should be_admin
166
+ expect(Foo::User.new).to be_active
167
+ expect(Foo::User.new).to be_admin
168
168
  end
169
169
  end
170
170
 
171
171
  context "for fetched resources" do
172
172
  before do
173
- Her::API.setup :url => "https://api.example.com" do |builder|
173
+ Her::API.setup url: "https://api.example.com" do |builder|
174
174
  builder.use Her::Middleware::FirstLevelParseJSON
175
175
  builder.use Faraday::Request::UrlEncoded
176
176
  builder.adapter :test do |stub|
177
- stub.post("/users") { |env| ok! :id => 3, :active => (params(env)[:active] == "true" ? true : false) }
177
+ stub.post("/users") { |env| ok! id: 3, active: (params(env)[:active] == "true" ? true : false) }
178
178
  end
179
179
  end
180
180
 
181
- spawn_model 'Foo::User' do
182
- default_scope lambda { where(:active => true) }
181
+ spawn_model "Foo::User" do
182
+ default_scope -> { where(active: true) }
183
183
  end
184
184
  end
185
185
 
186
- it("should apply the scope to the request") { Foo::User.create.should be_active }
186
+ it("should apply the scope to the request") { expect(Foo::User.create).to be_active }
187
187
  end
188
188
 
189
189
  context "for fetched collections" do
190
190
  before do
191
- Her::API.setup :url => "https://api.example.com" do |builder|
191
+ Her::API.setup url: "https://api.example.com" do |builder|
192
192
  builder.use Her::Middleware::FirstLevelParseJSON
193
193
  builder.use Faraday::Request::UrlEncoded
194
194
  builder.adapter :test do |stub|
195
- stub.get("/users?active=true") { |env| ok! [{ :id => 3, :active => (params(env)[:active] == "true" ? true : false) }] }
195
+ stub.get("/users?active=true") { |env| ok! [{ id: 3, active: (params(env)[:active] == "true" ? true : false) }] }
196
196
  end
197
197
  end
198
198
 
199
- spawn_model 'Foo::User' do
200
- default_scope lambda { where(:active => true) }
199
+ spawn_model "Foo::User" do
200
+ default_scope -> { where(active: true) }
201
201
  end
202
202
  end
203
203
 
204
- it("should apply the scope to the request") { Foo::User.all.first.should be_active }
204
+ it("should apply the scope to the request") { expect(Foo::User.all.first).to be_active }
205
205
  end
206
206
  end
207
207
 
208
208
  describe :map do
209
209
  before do
210
- Her::API.setup :url => "https://api.example.com" do |builder|
210
+ Her::API.setup url: "https://api.example.com" do |builder|
211
211
  builder.use Her::Middleware::FirstLevelParseJSON
212
212
  builder.adapter :test do |stub|
213
- stub.get("/users") do |env|
214
- ok! [{ :id => 1, :fullname => "Tobias Fünke" }, { :id => 2, :fullname => "Lindsay Fünke" }]
213
+ stub.get("/users") do
214
+ ok! [{ id: 1, fullname: "Tobias Fünke" }, { id: 2, fullname: "Lindsay Fünke" }]
215
215
  end
216
216
  end
217
217
  end
218
218
 
219
- spawn_model 'Foo::User'
219
+ spawn_model "Foo::User"
220
220
  end
221
221
 
222
222
  it "delegates the method to the fetched collection" do
223
- Foo::User.all.map(&:fullname).should == ["Tobias Fünke", "Lindsay Fünke"]
223
+ expect(Foo::User.all.map(&:fullname)).to eq(["Tobias Fünke", "Lindsay Fünke"])
224
224
  end
225
225
  end
226
226
  end
@@ -13,12 +13,12 @@ describe "Her::Model and ActiveModel::Validations" do
13
13
 
14
14
  it "validates attributes when calling #valid?" do
15
15
  user = Foo::User.new
16
- user.should_not be_valid
17
- user.errors.full_messages.should include("Fullname can't be blank")
18
- user.errors.full_messages.should include("Email can't be blank")
16
+ expect(user).not_to be_valid
17
+ expect(user.errors.full_messages).to include("Fullname can't be blank")
18
+ expect(user.errors.full_messages).to include("Email can't be blank")
19
19
  user.fullname = "Tobias Fünke"
20
20
  user.email = "tobias@bluthcompany.com"
21
- user.should be_valid
21
+ expect(user).to be_valid
22
22
  end
23
23
  end
24
24
 
@@ -35,8 +35,8 @@ describe "Her::Model and ActiveModel::Validations" do
35
35
  end
36
36
 
37
37
  it "validates attributes when calling #valid?" do
38
- user = User.new(:_errors => ["Email cannot be blank"])
39
- user.errors.should include("Email cannot be blank")
38
+ user = User.new(_errors: ["Email cannot be blank"])
39
+ expect(user.errors).to include("Email cannot be blank")
40
40
  end
41
41
  end
42
42
  end
data/spec/model_spec.rb CHANGED
@@ -1,13 +1,13 @@
1
1
  # encoding: utf-8
2
- require 'spec_helper'
2
+ require "spec_helper"
3
3
 
4
4
  describe Her::Model do
5
5
  before do
6
- Her::API.setup :url => "https://api.example.com" do |connection|
6
+ Her::API.setup url: "https://api.example.com" do |connection|
7
7
  connection.use Her::Middleware::FirstLevelParseJSON
8
8
  connection.adapter :test do |stub|
9
- stub.get("/users/1") { |env| [200, {}, { :id => 1, :name => "Tobias Fünke" }.to_json] }
10
- stub.get("/users/1/comments") { |env| [200, {}, [{ :id => 4, :body => "They're having a FIRESALE?" }].to_json] }
9
+ stub.get("/users/1") { [200, {}, { id: 1, name: "Tobias Fünke" }.to_json] }
10
+ stub.get("/users/1/comments") { [200, {}, [{ id: 4, body: "They're having a FIRESALE?" }].to_json] }
11
11
  end
12
12
  end
13
13
 
@@ -17,15 +17,28 @@ describe Her::Model do
17
17
  subject { Foo::User.find(1) }
18
18
 
19
19
  describe :has_key? do
20
- it { should_not have_key(:unknown_method_for_a_user) }
21
- it { should_not have_key(:unknown_method_for_a_user) }
22
- it { should have_key(:name) }
23
- it { should have_key(:comments) }
20
+ it { is_expected.not_to have_key(:unknown_method_for_a_user) }
21
+ it { is_expected.not_to have_key(:unknown_method_for_a_user) }
22
+ it { is_expected.to have_key(:name) }
23
+ it { is_expected.to have_key(:comments) }
24
+ end
25
+
26
+ describe :serialization do
27
+ it "should be serialized without an error" do
28
+ expect { Marshal.dump(subject.comments) }.not_to raise_error
29
+ end
30
+
31
+ it "should correctly load serialized object" do
32
+ serialized_comments = Marshal.load(Marshal.dump(subject.comments))
33
+ expect(subject.comments.size).to eq(serialized_comments.size)
34
+ expect(subject.comments.first.id).to eq(serialized_comments.first.id)
35
+ expect(subject.comments.first.body).to eq(serialized_comments.first.body)
36
+ end
24
37
  end
25
38
 
26
39
  describe :[] do
27
- it { should_not have_key(:unknown_method_for_a_user) }
28
- specify { subject[:name].should == "Tobias Fünke" }
29
- specify { subject[:comments].first.body.should == "They're having a FIRESALE?" }
40
+ it { is_expected.not_to have_key(:unknown_method_for_a_user) }
41
+ specify { expect(subject[:name]).to eq("Tobias Fünke") }
42
+ specify { expect(subject[:comments].first.body).to eq("They're having a FIRESALE?") }
30
43
  end
31
44
  end
data/spec/spec_helper.rb CHANGED
@@ -1,11 +1,10 @@
1
- $:.unshift File.join(File.dirname(__FILE__), *%w[.. lib])
1
+ $LOAD_PATH.unshift File.join(File.dirname(__FILE__), "..", "lib")
2
2
 
3
3
  require "rspec"
4
- require "rspec/its"
5
4
  require "her"
6
5
 
7
6
  # Require everything in `spec/support`
8
- Dir[File.expand_path('../../spec/support/**/*.rb', __FILE__)].map(&method(:require))
7
+ Dir[File.expand_path("../../spec/support/**/*.rb", __FILE__)].map(&method(:require))
9
8
 
10
9
  # Remove ActiveModel deprecation message
11
10
  I18n.enforce_available_locales = false
@@ -3,7 +3,7 @@ module Her
3
3
  module Macros
4
4
  module ModelMacros
5
5
  # Create a class and automatically inject Her::Model into it
6
- def spawn_model(klass, options={}, &block)
6
+ def spawn_model(klass, options = {}, &block)
7
7
  super_class = options[:super_class]
8
8
  model_type = options[:type] || Her::Model
9
9
  new_class = if super_class
@@ -12,7 +12,7 @@ module Her
12
12
  Class.new
13
13
  end
14
14
  if klass =~ /::/
15
- base, submodel = klass.split(/::/).map{ |s| s.to_sym }
15
+ base, submodel = klass.split(/::/).map(&:to_sym)
16
16
  Object.const_set(base, Module.new) unless Object.const_defined?(base)
17
17
  Object.const_get(base).module_eval do
18
18
  remove_const submodel if constants.map(&:to_sym).include?(submodel)
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: her
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.8.1
4
+ version: 0.8.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - Rémi Prévost
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2015-09-07 00:00:00.000000000 Z
11
+ date: 2016-12-20 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rake
@@ -30,42 +30,14 @@ dependencies:
30
30
  requirements:
31
31
  - - "~>"
32
32
  - !ruby/object:Gem::Version
33
- version: '2.13'
33
+ version: '3.5'
34
34
  type: :development
35
35
  prerelease: false
36
36
  version_requirements: !ruby/object:Gem::Requirement
37
37
  requirements:
38
38
  - - "~>"
39
39
  - !ruby/object:Gem::Version
40
- version: '2.13'
41
- - !ruby/object:Gem::Dependency
42
- name: rspec-its
43
- requirement: !ruby/object:Gem::Requirement
44
- requirements:
45
- - - "~>"
46
- - !ruby/object:Gem::Version
47
- version: '1.0'
48
- type: :development
49
- prerelease: false
50
- version_requirements: !ruby/object:Gem::Requirement
51
- requirements:
52
- - - "~>"
53
- - !ruby/object:Gem::Version
54
- version: '1.0'
55
- - !ruby/object:Gem::Dependency
56
- name: fivemat
57
- requirement: !ruby/object:Gem::Requirement
58
- requirements:
59
- - - "~>"
60
- - !ruby/object:Gem::Version
61
- version: '1.2'
62
- type: :development
63
- prerelease: false
64
- version_requirements: !ruby/object:Gem::Requirement
65
- requirements:
66
- - - "~>"
67
- - !ruby/object:Gem::Version
68
- version: '1.2'
40
+ version: '3.5'
69
41
  - !ruby/object:Gem::Dependency
70
42
  name: json
71
43
  requirement: !ruby/object:Gem::Requirement
@@ -89,7 +61,7 @@ dependencies:
89
61
  version: 3.0.0
90
62
  - - "<="
91
63
  - !ruby/object:Gem::Version
92
- version: 4.3.0
64
+ version: 6.0.0
93
65
  type: :runtime
94
66
  prerelease: false
95
67
  version_requirements: !ruby/object:Gem::Requirement
@@ -99,7 +71,7 @@ dependencies:
99
71
  version: 3.0.0
100
72
  - - "<="
101
73
  - !ruby/object:Gem::Version
102
- version: 4.3.0
74
+ version: 6.0.0
103
75
  - !ruby/object:Gem::Dependency
104
76
  name: activesupport
105
77
  requirement: !ruby/object:Gem::Requirement
@@ -109,7 +81,7 @@ dependencies:
109
81
  version: 3.0.0
110
82
  - - "<="
111
83
  - !ruby/object:Gem::Version
112
- version: 4.3.0
84
+ version: 6.0.0
113
85
  type: :runtime
114
86
  prerelease: false
115
87
  version_requirements: !ruby/object:Gem::Requirement
@@ -119,7 +91,7 @@ dependencies:
119
91
  version: 3.0.0
120
92
  - - "<="
121
93
  - !ruby/object:Gem::Version
122
- version: 4.3.0
94
+ version: 6.0.0
123
95
  - !ruby/object:Gem::Dependency
124
96
  name: faraday
125
97
  requirement: !ruby/object:Gem::Requirement
@@ -163,6 +135,7 @@ extra_rdoc_files: []
163
135
  files:
164
136
  - ".gitignore"
165
137
  - ".rspec"
138
+ - ".rubocop.yml"
166
139
  - ".travis.yml"
167
140
  - ".yardopts"
168
141
  - CONTRIBUTING.md
@@ -252,7 +225,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
252
225
  version: '0'
253
226
  requirements: []
254
227
  rubyforge_project:
255
- rubygems_version: 2.2.3
228
+ rubygems_version: 2.5.1
256
229
  signing_key:
257
230
  specification_version: 4
258
231
  summary: A simple Representational State Transfer-based Hypertext Transfer Protocol-powered