ecoportal-api-v2 0.8.30 → 0.8.31
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 +4 -4
- data/CHANGELOG.md +23 -1
- data/lib/ecoportal/api/common/content/array_model.rb +3 -2
- data/lib/ecoportal/api/common/content/class_helpers.rb +23 -9
- data/lib/ecoportal/api/common/content/collection_model.rb +2 -1
- data/lib/ecoportal/api/common/content/double_model.rb +12 -8
- data/lib/ecoportal/api/v2/page/component/selection_option.rb +1 -1
- data/lib/ecoportal/api/v2_version.rb +1 -1
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: f7fdd84d3767ffc6a9a5adfe976ad4a485754c076475d01da46877aa38e01d65
|
4
|
+
data.tar.gz: 8c775abd99edba549b7b8abe049ac06c631005671b972a9340795857b378d39a
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: a4cc041705a2ac1e2ebe6eaaf7cc30fe79b780a1b5dc397bb1526fe0112df0a050d07452100e0174110e5cf30271ef1b3ddbb4f8b866c66824755c6f6bd287ad
|
7
|
+
data.tar.gz: 1bdb9cd3ff3cf0b64f282f4be9ec372c276c8deee7b47b8a142eab20069c2ef072966b87a1a1c9047e01a54895354bd7e2a9a404215eabf6bbc689641297b8d4
|
data/CHANGELOG.md
CHANGED
@@ -1,7 +1,29 @@
|
|
1
1
|
# Change Log
|
2
2
|
All notable changes to this project will be documented in this file.
|
3
3
|
|
4
|
-
## [0.8.
|
4
|
+
## [0.8.31] - 2022-08-xx
|
5
|
+
|
6
|
+
### Added
|
7
|
+
- `Ecoportal::API:V2::Common::Content::ClassHelpers.uid` to generate a random uid
|
8
|
+
|
9
|
+
### Changed
|
10
|
+
- added **inheritable attributes** to
|
11
|
+
- `Ecoportal::API:V2::Common::Content::ArrayModel` (:order_matteres, :uniq)
|
12
|
+
- `Ecoportal::API:V2::Common::Content::CollectionModel` (klass, :order_matters, :order_key, :items_key, :new_item)
|
13
|
+
- `Ecoportal::API:V2::Common::Content::DoubleModel` (:key)
|
14
|
+
- moved `SecureRandom` dependency to `Ecoportal::API:V2::Common::Content::ClassHelpers`
|
15
|
+
|
16
|
+
### Fixed
|
17
|
+
- `Ecoportal::API::V2::Page::Component::SelectionOption#numeric!` fix conversion
|
18
|
+
- `Ecoportal::API:V2::Common::Content::ClassHelpers`, `new_class`
|
19
|
+
- **Added** parameter `namespace` (default: the class `inherits` parameter): it's purpose is to define the namespace where the new class will sit
|
20
|
+
- **Fixed** also the namespace where the new class constant will sit (it was using always the source class `self`)
|
21
|
+
- **Improved** `name` parameter to have a default value that is randomized. This is useful when we just want to do something like `SomeClass.new_class`, where the name of the new class doesn't really matter.
|
22
|
+
- **Fixed** lost reference of class resolvers when using `embeds_many`
|
23
|
+
- `Ecoportal::API:V2::Common::Content::ClassHelpers`, `resolve_class` accepts now a `Hash` with `key` referrer class, and `value` any of the previously accepted ones (i.e. `Symbol` or `String`). To this purpose, `source_class` parameter has been added.
|
24
|
+
- This has been natively wrapped in `Ecoportal::API:V2::Common::Content::DoubleModel.embeds_many`, as it naturally shares the `class_resolver` with a third party class that loses the reference.
|
25
|
+
|
26
|
+
## [0.8.30] - 2022-07-11
|
5
27
|
|
6
28
|
### Added
|
7
29
|
- `Ecoportal::API::V2::Page::MoultCounter`
|
@@ -6,7 +6,7 @@ module Ecoportal
|
|
6
6
|
# @note
|
7
7
|
# - Its purpose is to handle an Array of basic objects (i.e. `Date`, `String`, `Number`)
|
8
8
|
class ArrayModel < Content::DoubleModel
|
9
|
-
class TypeMismatchedComparison <
|
9
|
+
class TypeMismatchedComparison < StandardError
|
10
10
|
def initialize (this: nil, that: msg = "Trying to compare objects with different behavior.")
|
11
11
|
if this
|
12
12
|
msg += " From object with 'order_matters: #{this.order_matters?}' and 'uniq: #{this.uniq?}'."
|
@@ -30,9 +30,10 @@ module Ecoportal
|
|
30
30
|
raise "To use this comparison both objects should be `ArrayModel`" unless a.is_a?(ArrayModel) && b.is_a?(ArrayModel)
|
31
31
|
(a.order_matters? == b.order_matters?) && (a.uniq? == b.uniq?)
|
32
32
|
end
|
33
|
-
|
34
33
|
end
|
35
34
|
|
35
|
+
inheritable_class_vars :order_matteres, :uniq
|
36
|
+
|
36
37
|
def initialize(doc = [], parent: self, key: nil)
|
37
38
|
super(doc, parent: parent, key: key)
|
38
39
|
end
|
@@ -1,3 +1,4 @@
|
|
1
|
+
require 'securerandom'
|
1
2
|
module Ecoportal
|
2
3
|
module API
|
3
4
|
module Common
|
@@ -10,9 +11,10 @@ module Ecoportal
|
|
10
11
|
# @note it caches the resolved `klass`es
|
11
12
|
# @raise [Exception] when could not resolve if `exception` is `true`
|
12
13
|
# @param klass [Class, String, Symbol] the class to resolve
|
14
|
+
# @param source_class [Class] when the reference to `klass` belongs to a different inheritance chain.
|
13
15
|
# @param exception [Boolean] if it should raise exception when could not resolve
|
14
16
|
# @return [Class] the `Class` constant
|
15
|
-
def resolve_class(klass, exception: true)
|
17
|
+
def resolve_class(klass, source_class: self, exception: true)
|
16
18
|
@resolved ||= {}
|
17
19
|
@resolved[klass] ||=
|
18
20
|
case klass
|
@@ -25,18 +27,24 @@ module Ecoportal
|
|
25
27
|
raise if exception
|
26
28
|
end
|
27
29
|
when Symbol
|
28
|
-
resolve_class(
|
30
|
+
source_class.resolve_class(source_class.send(klass))
|
31
|
+
when Hash
|
32
|
+
referrer, referred = klass.first
|
33
|
+
resolve_class(referred, source_class: referrer, exception: exception)
|
29
34
|
else
|
30
35
|
raise "Unknown class: #{klass}" if exception
|
31
36
|
end
|
32
37
|
end
|
33
38
|
|
34
39
|
# Helper to normalize `key` into a correct `ruby` **constant name**
|
40
|
+
# @note it removes namespace syntax `::`
|
35
41
|
# @param key [String, Symbol] to be normalized
|
36
42
|
# @return [String] a correct constant name
|
37
43
|
def to_constant(key)
|
38
|
-
str_name = key.to_s.strip.split(
|
39
|
-
str.slice(0).upcase + str.slice(1..-1)
|
44
|
+
str_name = key.to_s.strip.split(/::/).compact.map do |str|
|
45
|
+
str.slice(0).upcase + str.slice(1..-1)
|
46
|
+
end.join("").split(/[\-\_ :]+/i).compact.map do |str|
|
47
|
+
str.slice(0).upcase + str.slice(1..-1)
|
40
48
|
end.join("")
|
41
49
|
end
|
42
50
|
|
@@ -49,20 +57,27 @@ module Ecoportal
|
|
49
57
|
str
|
50
58
|
end
|
51
59
|
|
60
|
+
# Generates random ids in hexadecimal to use in class name generation.
|
61
|
+
# @param len [Integeter] length of the `uid`
|
62
|
+
# @return [String] a random unique id of length `len`
|
63
|
+
def uid(len = 8);
|
64
|
+
SecureRandom.hex(len/2)
|
65
|
+
end
|
66
|
+
|
52
67
|
# If the class for `name` exists, it returns it. Otherwise it generates it.
|
53
68
|
# @param name [String, Symbol] the name of the new class
|
54
69
|
# @param inherits [Class] the parent class to _inherit_ from
|
70
|
+
# @param namespace [Class, String] an existing `constant` (class or module) the new class will be namespaced on
|
55
71
|
# @yield [child_class] configure the new class
|
56
72
|
# @yieldparam child_class [Class] the new class
|
57
73
|
# @return [Class] the new generated class
|
58
|
-
def new_class(name, inherits:)
|
74
|
+
def new_class(name = "Child#{uid}", inherits: self, namespace: inherits)
|
59
75
|
name = name.to_sym.freeze
|
60
76
|
class_name = to_constant(name)
|
61
|
-
full_class_name = "#{inherits}::#{class_name}"
|
62
77
|
|
63
|
-
unless target_class = resolve_class(
|
78
|
+
unless target_class = resolve_class("#{namespace}::#{class_name}", exception: false)
|
64
79
|
target_class = Class.new(inherits)
|
65
|
-
|
80
|
+
Kernel.const_get(namespace.to_s).const_set class_name, target_class
|
66
81
|
end
|
67
82
|
|
68
83
|
target_class.tap do |klass|
|
@@ -138,7 +153,6 @@ module Ecoportal
|
|
138
153
|
subclass.instance_variable_set(instance_var, value.freeze)
|
139
154
|
end
|
140
155
|
end
|
141
|
-
|
142
156
|
end
|
143
157
|
end
|
144
158
|
end
|
@@ -82,11 +82,12 @@ module Ecoportal
|
|
82
82
|
klass.uniq = uniq
|
83
83
|
end
|
84
84
|
end
|
85
|
-
|
86
85
|
end
|
87
86
|
|
88
87
|
include Enumerable
|
89
88
|
|
89
|
+
inheritable_class_vars :klass, :order_matters, :order_key, :items_key, :new_item
|
90
|
+
|
90
91
|
def initialize(ini_doc = [], parent: self, key: nil)
|
91
92
|
unless self.class.klass?
|
92
93
|
raise "Undefined base 'klass' or 'new_item' callback for #{self.class}"
|
@@ -1,5 +1,3 @@
|
|
1
|
-
require 'securerandom'
|
2
|
-
|
3
1
|
module Ecoportal
|
4
2
|
module API
|
5
3
|
module Common
|
@@ -12,7 +10,7 @@ module Ecoportal
|
|
12
10
|
extend Common::Content::ClassHelpers
|
13
11
|
include Common::Content::ModelHelpers
|
14
12
|
|
15
|
-
class UnlinkedModel <
|
13
|
+
class UnlinkedModel < StandardError
|
16
14
|
def initialize (msg = "Something went wrong when linking the document.", from: nil, key: nil)
|
17
15
|
msg += " From: #{from}." if from
|
18
16
|
msg += " key: #{key}." if key
|
@@ -31,8 +29,8 @@ module Ecoportal
|
|
31
29
|
@key = value.to_s.freeze
|
32
30
|
end
|
33
31
|
|
34
|
-
def new_uuid(length:
|
35
|
-
|
32
|
+
def new_uuid(length: 24)
|
33
|
+
uid(length)
|
36
34
|
end
|
37
35
|
|
38
36
|
# Same as `attr_reader` but links to a subjacent `Hash` model property
|
@@ -208,18 +206,24 @@ module Ecoportal
|
|
208
206
|
else
|
209
207
|
raise "You should either specify the 'klass' of the elements or the 'enum_class'"
|
210
208
|
end
|
211
|
-
embed(method, key: key, multiple: true, klass: eclass)
|
209
|
+
embed(method, key: key, multiple: true, klass: eclass) do |instance_with_called_method|
|
210
|
+
# keep reference to the original class to resolve the `klass` dependency
|
211
|
+
# See stackoverflow: https://stackoverflow.com/a/73709529/4352306
|
212
|
+
referrer_class = instance_with_called_method.class
|
213
|
+
eclass.klass = {referrer_class => klass} if klass
|
214
|
+
end
|
212
215
|
end
|
213
216
|
|
214
217
|
private
|
215
218
|
|
216
|
-
def embed(method, key: method, nullable: false, multiple: false, klass
|
219
|
+
def embed(method, key: method, nullable: false, multiple: false, klass:, &block)
|
217
220
|
method = method.to_s.freeze
|
218
221
|
var = instance_variable_name(method).freeze
|
219
222
|
k = key.to_s.freeze
|
220
223
|
|
221
224
|
# retrieving method (getter)
|
222
225
|
define_method(method) do
|
226
|
+
yield(self) if block_given?
|
223
227
|
return instance_variable_get(var) if instance_variable_defined?(var)
|
224
228
|
unless nullable
|
225
229
|
doc[k] ||= multiple ? [] : {}
|
@@ -239,7 +243,7 @@ module Ecoportal
|
|
239
243
|
|
240
244
|
end
|
241
245
|
|
242
|
-
inheritable_class_vars :forced_model_keys
|
246
|
+
inheritable_class_vars :forced_model_keys, :key
|
243
247
|
|
244
248
|
attr_reader :_parent, :_key
|
245
249
|
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: ecoportal-api-v2
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.8.
|
4
|
+
version: 0.8.31
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Oscar Segura
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date: 2022-
|
11
|
+
date: 2022-09-14 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|