ecoportal-api-v2 0.8.10 → 0.8.14

Sign up to get free protection for your applications and to get access to all the features.
Files changed (32) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +80 -1
  3. data/lib/ecoportal/api/common/content/array_model.rb +10 -17
  4. data/lib/ecoportal/api/common/content/collection_model.rb +23 -6
  5. data/lib/ecoportal/api/common/content/doc_helpers.rb +2 -2
  6. data/lib/ecoportal/api/common/content/double_model.rb +15 -5
  7. data/lib/ecoportal/api/common/content/hash_diff_patch.rb +1 -1
  8. data/lib/ecoportal/api/v2/page/component/action.rb +1 -1
  9. data/lib/ecoportal/api/v2/page/component/action_field.rb +3 -3
  10. data/lib/ecoportal/api/v2/page/component/checklist_field.rb +3 -3
  11. data/lib/ecoportal/api/v2/page/component/file.rb +1 -1
  12. data/lib/ecoportal/api/v2/page/component/files_field.rb +3 -3
  13. data/lib/ecoportal/api/v2/page/component/gauge_field.rb +3 -3
  14. data/lib/ecoportal/api/v2/page/component/selection_field.rb +3 -3
  15. data/lib/ecoportal/api/v2/page/component.rb +17 -0
  16. data/lib/ecoportal/api/v2/page/components.rb +24 -0
  17. data/lib/ecoportal/api/v2/page/force/binding.rb +54 -0
  18. data/lib/ecoportal/api/v2/page/force/bindings.rb +126 -0
  19. data/lib/ecoportal/api/v2/page/force.rb +34 -0
  20. data/lib/ecoportal/api/v2/page/forces.rb +103 -0
  21. data/lib/ecoportal/api/v2/page/permission_flags.rb +0 -2
  22. data/lib/ecoportal/api/v2/page/section.rb +77 -17
  23. data/lib/ecoportal/api/v2/page/sections.rb +36 -6
  24. data/lib/ecoportal/api/v2/page/stage.rb +39 -3
  25. data/lib/ecoportal/api/v2/page/stages.rb +9 -3
  26. data/lib/ecoportal/api/v2/page.rb +46 -1
  27. data/lib/ecoportal/api/v2/pages/page_stage.rb +51 -0
  28. data/lib/ecoportal/api/v2/pages.rb +3 -0
  29. data/lib/ecoportal/api/v2/registers/search_results.rb +13 -0
  30. data/lib/ecoportal/api/v2/registers.rb +15 -2
  31. data/lib/ecoportal/api/v2_version.rb +1 -1
  32. metadata +7 -2
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: e8961e37170e3ffe06d26c17daa6672a15d595ff91c5e8fb38253549b166057a
4
- data.tar.gz: c9b25eb42cfa9cb45cee3d830067eddc7d4191fa87f0fd4e35620e346ca27922
3
+ metadata.gz: 3480ab100855337eef9de50609f4f4776418de1b09a5a1eac3ee31f936f4cd00
4
+ data.tar.gz: 2520bf687a1d00df3d925788600f78c266fcc6599ac816dac1f7df08703bc9e9
5
5
  SHA512:
6
- metadata.gz: a68c58f62600b78745caff84d7e0dbda83e2f79cba8d41ca08ffbcf361060bd614fc6957f38878bb09d41dc6db1fcff3e307dd0072d21d960073683e6881fdbc
7
- data.tar.gz: 998e1409fd0674bc409ab2ff1dcfb68f42a0dd37bca8eab040750cc8cc10ca2651fb98dc61adf3d44b0b0dbdc0bccfe384786cdd79ad1fd9ed96d12a00f3a4b4
6
+ metadata.gz: 011752c7161177026fdcd59a01ee7eb048094eb29c5688e2b68e6ad2aec0a263e93990726c1fca86eb1bfc86d000d360db4d8cf685783f7c2ab8257570258740
7
+ data.tar.gz: 012ed55b7472c56f76b2e1bdafd3ce8cb926c67d5d94e6b3349a7c21ade9ff3a20c2caa4974fe9cdc30a6dba8312663edc633ae7d0a66b31117b57e795727dde
data/CHANGELOG.md CHANGED
@@ -1,7 +1,86 @@
1
1
  # Change Log
2
2
  All notable changes to this project will be documented in this file.
