ecoportal-api-v2 0.9.7 → 1.0.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: cd61279f012c243d8ac7dc7168273fb76ad2555871f464c17c38a2a54147aea8
4
- data.tar.gz: a7837403a1df3d5e386d9fd71a24cb29eb98c43ea12637c9a4dd19301ac13bec
3
+ metadata.gz: d8f33b34cbba08724022856ecf54859aa40fed40bbeb2d06972edb97a0f5a171
4
+ data.tar.gz: bb53bb8e3e882b93aed2bb6f6a6f43df1c9405e5b4a6ac18b86d99ab52b28be8
5
5
  SHA512:
6
- metadata.gz: bb9cdb7833b7eae455274cd89348e395c01bd423d8d9591c62b1f66a129b8e3ef9a13f6301870cf75b4707be6306e4ee83299b6bcc9ce05d3224741d44141019
7
- data.tar.gz: 9009e8341f147df8610777d99026f6a2107b2fe64d5e0e0c5fa0b7623d632982899e00728a02c6a737395a6755a40f19a54e805b86ab6648cef51e46e3c3c9a4
6
+ metadata.gz: 6bf8ffdc02fb8471a833e3451abd89bb09b573f6a711cbc99eec189d528bd7c90643a0a33d86ae89c08fa449c65c3bdf0a1c1353b33afe049ffa62a3dac5a6ac
7
+ data.tar.gz: 8444d1a3c41222e186aeffbf15158b8048187508a66d225413aab38d1a07f61a0a14508d1259eed7b131c2806cdac537814b057a017b3aad8d21514a515244c1
data/CHANGELOG.md CHANGED
@@ -1,12 +1,23 @@
1
1
  # Change Log
2
2
  All notable changes to this project will be documented in this file.
3
3
 
4
- ## [0.9.8] - 2022-11-xx
4
+ ## [1.0.1] - 2023-02-xx
5
5
 
6
6
  ### Added
7
7
  ### Changed
8
8
  ### Fixed
9
9
 
10
+ ## [1.0.1] - 2023-02-24
11
+
12
+ ### Added
13
+ - Allowed `read_only` mode to support `emdeds_many` with item class without `method_key`
14
+ - `Ecoportal::API::Common::Content::DoubleModel`
15
+ - `Ecoportal::API::Common::Content::CollectionModel`
16
+ - `Ecoportal::API::Common::Content::ArrayModel`
17
+
18
+ ### Changed
19
+ - It is a risky change (so treated as a **braking change**)
20
+
10
21
  ## [0.9.7] - 2022-11-29
11
22
 
12
23
  ### Added
@@ -34,8 +34,8 @@ module Ecoportal
34
34
 
35
35
  inheritable_class_vars :order_matteres, :uniq
36
36
 
37
- def initialize(doc = [], parent: self, key: nil)
38
- super(doc, parent: parent, key: key)
37
+ def initialize(doc = [], parent: self, key: nil, read_only: false)
38
+ super(doc, parent: parent, key: key, read_only: read_only)
39
39
  end
40
40
 
41
41
  def order_matters?; self.class.order_matters; end
@@ -52,7 +52,7 @@ module Ecoportal
52
52
  # @yieldparam doc [Hash]
53
53
  # @yieldreturn [Klass] instance object of the target `klass`
54
54
  # @return [Klass] instance object of the target `klass`
55
- def new_item(doc = NOT_USED, parent: nil, key: nil, &block)
55
+ def new_item(doc = NOT_USED, parent: nil, key: nil, read_only: false, &block)
56
56
  if block
57
57
  @new_item = block
58
58
  elsif used_param?(doc)
@@ -61,7 +61,7 @@ module Ecoportal
61
61
  @new_item.call(doc, parent, key)
62
62
  else
63
63
  if target_class = self.klass(doc)
64
- doc.is_a?(target_class) ? doc : target_class.new(doc, parent: parent, key: key)
64
+ doc.is_a?(target_class) ? doc : target_class.new(doc, parent: parent, key: key, read_only: read_only)
65
65
  else
66
66
  raise "Could not find a class for: #{doc}"
67
67
  end
@@ -88,7 +88,7 @@ module Ecoportal
88
88
 
89
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
+ def initialize(ini_doc = [], parent: self, key: nil, read_only: false)
92
92
  unless self.class.klass?
93
93
  raise "Undefined base 'klass' or 'new_item' callback for #{self.class}"
94
94
  end
@@ -102,7 +102,7 @@ module Ecoportal
102
102
  []
103
103
  end
104
104
 
105
- super(ini_doc, parent: parent, key: key)
105
+ super(ini_doc, parent: parent, key: key, read_only: read_only)
106
106
  end
107
107
 
108
108
  # @return [Class] the class of the elements of the Collection
@@ -256,7 +256,7 @@ module Ecoportal
256
256
  private
257
257
 
258
258
  def new_item(value)
259
- self.class.new_item(value, parent: self)
259
+ self.class.new_item(value, parent: self, read_only: self._read_only)
260
260
  end
261
261
 
262
262
  # Helper to remove tracked down instance variables
@@ -18,6 +18,9 @@ module Ecoportal
18
18
  end
19
19
  end
20
20
 
21
+ class NoKeyMethod < StandardError
22
+ end
23
+
21
24
  class << self
22
25
  attr_reader :key
23
26
 
@@ -171,7 +174,7 @@ module Ecoportal
171
174
 
172
175
  define_method method do
173
176
  return instance_variable_get(var) if instance_variable_defined?(var)
174
- new_obj = dim_class.new(parent: self, key: method)
177
+ new_obj = dim_class.new(parent: self, key: method, read_only: self._read_only)
175
178
  variable_set(var, new_obj)
176
179
  end
