wolf_core 1.0.128 → 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: 192259d44e9ec6c0acd98e7d6eace9f4cef88d42d654b1fb610393f28d86b477
4
- data.tar.gz: 5c937dc81c8c128444534f16bc29fc694c351145405e31ba44d356923dc7ff97
3
+ metadata.gz: 13b9f12f268c910b4604b70fcba2079ff8bf46c884edabec3b92c686f766479b
4
+ data.tar.gz: 144f2e33fc8a41c4186c3708d7b7bbaca48028c8b25379d1b4197ca6e7e8a0ba
5
5
  SHA512:
6
- metadata.gz: 10f4e0de69585ab2917db0ff33791585e1fc1f12662524e08c3b06f651c70372e143d8e9a064a0927cbd13c1bd57a6befc169393711c07eb4972fc928b864238
7
- data.tar.gz: f98865e77e10b76f68fdaac1e3fc3bdece17965e6e0c86c7c8f4aa9cce97034be95173ac64ffe4c5e293d5de4e19c86ec520f46a53d949b08c1ea300a0d5950b
6
+ metadata.gz: 7d012ded29da0a76e47628ffd7742809aaaf0cedfbfb2d5201f1ef60000c566424841241a2fd357950bbb5477ec7b420508827707715307971d13630f7012d56
7
+ data.tar.gz: 41f005fc6549476d9b3eb72d150568629a3bf42dc3ba52ff29a8ca7e7fd92d563cb5df45974f83bff0fa9641b7345a0e4f7407a90084256320acfe029a855782
@@ -2,14 +2,16 @@
2
2
 
3
3
  module WolfCore
4
4
  class InstanceApplicationSerializer
5
- attr_reader :options
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:, options: nil)
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,32 +46,33 @@ module WolfCore
42
46
  end
43
47
  end
44
48
 
45
- def serialize(object:, options: nil)
49
+ def serialize(object: nil, options: nil)
46
50
  Result.try do
51
+ @object = object if object
47
52
  options ||= {}
48
53
  @options = @options.merge(options)
49
54
 
50
- serialized_object = as_json(object: object).with_indifferent_access
55
+ serialized_object = as_json(object: @object).with_indifferent_access
51
56
  Result.success(data: { serialized_object: serialized_object })
52
57
  end
53
58
  end
54
59
 
55
60
  private
56
61
 
57
- def serialized_attributes(object:)
62
+ def serialized_attributes
58
63
  self.class._attributes.each_with_object({}) do |attribute, hash|
59
64
  hash[attribute] = if respond_to?(attribute, true)
60
65
  send(attribute)
61
- elsif object.respond_to?(attribute)
62
- object.public_send(attribute)
66
+ elsif @object.respond_to?(attribute)
67
+ @object.public_send(attribute)
63
68
  end
64
69
  end
65
70
  end
66
71
 
67
- def serialized_relationships(object:)
72
+ def serialized_relationships
68
73
  self.class._relationships.each_with_object({}) do |(key, serializer_class), hash|
69
74
  serializer = serializer_class.new(options: @options)
70
- related_object = object.public_send(key)
75
+ related_object = @object.public_send(key)
71
76
  hash[key] = if related_object.is_a?(Enumerable)
72
77
  serializer.serialize_all(collection: related_object)
73
78
  else
@@ -76,9 +81,9 @@ module WolfCore
76
81
  end
77
82
  end
78
83
 
79
- def as_json(object:)
80
- attributes = serialized_attributes(object: object)
81
- relationships = serialized_relationships(object: object)
84
+ def as_json
85
+ attributes = serialized_attributes
86
+ relationships = serialized_relationships
82
87
  attributes.merge(relationships)
83
88
  end
84
89
  end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module WolfCore
4
- VERSION = "1.0.128"
4
+ VERSION = "1.1.0"
5
5
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: wolf_core
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.128
4
+ version: 1.1.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Javier Roncallo