3
3
 
4
- ## [0.8.10] - 2021-08-xx
4
+ ## [0.8.14] - 2021-09-xx
5
+
6
+ ### Added
7
+ - `Ecoportal::API::Common::Content::CollectionModel`
8
+ - `#_doc_pos` for clarity
9
+ - `#include?` to check if an element is present in the collection
10
+ - `Ecoportal::API::V2::Page::Stage#section?` check if section belongs to stage
11
+ - `Ecoportal::API::V2::Page::Sections`
12
+ - `#get_by_id`
13
+ - `#unattached` sections that are not attached to any stage
14
+ - `Ecoportal::API::V2::Page::Section`
15
+ - `#add_component` **added** parameter `before`: to cover case where you want to add it at the beginning
16
+ - `#component?` **widened** parameter type
17
+ - `#stages` stages where this section is attached
18
+ - `attached?` whether or not the section is attached
19
+ - `Ecoportal::API::V2::Page::Component`
20
+ - `#attached?` whether or not the field is attached to some section
21
+ - `#multi_section?` if the field is attached to more than one section :/
22
+ - **added** support for `forces`
23
+ - `Ecoportal::API::V2::Page::Force`
24
+ - `Ecoportal::API::V2::Page::Forces`
25
+ - `Ecoportal::API::V2::Page::Force::Binding`
26
+ - `Ecoportal::API::V2::Page::Force::Bindings`
27
+ - **added** shortcut to `#ooze` throughout all the model
28
+ - **validations** in some methods
29
+ - `Ecoportal::API::V2::Page::Stage#add_section`: section should be in `ooze.sections`
30
+ - `Ecoportal::API::V2::Page::Section#add_component`: component should be in `ooze.components`
31
+ - `Ecoportal::API::V2::Page::Force::Bindings#add`:
32
+ - section to be in `ooze.sections`
33
+ - component to be in `ooze.components`
34
+ - `Ecoportal::API::V2::Pages::PageStage#as_update` on page instances, it checks that
35
+ - Fields do not belong to more than one section
36
+ - Fields belong at least to one section
37
+ - Sections belong to at least one stage
38
+
39
+ ### Changed
40
+ ### Fixed
41
+ - `Ecoportal::API::Common::Content::ArrayModel#insert_one` was not inserting when not found. Should insert at least at the end.
42
+
43
+ ## [0.8.13] - 2021-09-03
44
+
45
+ ### Fixed
46
+ - `Ecoportal::API::V2::Page::Sections`
47
+ - `weight` fixing should only happen on entire page, **not** on stage sections that could change the order of section shared with other stages
48
+ - `#scope_weight` was not excluding the section when using `weight` of the last one
49
+ - Several classes with `embeds_many` were performing the `ordering` of those elements in the wrong way
50
+ - `Ecoportall::API::Common::Content::DoubleModel`
51
+ - `#replace_original_doc` had a couple of typos
52
+ - `#replace_doc` should only remove the variable referring to the current object and only if `new_doc` is `nil`
53
+ - `Ecoportal::API::Common::Content::CollectionModel` on insertion and deletion it was removing instance variable objects.
54
+ - There was no need for this approach, as the only necessary thing is to just keep those variable instances up to date in the correct unique access point to do so.
55
+ - Aside note: the reason why this was initially designed this way is because there were some difficulties to identify the existing elements in the subjacent model. Once `#passkey` as added, this difficulty could be overcome.
56
+ - `Ecoportal::API::Common::Content::ArrayModel` on initialize, default `doc` should be an `Array`
57
+
58
+ ## [0.8.12] - 2021-08-30
59
+
60
+ ### Added
61
+ - `Ecoportal::API::V2::Pages::PageStage#current_stage`
62
+
63
+ ### Fixed
64
+ - `Ecoportal::API::Common::Content::DocHelpers#get_body` typo and wrong parameters on call.
65
+ - `Ecoportal::API::V2::Pages#update` should always return `Result` object.
66
+ - `Ecoportal::API::V2::Registers#search` was not using `cursor_id` to iterate.
67
+
68
+ ## [0.8.11] - 2021-08-27
69
+
70
+ ### Added
71
+ - `Ecoportal::API::V2::Page#stages?`
72
+ - `Ecoportal::API::V2::Pages::PageStage#current_stage_id`
73
+ - `Ecoportal::API::V2::Registers#search` added option keyed parameter `:only_first`
74
+ - which will make the method return the first `Ecoportal::API::V2::Registers::SearchResults`
75
+ - This has been **added** with the aim of knowing how many results out of how many entries.
76
+
77
+ ### Changed
78
+ - `Ecoportal::API::V2::Pages::PageStage#update` won't update unless there's something to update.
79
+
80
+ ### Fixed
81
+
82
+
83
+ ## [0.8.10] - 2021-08-22
5
84
 
