wolf_core 1.0.124 → 1.0.126

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: a6d0d4426f5be4937d10445bd9d98505ad98dc7a6aff55a1b46bd0244223a974
4
- data.tar.gz: 8f360acf026942c3f1a86198db0f23be9df94b0737f53489cabeb8818f7f5c98
3
+ metadata.gz: 77c52faa2c8055ca971a479f658db60d4110d0125d939ca6b3bbb4af3f24dfca
4
+ data.tar.gz: 7f2caa391cbffbab585f90d817e788731a494029af247d6177d91986e3c067e5
5
5
  SHA512:
6
- metadata.gz: 31337b7d69da176e2d999656e2af404b99fec14c6d62a9c7618b7153a394b849bb4d2ada3939caafa0f3e9589c06bc3bde6c6ffbf90df7cb66252a0d6bd7caef
7
- data.tar.gz: c7b56acc499f11030864909debfc75a4d97b9823c0ac0cebaa7633342802f7e103ac270ad683785723844007d8d969dfa2370b5423d5c890651ab0e59fb9e187
6
+ metadata.gz: 4b9a5093cc031f07cfce807d87e58a1aabe6a19b767c70e77df627766a75394617a897b86eeee5f7f413e8dcda8616d0d66c2cfaa0aad6352eb156a345d008f1
7
+ data.tar.gz: 50dcf730dfb87ed6c9ce8502543807f6b1e4cf6679fdafdce60563cd566044062d1cd43e4727293a233e3f7b1cac3e953e007c97d94e664df078e021d4cb9ee9
@@ -0,0 +1,85 @@
1
+ # frozen_string_literal: true
2
+
3
+ module WolfCore
4
+ class InstanceApplicationSerializer
5
+ attr_reader :options
6
+
7
+ def initialize(options: nil)
8
+ @options = options || {}
9
+ end
10
+
11
+ def self.attributes(*attrs)
12
+ _attributes.concat(attrs.map(&:to_sym))
13
+ end
14
+
15
+ def self.has_many(relationship, serializer:) # rubocop:disable Naming/PredicateName
16
+ _relationships[relationship.to_sym] = serializer
17
+ end
18
+
19
+ def self.has_one(relationship, serializer:) # rubocop:disable Naming/PredicateName
20
+ _relationships[relationship.to_sym] = serializer
21
+ end
22
+
23
+ def self._attributes
24
+ @_attributes ||= []
25
+ end
26
+
27
+ def self._relationships
28
+ @_relationships ||= {}
29
+ end
30
+
31
+ def serialize_all(collection:, options: nil)
32
+ Result.try do
33
+ options ||= {}
34
+ @options = @options.merge(options)
35
+
36
+ results = collection.map do |item|
37
+ result = serialize(object: item, options: @options)
38
+ result.raise_error
39
+ result
40
+ end
41
+ Result.success(data: { serialized_collection: results.map { |result| result.data.serialized_object } })
42
+ end
43
+ end
44
+
45
+ def serialize(object:, options: nil)
46
+ Result.try do
47
+ options ||= {}
48
+ @options = @options.merge(options)
49
+
50
+ serialized_object = as_json(object: object).with_indifferent_access
51
+ Result.success(data: { serialized_object: serialized_object })
52
+ end
53
+ end
54
+
55
+ private
56
+
57
+ def serialized_attributes(object:)
58
+ self.class._attributes.each_with_object({}) do |attribute, hash|
59
+ hash[attribute] = if respond_to?(attribute, true)
60
+ send(attribute)
61
+ elsif object.respond_to?(attribute)
62
+ object.public_send(attribute)
63
+ end
64
+ end
65
+ end
66
+
67
+ def serialized_relationships(object:)
68
+ self.class._relationships.each_with_object({}) do |(key, serializer_class), hash|
69
+ serializer = serializer_class.new(options: @options)
70
+ related_object = object.public_send(key)
71
+ hash[key] = if related_object.is_a?(Enumerable)
72
+ serializer.serialize_all(collection: related_object)
73
+ else
74
+ serializer.serialize(object: related_object)
75
+ end
76
+ end
77
+ end
78
+
79
+ def as_json(object:)
80
+ attributes = serialized_attributes(object: object)
81
+ relationships = serialized_relationships(object: object)
82
+ attributes.merge(relationships)
83
+ end
84
+ end
85
+ end
@@ -16,9 +16,13 @@ class Result
16
16
  end
17
17
 
18
18
  def self.try
19
- yield
19
+ try_result = yield
20
+ unless try_result.is_a?(Result)
21
+ raise StandardError, "Try result must be a Result object, got: #{try_result.class}"
22
+ end
23
+ try_result
20
24
  rescue StandardError => e
21
- Result.failure(error: { message: e.message, original_backtrace: e.backtrace })
25
+ Result.failure(error: { message: e.message, error_class: e.class, backtrace: e.backtrace })
22
26
  end
23
27
 
24
28
  def success?
@@ -71,4 +75,15 @@ class Result
71
75
  end
72
76
  fold_result
73
77
  end
78
+
79
+ def raise_error
80
+ if failure?
81
+ err_class = error.error_class || StandardError
82
+ e = err_class.new(error.message)
83
+ if error.backtrace
84
+ e.set_backtrace(error.backtrace)
85
+ end
86
+ raise e
87
+ end
88
+ end
74
89
  end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module WolfCore
4
- VERSION = "1.0.124"
4
+ VERSION = "1.0.126"
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.124
4
+ version: 1.0.126
5
5
  platform: ruby
6
6
  authors:
7
7
  - Javier Roncallo
@@ -142,6 +142,7 @@ files:
142
142
  - lib/wolf_core/infrastructure/http_operations.rb
143
143
  - lib/wolf_core/infrastructure/in_memory_storage_data_source.rb
144
144
  - lib/wolf_core/infrastructure/in_memory_storage_operations.rb
145
+ - lib/wolf_core/infrastructure/instance_application_serializer.rb
145
146
  - lib/wolf_core/infrastructure/lambda_function_data_source.rb
146
147
  - lib/wolf_core/infrastructure/lambda_function_operations.rb
147
148
  - lib/wolf_core/infrastructure/no_sql_db_data_source.rb