distant 0.1.1 → 0.1.2
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/distant.gemspec +1 -1
- data/lib/distant/base.rb +16 -0
- data/spec/client/base_spec.rb +30 -0
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 26713c61cff1da17cdee7e0eb0b26cfa735aa2ec
|
4
|
+
data.tar.gz: 699ec5c4908a19d80dba0f9c84226ce02e2208e8
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 278282dcb599fc56884c04b9b10782f65615813fd5dee3726d2a277b589cffc887e499718f9b5ad74cf7c8c0225a682c6ecbf8cd418de6be614a7e72f0afed73
|
7
|
+
data.tar.gz: df11c199e6bf8de859cbb3fe585c949e390aa486cc8c13d90ed9efb1a84df385265142473ae4a164df1ba9cbf4796be198b263b0439dd88355cd6bf8763624d0
|
data/distant.gemspec
CHANGED
data/lib/distant/base.rb
CHANGED
@@ -11,6 +11,7 @@ module Distant
|
|
11
11
|
|
12
12
|
def self.inherited(klass)
|
13
13
|
klass.extend(ClassMethods)
|
14
|
+
klass.init_class_vars
|
14
15
|
end
|
15
16
|
|
16
17
|
def connection
|
@@ -18,6 +19,11 @@ module Distant
|
|
18
19
|
end
|
19
20
|
|
20
21
|
module ClassMethods
|
22
|
+
def init_class_vars
|
23
|
+
@has_many = [ ]
|
24
|
+
@belongs_to = [ ]
|
25
|
+
end
|
26
|
+
|
21
27
|
def connection
|
22
28
|
@@connection ||= Distant::Connection.new( Distant.config )
|
23
29
|
end
|
@@ -41,6 +47,7 @@ module Distant
|
|
41
47
|
end
|
42
48
|
|
43
49
|
def has_many(plural, route)
|
50
|
+
@has_many << plural.to_sym
|
44
51
|
define_method(plural) do
|
45
52
|
path = self.class.path_closure_generator(route).call(id: self.id)
|
46
53
|
headers = Distant.config.default_headers('')
|
@@ -51,7 +58,16 @@ module Distant
|
|
51
58
|
end
|
52
59
|
end
|
53
60
|
|
61
|
+
def has_many?(plural)
|
62
|
+
@has_many.include? plural.to_sym
|
63
|
+
end
|
64
|
+
|
65
|
+
def belongs_to?(singular)
|
66
|
+
@belongs_to.include? singular.to_sym
|
67
|
+
end
|
68
|
+
|
54
69
|
def belongs_to(singular, route)
|
70
|
+
@belongs_to << singular.to_sym
|
55
71
|
define_method(singular) do
|
56
72
|
foreign_key_attr = singular.to_s + '_id'
|
57
73
|
foreign_key_value = self.send(foreign_key_attr)
|
data/spec/client/base_spec.rb
CHANGED
@@ -223,4 +223,34 @@ describe Distant::Base do
|
|
223
223
|
end
|
224
224
|
end
|
225
225
|
end
|
226
|
+
|
227
|
+
describe '#has_many?(other_things)' do
|
228
|
+
context 'when the thing' do
|
229
|
+
context 'does have many other_things' do
|
230
|
+
it 'returns true' do
|
231
|
+
expect(Distant::BaseTest.has_many?(:sub_tests)).to be_truthy
|
232
|
+
end
|
233
|
+
end
|
234
|
+
context 'does not have many other_things' do
|
235
|
+
it 'returns false' do
|
236
|
+
expect(Distant::BaseTest.has_many?(:sdf987sd98f7)).to be_falsey
|
237
|
+
end
|
238
|
+
end
|
239
|
+
end
|
240
|
+
end
|
241
|
+
|
242
|
+
describe '#belongs_to?(other_thing)' do
|
243
|
+
context 'when the thing' do
|
244
|
+
context 'does belong to other_thing' do
|
245
|
+
it 'returns true' do
|
246
|
+
expect(Distant::SubTest.belongs_to?(:base_test)).to be_truthy
|
247
|
+
end
|
248
|
+
end
|
249
|
+
context 'does not have many other_things' do
|
250
|
+
it 'returns false' do
|
251
|
+
expect(Distant::SubTest.belongs_to?(:kjh234kjh23kj4h)).to be_falsey
|
252
|
+
end
|
253
|
+
end
|
254
|
+
end
|
255
|
+
end
|
226
256
|
end
|