6
85
  ### Added
7
86
  - `Ecoportal::API::Common::Content::CollectionModel#[]` now supports position as well
@@ -33,7 +33,7 @@ module Ecoportal
33
33
 
34
34
  end
35
35
 
36
- def initialize(doc = {}, parent: self, key: nil)
36
+ def initialize(doc = [], parent: self, key: nil)
37
37
  super(doc, parent: parent, key: key)
38
38
  end
39
39
 
@@ -224,25 +224,18 @@ module Ecoportal
224
224
  i = index(value)
225
225
  return i if (i && uniq?)
226
226
  pos = case
227
- when used_param?(pos)
228
- pos || length
229
- when used_param?(before)
230
- before ? index(before) : length
231
- when used_param?(after)
232
- if after
233
- if i = index(after) then i + 1 end
234
- else
235
- length
236
- end
237
- else
238
- length
227
+ when used_param?(pos) && pos
228
+ pos
229
+ when used_param?(before) && before
230
+ index(before)
231
+ when used_param?(after) && after
232
+ if i = index(after) then i + 1 end
239
233
  end
240
234
 
235
+ pos ||= length
241
236
  pos.tap do |i|
242
- unless !i
243
- _items.insert(pos, value)
244
- on_change
245
- end
237
+ _items.insert(pos, value)
238
+ on_change
246
239
  end
247
240
  end
248
241
 
@@ -109,7 +109,14 @@ module Ecoportal
109
109
  self.class.klass
110
110
  end
111
111
 
112
+ def _doc_pos(value)
113
+ _doc_key(value)
114
+ end
115
+
112
116
  # Transforms `value` into the actual `key` to access the object in the doc `Array`
117
+ # @note
118
+ # - The name of the method is after the paren't class method
119
+ # - This method would have been better called `_doc_pos` :)
113
120
  def _doc_key(value)
114
121
  #print "*(#{value.class})"
115
122
  return super(value) unless value.is_a?(Hash) || value.is_a?(Content::DoubleModel)
@@ -149,6 +156,13 @@ module Ecoportal
149
156
  items_by_key[get_key(value)]
150
157
  end
151
158
 
159
+ # Checks if an element exists in the collection
160
+ # @param value [String, Hash, Ecoportal::API::Common::Content::DoubleModel]
161
+ # @return [Boolean] whether or not it is included
162
+ def include?(value)
163
+ items_by_key.key?(get_key(value))
164
+ end
165
+
152
166
  # @return [Array<Object>] the `items_class` element object
153
167
  def values_at(*keys)
154
168
  keys.map {|key| self[key]}
@@ -167,7 +181,10 @@ module Ecoportal
167
181
  if item = self[value]
168
182
  item.replace_doc(item_doc)
169
183
  else
170
- pos_idx = _doc_upsert(item_doc, pos: pos, before: before, after: after)
184
+ _doc_upsert(item_doc, pos: pos, before: before, after: after).tap do |pos_idx|
185
+ _items.insert(pos_idx, new_item(item_doc))
186
+ @indexed = false
187
+ end
171
188
  end
172
189
  (item || self[item_doc]).tap do |item|
173
190
  yield(item) if block_given?
@@ -185,6 +202,8 @@ module Ecoportal
185
202
  end
186
203
  if item = self[value]
187
204
  _doc_delete(item.doc)
205
+ @indexed = false
206
+ _items.delete(item)
188
207
  end
189
208
  end
190
209
 
@@ -195,7 +214,8 @@ module Ecoportal
195
214
  def items_key; self.class.items_key; end
196
215
 
197
216
  def on_change
198
- variables_remove!
217
+ @indexed = false
218
+ #variables_remove!
199
219
  end
200
220
 
201
221
  # Gets the `key` of the object `value`
