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 +4 -4
- data/Gemfile.lock +1 -1
- data/lib/abc_jsonapi/included_resource.rb +6 -12
- data/lib/abc_jsonapi/serializer.rb +12 -13
- data/lib/abc_jsonapi/version.rb +1 -1
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 3aa80495503b3e1e6f693eed1c642b7f54b606dea641241b00ae1e7c1ea3c50f
|
4
|
+
data.tar.gz: aa67228eead67971ecee44be3b99cd47e1f41c7dcf0f9bb2aba7cc0417e20704
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 59558326daa35240ef5128960eacb9c4db399e88c338a5216c1050810be7a058064513a815af728a93ac7bd3f80795a3670fd53a6954cb4bcaecc43a3172e13b
|
7
|
+
data.tar.gz: c0a418f41997226b926ab28ca00468c12b0817547c6e958cdd279730db7c0ad228c459be6139c482e1312ded0e5d5c9f6a8fdaaca473ecc17c896a16ca1370df
|
data/Gemfile.lock
CHANGED
@@ -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
|
-
|
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
|
60
|
-
block.call(
|
53
|
+
if block.present?
|
54
|
+
block.call(collection)
|
61
55
|
else
|
62
|
-
collection.map(
|
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
|
-
|
48
|
+
relationships << { type: :has_one, name: relationship, block: block }
|
56
49
|
end
|
57
50
|
|
58
51
|
def has_many(relationship, &block)
|
59
|
-
|
52
|
+
relationships << { type: :has_many, name: relationship, block: block }
|
60
53
|
end
|
61
54
|
|
62
55
|
def belongs_to(relationship, &block)
|
63
|
-
|
56
|
+
relationships << { type: :belongs_to, name: relationship, block: block }
|
64
57
|
end
|
65
58
|
|
66
59
|
def resource_type(rtype = nil)
|
67
|
-
|
60
|
+
resource_type ||= rtype || Helpers.pluralize_if_necessary(default_type)
|
68
61
|
end
|
69
62
|
|
70
63
|
def attribute(name, &block)
|
71
|
-
|
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
|
data/lib/abc_jsonapi/version.rb
CHANGED
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.
|
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-
|
11
|
+
date: 2019-10-21 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|