wolf_core 1.0.129 → 1.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 13b9f12f268c910b4604b70fcba2079ff8bf46c884edabec3b92c686f766479b
|
4
|
+
data.tar.gz: 144f2e33fc8a41c4186c3708d7b7bbaca48028c8b25379d1b4197ca6e7e8a0ba
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 7d012ded29da0a76e47628ffd7742809aaaf0cedfbfb2d5201f1ef60000c566424841241a2fd357950bbb5477ec7b420508827707715307971d13630f7012d56
|
7
|
+
data.tar.gz: 41f005fc6549476d9b3eb72d150568629a3bf42dc3ba52ff29a8ca7e7fd92d563cb5df45974f83bff0fa9641b7345a0e4f7407a90084256320acfe029a855782
|
@@ -4,12 +4,14 @@ module WolfCore
|
|
4
4
|
class InstanceApplicationSerializer
|
5
5
|
attr_reader :options, :object
|
6
6
|
|
7
|
-
def initialize(options: nil)
|
7
|
+
def initialize(object: nil, collection: nil, options: nil)
|
8
|
+
@object = object
|
9
|
+
@collection = collection
|
8
10
|
@options = options || {}
|
9
11
|
end
|
10
12
|
|
11
13
|
def self.attributes(*attrs)
|
12
|
-
_attributes.concat(attrs.map(&:to_sym))
|
14
|
+
_attributes.concat(attrs.map(&:to_sym)).uniq!
|
13
15
|
end
|
14
16
|
|
15
17
|
def self.has_many(relationship, serializer:) # rubocop:disable Naming/PredicateName
|
@@ -28,12 +30,14 @@ module WolfCore
|
|
28
30
|
@_relationships ||= {}
|
29
31
|
end
|
30
32
|
|
31
|
-
def serialize_all(collection
|
33
|
+
def serialize_all(collection: nil, options: nil)
|
32
34
|
Result.try do
|
35
|
+
@collection = collection if collection
|
36
|
+
|
33
37
|
options ||= {}
|
34
38
|
@options = @options.merge(options)
|
35
39
|
|
36
|
-
results = collection.map do |item|
|
40
|
+
results = @collection.map do |item|
|
37
41
|
result = serialize(object: item, options: @options)
|
38
42
|
result.raise_error
|
39
43
|
result
|
@@ -42,33 +46,33 @@ module WolfCore
|
|
42
46
|
end
|
43
47
|
end
|
44
48
|
|
45
|
-
def serialize(object
|
49
|
+
def serialize(object: nil, options: nil)
|
46
50
|
Result.try do
|
47
|
-
@object = object
|
51
|
+
@object = object if object
|
48
52
|
options ||= {}
|
49
53
|
@options = @options.merge(options)
|
50
54
|
|
51
|
-
serialized_object = as_json(object: object).with_indifferent_access
|
55
|
+
serialized_object = as_json(object: @object).with_indifferent_access
|
52
56
|
Result.success(data: { serialized_object: serialized_object })
|
53
57
|
end
|
54
58
|
end
|
55
59
|
|
56
60
|
private
|
57
61
|
|
58
|
-
def serialized_attributes
|
62
|
+
def serialized_attributes
|
59
63
|
self.class._attributes.each_with_object({}) do |attribute, hash|
|
60
64
|
hash[attribute] = if respond_to?(attribute, true)
|
61
65
|
send(attribute)
|
62
|
-
elsif object.respond_to?(attribute)
|
63
|
-
object.public_send(attribute)
|
66
|
+
elsif @object.respond_to?(attribute)
|
67
|
+
@object.public_send(attribute)
|
64
68
|
end
|
65
69
|
end
|
66
70
|
end
|
67
71
|
|
68
|
-
def serialized_relationships
|
72
|
+
def serialized_relationships
|
69
73
|
self.class._relationships.each_with_object({}) do |(key, serializer_class), hash|
|
70
74
|
serializer = serializer_class.new(options: @options)
|
71
|
-
related_object = object.public_send(key)
|
75
|
+
related_object = @object.public_send(key)
|
72
76
|
hash[key] = if related_object.is_a?(Enumerable)
|
73
77
|
serializer.serialize_all(collection: related_object)
|
74
78
|
else
|
@@ -77,9 +81,9 @@ module WolfCore
|
|
77
81
|
end
|
78
82
|
end
|
79
83
|
|
80
|
-
def as_json
|
81
|
-
attributes = serialized_attributes
|
82
|
-
relationships = serialized_relationships
|
84
|
+
def as_json
|
85
|
+
attributes = serialized_attributes
|
86
|
+
relationships = serialized_relationships
|
83
87
|
attributes.merge(relationships)
|
84
88
|
end
|
85
89
|
end
|
data/lib/wolf_core/version.rb
CHANGED