@@ -253,9 +273,7 @@ module Ecoportal
253
273
  # @return [Object] the element deleted from `doc`
254
274
  def _doc_delete(value)
255
275
  if current_pos = _doc_key(value)
256
- _doc_items.delete_at(current_pos).tap do |deleted|
257
- on_change
258
- end
276
+ _doc_items.delete_at(current_pos)
259
277
  end
260
278
  end
261
279
 
@@ -275,7 +293,6 @@ module Ecoportal
275
293
  pos = (pos && pos < _doc_items.length)? pos : _doc_items.length
276
294
  pos.tap do |i|
277
295
  _doc_items.insert(pos, value)
278
- on_change
279
296
  end
280
297
 
281
298
  end
@@ -13,9 +13,9 @@ module Ecoportal
13
13
  when doc.respond_to?(:as_update)
14
14
  doc.as_update
15
15
  when doc.respond_to?(:as_json)
16
- Common::Content::HashPatchDiff.patch_diff(doc.as_json)
16
+ Common::Content::HashDiffPatch.patch_diff(doc.as_json, nil)
17
17
  when doc.is_a?(Hash)
18
- Common::Content::HashPatchDiff.patch_diff(doc)
18
+ Common::Content::HashDiffPatch.patch_diff(doc, nil)
19
19
  else
20
20
  raise "Could not get body for doc: #{doc}"
21
21
  end
@@ -240,7 +240,7 @@ module Ecoportal
240
240
  end
241
241
 
242
242
  inheritable_class_vars :forced_model_keys
243
-
243
+
244
244
  attr_reader :_parent, :_key
245
245
 
246
246
  def initialize(doc = {}, parent: self, key: nil)
@@ -367,8 +367,8 @@ module Ecoportal
367
367
  @doc = new_doc
368
368
  else
369
369
  dig_set(_parent.doc, [_doc_key(_key)].flatten, new_doc)
370
- _parent.variable_remove!(_key)
371
- variables_remove!
370
+ _parent.variable_remove!(_key) unless new_doc
371
+ #variables_remove!
372
372
  end
373
373
  end
374
374
 
@@ -385,9 +385,9 @@ module Ecoportal
385
385
  def replace_original_doc(new_doc)
386
386
  raise UnlinkedModel.new(from: "#{self.class}#replace_original_doc", key: _key) unless linked?
387
387
  if is_root?
388
- @orginal_doc = new_doc
388
+ @original_doc = new_doc
389
389
  else
390
- dig_set(_parent.orginal_doc, [_doc_key(_key)].flatten, new_doc)
390
+ dig_set(_parent.original_doc, [_doc_key(_key)].flatten, new_doc)
391
391
  end
392
392
  end
393
393
 
@@ -409,11 +409,21 @@ module Ecoportal
409
409
 
410
410
  # Removes all the persistent variables
411
411
  def variables_remove!
412
+ #puts "going to remove vars: #{@_dim_vars} on #{self.class} (parent: #{identify_parent(self._parent)})"
412
413
  @_dim_vars.dup.map {|k| variable_remove!(k)}
413
414
  end
414
415
 
415
416
  private
416
417
 
418
+ def identify_parent(object)
419
+ case object
420
+ when Ecoportal::API::V2::Page::Stage
421
+ "stage #{object.name}"
422
+ when Ecoportal::API::V2::Page::Section
423
+ "section #{object.heading}"
424
+ end
425
+ end
426
+
417
427
  def instance_variable_name(key)
418
428
  self.class.instance_variable_name(key)
419
429
  end
@@ -60,7 +60,7 @@ module Ecoportal
60
60
  a == b
61
61
  end
62
62
 
63
- # Compares `a` as charring changes of `b`
63
+ # Compares `a` as carrying changes of `b`
64
64
  # @return [Hash] patch data object with only changes
65
65
  def patch_data(a, b = nil, delete: false)
66
66
  {}.tap do |data_hash|
@@ -8,7 +8,7 @@ module Ecoportal
8
8
  def new_doc
9
9
  {
10
10
  "id" => new_uuid,
11
- "weight" => 9999
11
+ "weight" => 99
12
12
  }
13
13
  end
14
14
  end
@@ -25,9 +25,9 @@ module Ecoportal
25
25
  end
26
26
 