177
180
  end
@@ -194,7 +197,10 @@ module Ecoportal
194
197
  # @param order_matters [Boolean] to state if the order will matter
195
198
  # @param klass [Class, String] the class of the individual elements it embeds
196
199
  # @param enum_class [Class, String] the class of the collection that will hold the individual elements
197
- def embeds_many(method, key: method, order_matters: false, order_key: nil, klass: nil, enum_class: nil)
200
+ # @param read_only [Boolean] whether or not should try to **work around** items `klass` missing a `key`
201
+ # - If set to `true` this is meant only for read purposes (won't be able to successufully insert)
202
+ def embeds_many(method, key: method, klass: nil, enum_class: nil,
203
+ order_matters: false, order_key: nil, read_only: false)
198
204
  if enum_class
199
205
  eclass = enum_class
200
206
  elsif klass
@@ -206,7 +212,7 @@ module Ecoportal
206
212
  else
207
213
  raise "You should either specify the 'klass' of the elements or the 'enum_class'"
208
214
  end
209
- embed(method, key: key, multiple: true, klass: eclass) do |instance_with_called_method|
215
+ embed(method, key: key, multiple: true, klass: eclass, read_only: read_only) do |instance_with_called_method|
210
216
  # keep reference to the original class to resolve the `klass` dependency
211
217
  # See stackoverflow: https://stackoverflow.com/a/73709529/4352306
212
218
  referrer_class = instance_with_called_method.class
@@ -216,7 +222,7 @@ module Ecoportal
216
222
 
217
223
  private
218
224
 
219
- def embed(method, key: method, nullable: false, multiple: false, klass:, &block)
225
+ def embed(method, key: method, nullable: false, multiple: false, klass:, read_only: false, &block)
220
226
  method = method.to_s.freeze
221
227
  var = instance_variable_name(method).freeze
222
228
  k = key.to_s.freeze
@@ -230,9 +236,22 @@ module Ecoportal
230
236
  end
231
237
  return variable_set(var, nil) unless doc[k]
232
238
 
233
- self.class.resolve_class(klass).new(
234
- doc[k], parent: self, key: k
235
- ).tap {|obj| variable_set(var, obj)}
239
+ embedded_class = self.class.resolve_class(klass)
240
+
241
+ if multiple && read_only
242
+ if doc[k].is_a?(Array) && embedded_class < Common::Content::CollectionModel
243
+ if (item_class = embedded_class.klass) && !item_class.key?
244
+ item_class.passkey :id
245
+ doc[k].each_with_index do |item_doc, i|
246
+ item_doc["id"] = "#{i}" unless item_doc.key?("id")
247
+ end
248
+ end
249
+ end
250
+ end
251
+
252
+ embedded_class.new(doc[k], parent: self, key: k, read_only: self._read_only || read_only).tap do |obj|
253
+ variable_set(var, obj)
254
+ end
236
255
  end
237
256
  end
238
257
 
@@ -244,16 +263,17 @@ module Ecoportal
244
263
 
245
264
  inheritable_class_vars :forced_model_keys, :key
246
265
 
247
- attr_reader :_parent, :_key
266
+ attr_reader :_parent, :_key, :_read_only
248
267
 
249
- def initialize(doc = {}, parent: self, key: nil)
250
- @_dim_vars = []
251
- @_parent = parent || self
252
- @_key = key || self
268
+ def initialize(doc = {}, parent: self, key: nil, read_only: false)
269
+ @_dim_vars = []
270
+ @_parent = parent || self
271
+ @_key = key || self
272
+ @_read_only = read_only
253
273
 
254
274
  self.class.enforce!(doc)
255
275
 
256
- if _parent == self
276
+ if (_parent == self) || read_only
257
277
  @doc = doc
258
278
  @original_doc = JSON.parse(@doc.to_json)
259
279
  end
@@ -271,13 +291,13 @@ module Ecoportal
271
291
 
272
292
  # @return [String] the `value` of the `key` method (i.e. `id` value)
273
293
  def key
274
- raise "No key_method defined for #{self.class}" unless key_method?
294
+ raise NoKeyMethod.new "No key_method defined for #{self.class}" unless key_method?
275
295
  self.method(key_method).call
276
296
  end
277
297
 
278
298
  # @param [String] the `value` of the `key` method (i.e. `id` value)
279
299
  def key=(value)
280
- raise "No key_method defined for #{self.class}" unless key_method?
300
+ raise NoKeyMethod.new "No key_method defined for #{self.class}" unless key_method?
281
301
  method = "#{key_method}="
282
302
  self.method(method).call(value)
283
303
  end
@@ -296,6 +316,7 @@ module Ecoportal
296
316
 
297
317
  # @return [nil, Hash] the underlying `Hash` model as is (carrying current changes)
298
318
  def doc
319
+ return @doc if doc_var?
299
320
  raise UnlinkedModel.new(from: "#{self.class}#doc", key: _key) unless linked?
300
321
  if is_root?
301
322
  @doc
@@ -378,8 +399,12 @@ module Ecoportal
378
399
 
379
400
  protected
380
401
 
402
+ def doc_var?
403
+ !!defined?(@doc)
404
+ end
405
+
381
406
  def is_root?
382
- _parent == self && !!defined?(@doc)
407
+ _parent == self && doc_var?
383
408
  end
384
409
 
385
410
  def linked?
@@ -1,5 +1,5 @@
1
1
  module Ecoportal
2
2
  module API
3
- GEM2_VERSION = "0.9.7"
3
+ GEM2_VERSION = "1.0.1"
4
4
  end
5
5
  end
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.9.7
4
+ version: 1.0.1
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-29 00:00:00.000000000 Z
11
+ date: 2023-02-23 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler