ecoportal-api-v2 0.9.6 → 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: 9502632e0f39a86587a093fb54bcadcd42b7a3db38ed98d5ec35ea5a56a9b257
4
- data.tar.gz: 393ff91c773c047ffa031350a88af80f395847f326ec4b13011f33dc2477c6b5
3
+ metadata.gz: d8f33b34cbba08724022856ecf54859aa40fed40bbeb2d06972edb97a0f5a171
4
+ data.tar.gz: bb53bb8e3e882b93aed2bb6f6a6f43df1c9405e5b4a6ac18b86d99ab52b28be8
5
5
  SHA512:
6
- metadata.gz: c7733b21b7d7dec324c7561cb495e83ffc39f6e567eb5332613590ca914f611ec8361d6a356506f4f6eb846f0cbc5f7361e6235e9c1eb0b992ef7010dff0d093
7
- data.tar.gz: 80d8a90e09c22ddd80854fa4d38ab6b7b6c0d533db3f694059818f20573c9a2f023888f3959fa8c67db8a37a6bbb9597a1d66fda0485e418c4197fcd1788d9e3
6
+ metadata.gz: 6bf8ffdc02fb8471a833e3451abd89bb09b573f6a711cbc99eec189d528bd7c90643a0a33d86ae89c08fa449c65c3bdf0a1c1353b33afe049ffa62a3dac5a6ac
7
+ data.tar.gz: 8444d1a3c41222e186aeffbf15158b8048187508a66d225413aab38d1a07f61a0a14508d1259eed7b131c2806cdac537814b057a017b3aad8d21514a515244c1
data/CHANGELOG.md CHANGED
@@ -1,10 +1,33 @@
1
1
  # Change Log
2
2
  All notable changes to this project will be documented in this file.
3
3
 
4
- ## [0.9.7] - 2022-11-xx
4
+ ## [1.0.1] - 2023-02-xx
5
5
 
6
6
  ### Added
7
7
  ### Changed
8
+ ### Fixed
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
+
21
+ ## [0.9.7] - 2022-11-29
22
+
23
+ ### Added
24
+ - `Ecoportal::API::V2::Page::Component::ReferenceField` added methods `#delete` and `#clear`
25
+
26
+ ### Changed
27
+ - `Ecoportal::API::V2::Page::Component::ReferenceField#add`
28
+ - be able to add multiple references
29
+ - prevent double-ups, just in case the server doesn't handle this
30
+
8
31
  ### Fixed
9
32
  - `Ecoportal::API::V2::Page::Component#bindings`
10
33
 
@@ -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
@@ -175,7 +175,7 @@ module Ecoportal
175
175
  # @return [Object] the `items_class` element object
176
176
  def upsert!(value, pos: NOT_USED, before: NOT_USED, after: NOT_USED)
177
177
  unless value.is_a?(Hash) || value.is_a?(Content::DoubleModel)
178
- raise "'Content::DoubleModel' or 'Hash' doc required"
178
+ raise "'Content::DoubleModel' or 'Hash' doc required. Given #{value.class}"
179
179
  end
180
180
  item_doc = value.is_a?(Content::DoubleModel)? value.doc : value
181
181
  item_doc = JSON.parse(item_doc.to_json)
@@ -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?
@@ -13,19 +13,36 @@ module Ecoportal
13
13
  def empty?
14
14
  reference_ids.empty?
15
15
  end
16
-
16
+
17
17
  def reference_ids
18
18
  references.map do |ref|
19
19
  ref["id"]
20
20
  end
21
21
  end
22
22
 
23
- def add(ref_id)
24
- doc["references"].push({
25
- "id" => ref_id,
26
- "weight" => 0,
27
- "patch_ver" => 0
28
- })
23
+ def add(*ref_ids)
24
+ doc["references"].tap do |refs|
25
+ ref_ids.each do |ref_id|
26
+ next if reference_ids.include?(ref_id)
27
+ refs.push({
28
+ "id" => ref_id,
29
+ "weight" => 0,
30
+ "patch_ver" => 0
31
+ })
32
+ end
33
+ end
34
+ end
35
+
36
+ def clear
37
+ delete(*reference_ids)
38
+ end
39
+
40
+ def delete(*ref_ids)
41
+ ref_ids.each do |ref_id|
42
+ if doc_ref = doc["references"].find {|doc_ref| doc_ref["id"] == ref_id}
43
+ doc["references"].delete(doc_ref)
44
+ end
45
+ end
29
46
  end
30
47
 
31
48
  def to_s(delimiter: "\n")
@@ -1,5 +1,5 @@
1
1
  module Ecoportal
2
2
  module API
3
- GEM2_VERSION = "0.9.6"
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.6
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-18 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