27
27
  def ordered_tasks
28
- actions.each_with_index.sort_by do |task, index|
29
- (task.weight >= 9999) ? [index, index] : [task.weight, index]
30
- end.map(&:first)
28
+ actions.sort_by.with_index do |task, index|
29
+ [task.weight, index]
30
+ end
31
31
  end
32
32
 
33
33
  # Quick config helper
@@ -19,9 +19,9 @@ module Ecoportal
19
19
  end
20
20
 
21
21
  def ordered_items
22
- items.each_with_index.sort_by do |item, index|
23
- (item.weight >= 9999) ? [index, index] : [item.weight, index]
24
- end.map(&:first)
22
+ items.sort_by.with_index do |item, index|
23
+ [item.weight, index]
24
+ end
25
25
  end
26
26
 
27
27
  private
@@ -8,7 +8,7 @@ module Ecoportal
8
8
  def new_doc
9
9
  {
10
10
  "id" => new_uuid,
11
- "position" => 9999
11
+ "position" => 99
12
12
  }
13
13
  end
14
14
  end
@@ -19,9 +19,9 @@ module Ecoportal
19
19
  end
20
20
 
21
21
  def ordered_files
22
- items.each_with_index.sort_by do |file, index|
23
- (file.position >= 9999) ? [index, index] : [file.position, index]
24
- end.map(&:first)
22
+ items.sort_by.with_index do |file, index|
23
+ [file.position, index]
24
+ end
25
25
  end
26
26
 
27
27
  private
@@ -21,9 +21,9 @@ module Ecoportal
21
21
  end
22
22
 
23
23
  def ordered_stops
24
- stops.each_with_index.sort_by do |stop, index|
25
- (stop.threshold >= 9999) ? [index, index] : [stop.threshold, index]
26
- end.map(&:first)
24
+ stops.sort_by.with_index do |stop, index|
25
+ [stop.threshold, index]
26
+ end
27
27
  end
28
28
 
29
29
  end
@@ -58,9 +58,9 @@ module Ecoportal
58
58
  end
59
59
 
60
60
  def ordered_options
61
- options.each_with_index.sort_by do |option, index|
62
- (option.weight >= 9999) ? [index, index] : [option.weight, index]
63
- end.map(&:first)
61
+ options.sort_by.with_index do |option, index|
62
+ [option.weight, index]
63
+ end
64
64
  end
65
65
 
66
66
  # Quick config helper
@@ -93,6 +93,10 @@ module Ecoportal
93
93
  passboolean :hide_view, :hidden_on_reports, :hidden_on_mobile
94
94
  passarray :refs
95
95
 
96
+ def ooze
97
+ self._parent.ooze
98
+ end
99
+
96
100
  def ref_backend
97
101
  refs.first
98
102
  end
@@ -103,10 +107,23 @@ module Ecoportal
103
107
  end
104
108
  end
105
109
 
110
+ # Looks up the section that this component belongs to.
111
+ # @return [Ecoportal::API::V2::Page::Section, nil] the section where this field is attached to.
106
112
  def section
107
113
  root.sections.find {|sec| sec.component?(id)}
108
114
  end
109
115
 
116
+ # @return [Boolean] whether or not this field is attached to any section.
117
+ def attached?
118
+ !!section
119
+ end
120
+
121
+ # @return [Boolean] whether or not the component has been attached to more than one section.
122
+ def multi_section?
123
+ secs = ooze.sections.select {|sec| sec.component?(id)}
124
+ secs.length > 1
125
+ end
126
+
110
127
  def indexable_label
111
128
  self.class.indexable_label(label)
112
129
  end
@@ -13,18 +13,27 @@ module Ecoportal
13
13
 
14
14
  order_matters = true
15
15
 
16
+ def ooze
17
+ self._parent.ooze
18
+ end
19
+
20
+ # @return [Ecoportal::API::V2::Page::Component] the field with `id`
16
21
  def get_by_id(id)
17
22
  self.find do |comp|
18
23
  comp.id == id
19
24
  end
20
25
  end
21
26
 
27
+ # @return [Array<Ecoportal::API::V2::Page::Component>] the fields of that `type`
22
28
  def get_by_type(type)
23
29
  self.select do |comp|
24
30
  comp.type.downcase == type.to_s.strip.downcase
25
31
  end
