abc_jsonapi 0.2.3 → 0.2.4

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: ef48038b2ba006ddb0fe1b53cb96e3623240b7357eb945de3c1ea3b461cfc5ae
4
- data.tar.gz: e0011ed1501305346f0ca1291da2383f21ffa6665f0e7d30544939584c76dedd
3
+ metadata.gz: 3aa80495503b3e1e6f693eed1c642b7f54b606dea641241b00ae1e7c1ea3c50f
4
+ data.tar.gz: aa67228eead67971ecee44be3b99cd47e1f41c7dcf0f9bb2aba7cc0417e20704
5
5
  SHA512:
6
- metadata.gz: 58e68cc41d35c51b09d6076c8d89151f0441df314acacc76bc1516c07073f0436b5e7f7adc30bd76823e7ec5dbd6810b84e495053c35754621e238525f14b339
7
- data.tar.gz: 1e8d1c9564221c3c49cb559274734b73fe4a796af50918726c5abb75b20779fe54b111d89933a35a19cb667a7e02f3d06e4a6e4ffc4db8c475233428eab7ba68
6
+ metadata.gz: 59558326daa35240ef5128960eacb9c4db399e88c338a5216c1050810be7a058064513a815af728a93ac7bd3f80795a3670fd53a6954cb4bcaecc43a3172e13b
7
+ data.tar.gz: c0a418f41997226b926ab28ca00468c12b0817547c6e958cdd279730db7c0ad228c459be6139c482e1312ded0e5d5c9f6a8fdaaca473ecc17c896a16ca1370df
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- abc_jsonapi (0.2.3)
4
+ abc_jsonapi (0.2.4)
5
5
  activesupport (>= 5.2)
6
6
  i18n
7
7
 
@@ -12,13 +12,7 @@ module AbcJsonapi
12
12
  def serializable_hash
13
13
  includes.each do |include_path|
14
14
  include_chain = include_path.split('.')
15
- if resource.is_a?(Enumerable)
16
- resource.each do |single_res|
17
- get_included_records(single_res, include_chain.dup)
18
- end
19
- else
20
- get_included_records(resource, include_chain)
21
- end
15
+ get_included_records(resource, include_chain.dup)
22
16
  end
23
17
  includes_result.flatten
24
18
  end
@@ -36,7 +30,7 @@ module AbcJsonapi
36
30
 
37
31
  # Get included resource
38
32
  if resource.is_a?(Enumerable)
39
- resource = included_items_from_collection(resource, inc_resource_name)
33
+ resource = included_items_from_collection(resource, inc_resource_name, relationship.dig(:block))
40
34
  else
41
35
  resource = resource.public_send(inc_resource_name)
42
36
  end
@@ -54,12 +48,12 @@ module AbcJsonapi
54
48
  end
55
49
  end
56
50
 
57
- def included_items_from_collection(collection, include_name)
51
+ def included_items_from_collection(collection, include_name, block = nil)
58
52
  # Run custom include strategy if block given. Otherwise run default method
59
- if (block = relationship.dig(:block)).present?
60
- block.call(resource)
53
+ if block.present?
54
+ block.call(collection)
61
55
  else
62
- collection.map(&:include_name).flatten.uniq
56
+ collection.map{ |res| res.public_send(include_name) }.flatten.uniq
63
57
  end
64
58
  end
65
59
 
@@ -13,13 +13,7 @@ module AbcJsonapi
13
13
 
14
14
  attr_reader :resource, :result_hash, :resource_type, :resource_attributes,
15
15
  :relationships, :virtual_attributes, :includes, :meta
16
-
17
- included do
18
- @resource_attributes = []
19
- @relationships = []
20
- @virtual_attributes = []
21
- end
22
-
16
+
23
17
  def initialize(resource, options = {})
24
18
  @resource = resource
25
19
  @result_hash = { data: nil }
@@ -45,30 +39,35 @@ module AbcJsonapi
45
39
  end
46
40
 
47
41
  module ClassMethods
48
- attr_reader :resource_attributes, :relationships, :virtual_attributes
49
42
 
50
43
  def attributes(*attributes)
51
44
  @resource_attributes = attributes
52
45
  end
53
46
 
54
47
  def has_one(relationship, &block)
55
- @relationships << { type: :has_one, name: relationship, block: block }
48
+ relationships << { type: :has_one, name: relationship, block: block }
56
49
  end
57
50
 
58
51
  def has_many(relationship, &block)
59
- @relationships << { type: :has_many, name: relationship, block: block }
52
+ relationships << { type: :has_many, name: relationship, block: block }
60
53
  end
61
54
 
62
55
  def belongs_to(relationship, &block)
63
- @relationships << { type: :belongs_to, name: relationship, block: block }
56
+ relationships << { type: :belongs_to, name: relationship, block: block }
64
57
  end
65
58
 
66
59
  def resource_type(rtype = nil)
67
- @resource_type ||= rtype || Helpers.pluralize_if_necessary(default_type)
60
+ resource_type ||= rtype || Helpers.pluralize_if_necessary(default_type)
68
61
  end
69
62
 
70
63
  def attribute(name, &block)
71
- @virtual_attributes << { name: name, block: block }
64
+ virtual_attributes << { name: name, block: block }
65
+ end
66
+
67
+ [:resource_attributes, :relationships, :virtual_attributes].each do |var|
68
+ define_method(var) do
69
+ instance_variable_get("@#{var}") || instance_variable_set("@#{var}", [])
70
+ end
72
71
  end
73
72
 
74
73
  private
@@ -1,3 +1,3 @@
1
1
  module AbcJsonapi
2
- VERSION = "0.2.3"
2
+ VERSION = "0.2.4"
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: abc_jsonapi
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.3
4
+ version: 0.2.4
5
5
  platform: ruby
6
6
  authors:
7
7
  - Zhmakin Evgeniy
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2019-10-18 00:00:00.000000000 Z
11
+ date: 2019-10-21 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler