ecoportal-api-v2 0.9.7 → 1.1.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: a951381010ef926bce6a42eceb729ed3b8adfe8344ee75161e060e741675f405
4
+ data.tar.gz: 8808bef7b78d1b333de9af88d9e2eac68511213ce0312efadc79d25ef520ff8a
5
5
  SHA512:
6
- metadata.gz: bb9cdb7833b7eae455274cd89348e395c01bd423d8d9591c62b1f66a129b8e3ef9a13f6301870cf75b4707be6306e4ee83299b6bcc9ce05d3224741d44141019
7
- data.tar.gz: 9009e8341f147df8610777d99026f6a2107b2fe64d5e0e0c5fa0b7623d632982899e00728a02c6a737395a6755a40f19a54e805b86ab6648cef51e46e3c3c9a4
6
+ metadata.gz: 75fea111bdc18536084d87d9899a4185a0f8573d3aa0567a355263bb39855880663d5af3f1443c72c6e1d2dd24b1c0420aa5a9777ddd0345209debec4334af9b
7
+ data.tar.gz: 42e2db2fcaca353e334b892267fd01df6702708ff79f3ed44474d79dc26d879532af761c7da78cfcb1c1b1abb313c033e80f7da1d21385f02195a88bd800d47c
data/CHANGELOG.md CHANGED
@@ -1,12 +1,31 @@
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.1.2] - 2023-03-xx
5
5
 
6
6
  ### Added
7
7
  ### Changed
8
8
  ### Fixed
9
9
 
10
+ ## [1.1.1] - 2023-03-09
11
+
12
+ ### Added
13
+ ### Changed
14
+ - upgraded core gem `ecoportal-api`
15
+
16
+ ### Fixed
17
+
18
+ ## [1.0.1] - 2023-02-24
19
+
20
+ ### Added
21
+ - Allowed `read_only` mode to support `emdeds_many` with item class without `method_key`
22
+ - `Ecoportal::API::Common::Content::DoubleModel`
23
+ - `Ecoportal::API::Common::Content::CollectionModel`
24
+ - `Ecoportal::API::Common::Content::ArrayModel`
25
+
26
+ ### Changed
27
+ - It is a risky change (so treated as a **braking change**)
28
+
10
29
  ## [0.9.7] - 2022-11-29
11
30
 
12
31
  ### Added
@@ -29,5 +29,5 @@ Gem::Specification.new do |spec|
29
29
  spec.add_development_dependency "redcarpet", ">= 3.5.1", "< 3.6"
30
30
  spec.add_development_dependency "pry" , ">= 0.14"
31
31
 
32
- spec.add_dependency 'ecoportal-api', '>= 0.8.5', '< 0.9'
32
+ spec.add_dependency 'ecoportal-api', '>= 0.9.1', '< 0.10'
33
33
  end
@@ -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.1.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.1.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-03-09 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -130,20 +130,20 @@ dependencies:
130
130
  requirements:
131
131
  - - ">="
132
132
  - !ruby/object:Gem::Version
133
- version: 0.8.5
133
+ version: 0.9.1
134
134
  - - "<"
135
135
  - !ruby/object:Gem::Version
136
- version: '0.9'
136
+ version: '0.10'
137
137
  type: :runtime
138
138
  prerelease: false
139
139
  version_requirements: !ruby/object:Gem::Requirement
140
140
  requirements:
141
141
  - - ">="
142
142
  - !ruby/object:Gem::Version
143
- version: 0.8.5
143
+ version: 0.9.1
144
144
  - - "<"
145
145
  - !ruby/object:Gem::Version
146
- version: '0.9'
146
+ version: '0.10'
147
147
  description:
148
148
  email:
149
149
  - rien@ecoportal.co.nz