26
32
  end
27
33
 
34
+ # @param name [String, Regexp] the `name` to search the field based on their `label`
35
+ # @param type [String] the `type` of the fields to be included
36
+ # @return [Array<Ecoportal::API::V2::Page::Component>] the fields that match `name`.
28
37
  def get_by_name(name, type: nil)
29
38
  pool = type ? get_by_type(type) : self
30
39
  pool.select do |comp|
@@ -32,6 +41,12 @@ module Ecoportal
32
41
  end
33
42
  end
34
43
 
44
+ # It creates a **new** component
45
+ # @param label [String, nil]
46
+ # @param type [String] the type of the field
47
+ # @yield [field] do some stuff with field
48
+ # @yieldparam [Ecoportal::API::V2::Page::Component] the created field
49
+ # @return [Ecoportal::API::V2::Page::Component] the created field.
35
50
  def add(label:, type:)
36
51
  fld_doc = component_class.new_doc(type: type)
37
52
  upsert!(fld_doc) do |fld|
@@ -40,6 +55,15 @@ module Ecoportal
40
55
  end
41
56
  end
42
57
 
58
+ # @return [Array<Ecoportal::API::V2::Page::Component>] **orphaned** fields (with no section).
59
+ def unattached
60
+ select {|comp| !comp.attached?}
61
+ end
62
+
63
+ # @return [Array<Ecoportal::API::V2::Page::Component>] fields belonging to more than one section.
64
+ def multi_section
65
+ select {|comp| comp.multi_section?}
66
+ end
43
67
  end
44
68
  end
45
69
  end
