jsonapi-realizer 2.0.1 → 2.0.2
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/jsonapi/realizer/resource.rb +57 -53
- data/lib/jsonapi/realizer/version.rb +1 -1
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 249bd86429ad678910d08bac26aa46fc25e6d22a326aef4c613c59bb1f8d7ace
|
4
|
+
data.tar.gz: ecc579c9998df302d71287d348fa4fe83c879913520f5d939b88e57af3dd6adf
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: e4f9fd5458983657b7ac6ad4ae84f4dcbfb00ffbb95366f4f28d180081e9a54baebd799d059be21a14aedc778eadd5113b68d5b57c48e02032b3bf05289370db
|
7
|
+
data.tar.gz: 268221f1161db90714f3c4660468948a6f30349256b2f14e22a0e65212875dfae0fef5a5b23ed7826528500bca40fb7ee6027ea9da54ab3e07e2765ccc196484
|
@@ -1,6 +1,8 @@
|
|
1
1
|
module JSONAPI
|
2
2
|
module Realizer
|
3
|
-
|
3
|
+
module Resource
|
4
|
+
extend ActiveSupport::Concern
|
5
|
+
|
4
6
|
attr_reader :model
|
5
7
|
|
6
8
|
def initialize(model)
|
@@ -31,73 +33,75 @@ module JSONAPI
|
|
31
33
|
self.class.configuration
|
32
34
|
end
|
33
35
|
|
34
|
-
|
35
|
-
|
36
|
-
|
36
|
+
class_methods do
|
37
|
+
def attribute(name)
|
38
|
+
attributes.public_send(name.to_sym)
|
39
|
+
end
|
37
40
|
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
+
def relationship(name)
|
42
|
+
relationships.public_send(name.to_sym)
|
43
|
+
end
|
41
44
|
|
42
|
-
|
43
|
-
|
44
|
-
|
45
|
+
def valid_attribute?(name, value)
|
46
|
+
attributes.respond_to?(name.to_sym)
|
47
|
+
end
|
45
48
|
|
46
|
-
|
47
|
-
|
48
|
-
|
49
|
+
def valid_relationship?(name, value)
|
50
|
+
relationships.respond_to?(name.to_sym)
|
51
|
+
end
|
49
52
|
|
50
|
-
|
51
|
-
|
52
|
-
|
53
|
+
def valid_sparse_field?(name)
|
54
|
+
attribute(name).fetch(:selectable)
|
55
|
+
end
|
53
56
|
|
54
|
-
|
55
|
-
|
56
|
-
|
57
|
+
def valid_includes?(name)
|
58
|
+
relationship(name).fetch(:includable)
|
59
|
+
end
|
57
60
|
|
58
|
-
|
59
|
-
|
60
|
-
|
61
|
+
def has(name, selectable: true)
|
62
|
+
attributes.public_send("#{name}=", OpenStruct.new({name: name, selectable: selectable}))
|
63
|
+
end
|
61
64
|
|
62
|
-
|
63
|
-
|
64
|
-
|
65
|
+
def has_related(name, as: name, includable: true)
|
66
|
+
relationships.public_send("#{name}=", OpenStruct.new({name: name, as: as, includable: includable}))
|
67
|
+
end
|
65
68
|
|
66
|
-
|
67
|
-
|
68
|
-
|
69
|
+
def has_one(name, as: name, includable: true)
|
70
|
+
has_related(name, as: name, includable: includable)
|
71
|
+
end
|
69
72
|
|
70
|
-
|
71
|
-
|
72
|
-
|
73
|
+
def has_many(name, as: name, includable: true)
|
74
|
+
has_related(name, as: name, includable: includable)
|
75
|
+
end
|
73
76
|
|
74
|
-
|
75
|
-
|
76
|
-
|
77
|
+
def adapter
|
78
|
+
configuration.adapter
|
79
|
+
end
|
77
80
|
|
78
|
-
|
79
|
-
|
80
|
-
|
81
|
+
def attributes
|
82
|
+
configuration.attributes
|
83
|
+
end
|
81
84
|
|
82
|
-
|
83
|
-
|
84
|
-
|
85
|
+
def relationships
|
86
|
+
configuration.relationships
|
87
|
+
end
|
85
88
|
|
86
|
-
|
87
|
-
|
88
|
-
|
89
|
+
def model_class
|
90
|
+
configuration.model_class
|
91
|
+
end
|
89
92
|
|
90
|
-
|
91
|
-
|
92
|
-
|
93
|
-
|
94
|
-
|
95
|
-
|
96
|
-
|
97
|
-
|
93
|
+
def register(type, class_name:, adapter:)
|
94
|
+
JSONAPI::Realizer.register(
|
95
|
+
resource_class: self,
|
96
|
+
model_class: class_name.constantize,
|
97
|
+
adapter: JSONAPI::Realizer::Adapter.new(adapter),
|
98
|
+
type: type.to_s
|
99
|
+
)
|
100
|
+
end
|
98
101
|
|
99
|
-
|
100
|
-
|
102
|
+
def configuration
|
103
|
+
JSONAPI::Realizer.resource_mapping.fetch(self)
|
104
|
+
end
|
101
105
|
end
|
102
106
|
end
|
103
107
|
end
|