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: 91129e0763fe824d498dfced45a91b9549410bd42137525075401fab8da28894
4
- data.tar.gz: 11a1b4ed69b47e7a14c517ba879858a0bf55dff22beba9a7d231ec9eb89d1414
3
+ metadata.gz: 13b9f12f268c910b4604b70fcba2079ff8bf46c884edabec3b92c686f766479b
4
+ data.tar.gz: 144f2e33fc8a41c4186c3708d7b7bbaca48028c8b25379d1b4197ca6e7e8a0ba
5
5
  SHA512:
6
- metadata.gz: a19b37f2e8c9a2c953c29daec2a538853522c7c8a6eaa2015f52fb6b2d124e1a5800bc2495973601923c73c5d86dd440985b70f8a9fc1cb589e196ce183cb0fc
7
- data.tar.gz: 8673f95e1d39b74e42240c4e23764124e6f1a233d3fc8abc15c3a48cf6ce60a4f5a37e79033d4948a095a1a0398353dfcb51fcb487309c8fda6071db1fe393d0
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:, 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,33 +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
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(object:)
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(object:)
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(object:)
81
- attributes = serialized_attributes(object: object)
82
- relationships = serialized_relationships(object: object)
84
+ def as_json
85
+ attributes = serialized_attributes
86
+ relationships = serialized_relationships
83
87
  attributes.merge(relationships)
84
88
  end
85
89
  end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module WolfCore
4
- VERSION = "1.0.129"
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.129
4
+ version: 1.1.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Javier Roncallo