@@ -0,0 +1,54 @@
1
+ module Ecoportal
2
+ module API
3
+ class V2
4
+ class Page
5
+ class Force
6
+ class Binding < Common::Content::DoubleModel
7
+ class << self
8
+ def new_doc
9
+ {
10
+ "id" => new_uuid
11
+ }
12
+ end
13
+ end
14
+ COMPONENT_TYPE = "membrane_binding"
15
+ SECTION_TYPE = "flow_node_binding"
16
+
17
+ passkey :id
18
+ passforced :patch_ver, default: 1
19
+ passthrough :name, :reference_id, :type
20
+
21
+ def ooze
22
+ self._parent.ooze
23
+ end
24
+
25
+ # @return [Boolean] whether or not this the binding refers to a Component
26
+ def component?
27
+ type == COMPONENT_TYPE
28
+ end
29
+
30
+ # @return [Boolean] whether or not this the binding refers to a Section
31
+ def section?
32
+ type == SECTION_TYPE
33
+ end
34
+
35
+ # @return [Ecoportal::API::V2::Page::Component] field referenced by this binding.
36
+ def component
37
+ raise "This is not a component binding, but a section binding" unless component?
38
+ ooze.components.get_by_id(reference_id)
39
+ end
40
+
41
+ # @return [Ecoportal::API::V2::Page::Section] section referenced by this binding.
42
+ def section
43
+ raise "This is not a section binding, but a component binding" unless section?
44
+ ooze.sections.get_by_id(reference_id)
45
+ end
46
+ end
47
+ end
48
+ end
49
+ end
50
+ end
51
+ end
52
+
53
+ require 'ecoportal/api/v2/page/force/binding'
54
+ require 'ecoportal/api/v2/page/force/bindings'
@@ -0,0 +1,126 @@
1
+ module Ecoportal
2
+ module API
3
+ class V2
4
+ class Page
5
+ class Force
6
+ class Bindings < Common::Content::CollectionModel
7
+ class_resolver :binding_class, "Ecoportal::API::V2::Page::Force::Binding"
8
+
9
+ self.klass = :binding_class
10
+ order_matters = true
11
+
12
+ def ooze
13
+ self._parent.ooze
14
+ end
15
+
16
+ # @param id [String] the `id` of the binding to find.
17
+ # @return [Ecoportal::API::V2::Page::Force::Binding] binding with `id`
18
+ def get_by_id(id)
19
+ self.find do |bind|
20
+ bind.id == id
21
+ end
22
+ end
23
+
24
+ # @param type [String] target `type` of binding
25
+ # 1. Ecoportal::API::V2::Page::Force::Binding::COMPONENT_TYPE
26
+ # 2. Ecoportal::API::V2::Page::Force::Binding::SECTION_TYPE
27
+ # @return [Array<Ecoportal::API::V2::Page::Force::Binding>] the bindings of type `type`
28
+ def get_by_type(type = Ecoportal::API::V2::Page::Force::Binding::COMPONENT_TYPE)
29
+ self.select do |bind|
30
+ bind.type.downcase == type.to_s.strip.downcase
31
+ end
32
+ end
33
+
34
+ # @param type [String] target `type` of binding
35
+ # 1. Ecoportal::API::V2::Page::Force::Binding::COMPONENT_TYPE
36
+ # 2. Ecoportal::API::V2::Page::Force::Binding::SECTION_TYPE
37
+ # @return [Array<Ecoportal::API::V2::Page::Force::Binding>] the bindings matching `name`
38
+ def get_by_name(name, type: nil)
39
+ pool = type ? get_by_type(type) : self
40
+ pool.select do |bind|
41
+ same_string?(bind.name, name)
42
+ end
43
+ end
44
+
45
+ # Creates a new `binding`
46
+ # @note
47
+ # - As there's no position property, it will upsert to the array of bindings
48
+ # @raise [ArgumentError] when `reference` is neither of Component or Section
49
+ # @raise [Exception] when
50
+ # 1. `reference` is a field missing in ooze.components
51
+ # 2. `reference` is a section missing in ooze.sections
52
+ # @param reference [Ecoportal::API::V2::Page::Component, Ecoportal::API::V2::Page::Section]
53
+ # The field or the section to bind.
54
+ # @yield [binding] do some stuff with binding
55
+ # @yieldparam binding [Ecoportal::API::V2::Page::Force::Binding] the created binding
56
+ # @return [Ecoportal::API::V2::Page::Force::Binding] the created binding.
57
+ def add(reference, name:, pos: NOT_USED, before: NOT_USED, after: NOT_USED)
58
+ binding_doc = binding_class.new_doc
59
+
60
+ type = case reference
61
+ when Ecoportal::API::V2::Page::Component
62
+ unless ooze.components.include?(reference)
63
+ msg = "The field '#{reference.label}' (#{reference.id}) is not present in ooze.components.\n"
64
+ msg += "Review your script (i.e. @var where you store previous ooze runs)."
65
+ raise msg
66
+ end
67
+ Ecoportal::API::V2::Page::Force::Binding::COMPONENT_TYPE
68
+ when Ecoportal::API::V2::Page::Section
69
+ unless ooze.sections.include?(reference)
70
+ msg = "The section '#{reference.heading}' (#{reference.id}) is not present in ooze.sections.\n"
71
+ msg += "Review your script (i.e. @var where you store previous ooze runs)."
72
+ raise msg
73
+ end
74
+ Ecoportal::API::V2::Page::Force::Binding::SECTION_TYPE
75
+ else
76
+ msg = "You can only create bindings with Component and Section. Given: #{reference.class}"
77
+ raise ArgumentError.new(msg)
78
+ end
79
+
80
+ position = scope_position(pos: pos, before: before, after: after)
81
+ upsert!(binding_doc, pos: position) do |bind|
82
+ bind.name = name
83
+ bind.reference_id = reference.id
84
+ bind.type = type
85
+ yield(bind) if block_given?
86
+ end
87
+ end
88
+
89
+ private
90
+
91
+ def scope_position(pos: NOT_USED, before: NOT_USED, after: NOT_USED)
92
+ case
93
+ when used_param?(pos)
94
+ if pos = to_binding(pos)
95
+ _doc_pos(pos) - 1
96
+ end
97
+ when used_param?(before)
98
+ if before = to_binding(before)
99
+ _doc_pos(before) - 1
100
+ end
101
+ when used_param?(after)
102
+ if after = to_binding(after)
103
+ _doc_pos(after) - 1
104
+ end
105
+ end.yield_self do |position|
106
+ position ||= self.count
107
+ end
108
+ end
109
+
110
+ def to_binding(value)
111
+ case value
112
+ when Ecoportal::API::V2::Page::Force::Binding
113
+ value
114
+ when Numeric
115
+ self[value]
116
+ else
117
+ get_by_name(value).first
118
+ end
119
+ end
120
+
121
+ end
122
+ end
123
+ end
124
+ end
125
+ end
126
+ end