userlist 0.5.0 → 0.6.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
  SHA256:
3
- metadata.gz: a76254952c47cc9014e31019550491b96b1a8f46814cb6e44750ddf4c4d966f1
4
- data.tar.gz: 17eabd156ef0d1eda09a7480c9eb3f5822098f045123d2a2d9f33351630493d6
3
+ metadata.gz: 1f8e5ee8f9d8d18f98ebc7744ced642b9641ec6bd156fff4fbf0352be37dbaa2
4
+ data.tar.gz: a532a136941e347e7c7dc454f3ec7a735990f53abd8ddaff1bdf50eb67a3c219
5
5
  SHA512:
6
- metadata.gz: 7d3311432655c8b7758d59733845558f526208874f31fee9034dbbb8a75bca653dc39b8b18007928b7fae7d2cf3efae449126c2c355bd6f3b3e5d0e82f2a24a7
7
- data.tar.gz: 8531012b1ec1824c2ff60b537c641b17b534e60ea4665e476b66055f1f623ed51787c4a336402fb1adb63b1645e238008bb94ec64a2c7caf87cb21956d35111c
6
+ metadata.gz: 9065b7f9af8fc5692b334bcfa97410ee3260544f02cd6440cea57150d3daceb1e62c5b8fba8780f4309da764603966842b59064baad786918f1f23381a8ba0b5
7
+ data.tar.gz: f01918dafbaa9a837b25503f667b1387998036a5a626cce75fe3f45ef80539cb6e57aa9ab512bf8813f039b959eb138039ab0086bd98342dec5ef61f2d129969
@@ -8,7 +8,7 @@ module Userlist
8
8
  '/companies'
9
9
  end
10
10
 
11
- has_many :relationships, type: 'Userlist::Push::Relationship'
11
+ has_many :relationships, type: 'Userlist::Push::Relationship', inverse: :company
12
12
  has_many :users, type: 'Userlist::Push::User'
13
13
  has_one :user, type: 'Userlist::Push::User'
14
14
 
@@ -9,7 +9,8 @@ module Userlist
9
9
 
10
10
  def initialize(payload = {}, config = Userlist.config)
11
11
  raise Userlist::ArgumentError, 'Missing required payload' unless payload
12
- raise Userlist::ArgumentError, 'Missing required parameter :user or :company' unless payload[:user] || payload[:company]
12
+ raise Userlist::ArgumentError, 'Missing required parameter :user' unless payload[:user]
13
+ raise Userlist::ArgumentError, 'Missing required parameter :company' unless payload[:company]
13
14
 
14
15
  super
15
16
  end
@@ -20,13 +20,17 @@ module Userlist
20
20
  end
21
21
 
22
22
  def relationship_names
23
- @relationship_names ||= Set.new
23
+ @relationship_names ||= relationships.keys
24
+ end
25
+
26
+ def relationships
27
+ @relationships ||= {}
24
28
  end
25
29
 
26
30
  protected
27
31
 
28
32
  def has_one(name, type:) # rubocop:disable Naming/PredicateName
29
- relationship_names << name.to_sym
33
+ relationships[name.to_sym] = { type: type }
30
34
 
31
35
  generated_methods.class_eval <<-RUBY, __FILE__, __LINE__ + 1
32
36
  def #{name}
@@ -35,12 +39,14 @@ module Userlist
35
39
  RUBY
36
40
  end
37
41
 
38
- def has_many(name, type:) # rubocop:disable Naming/PredicateName
39
- relationship_names << name.to_sym
42
+ def has_many(name, **options) # rubocop:disable Naming/PredicateName
43
+ relationships[name.to_sym] = options
40
44
 
41
45
  generated_methods.class_eval <<-RUBY, __FILE__, __LINE__ + 1
42
46
  def #{name}
43
- ResourceCollection.new(payload[:#{name}], #{type}, config)
47
+ relationship = self.class.relationships[:#{name}]
48
+
49
+ ResourceCollection.new(payload[:#{name}], relationship, self, config)
44
50
  end
45
51
  RUBY
46
52
  end
@@ -3,19 +3,30 @@ module Userlist
3
3
  class ResourceCollection
4
4
  include Enumerable
5
5
 
6
- attr_reader :collection, :type, :config
6
+ attr_reader :collection, :relationship, :owner, :config
7
7
 
8
- def initialize(collection, type, config = Userlist.config)
8
+ def initialize(collection, relationship, owner, config = Userlist.config)
9
9
  @collection = Array(collection)
10
- @type = type
10
+ @relationship = relationship
11
+ @owner = owner
11
12
  @config = config
12
13
  end
13
14
 
14
15
  def each
15
16
  collection.each do |resource|
17
+ resource[inverse] = owner if inverse && resource.is_a?(Hash)
18
+
16
19
  yield type.from_payload(resource, config)
17
20
  end
18
21
  end
22
+
23
+ def type
24
+ Object.const_get(relationship[:type])
25
+ end
26
+
27
+ def inverse
28
+ relationship[:inverse]
29
+ end
19
30
  end
20
31
  end
21
32
  end
@@ -1,3 +1,5 @@
1
+ require 'set'
2
+
1
3
  module Userlist
2
4
  class Push
3
5
  class Serializer
@@ -4,7 +4,7 @@ module Userlist
4
4
  include Operations::Create
5
5
  include Operations::Delete
6
6
 
7
- has_many :relationships, type: 'Userlist::Push::Relationship'
7
+ has_many :relationships, type: 'Userlist::Push::Relationship', inverse: :user
8
8
  has_many :companies, type: 'Userlist::Push::Company'
9
9
  has_one :company, type: 'Userlist::Push::Company'
10
10
 
@@ -1,3 +1,3 @@
1
1
  module Userlist
2
- VERSION = '0.5.0'.freeze
2
+ VERSION = '0.6.0'.freeze
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: userlist
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.5.0
4
+ version: 0.6.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Benedikt Deicke
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2021-01-22 00:00:00.000000000 Z
11
+ date: 2021-05-14 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler