ecoportal-api-oozes 0.5.7 → 0.5.8

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: 114564bea6323ae47cf4f585a65a3a4aac2d3b77f9299ed38e8873b3bd914736
4
- data.tar.gz: 502053295e4b920ac85046a4d45277e7df61b67f173ffa1ba65fef18ba0b771b
3
+ metadata.gz: 22485d9290fed472c85b2871b538385efa432395714b2fa9f2395d5ca3dfc73e
4
+ data.tar.gz: 6d2c0590daac42d7ad69dc117de7fb188e8ed5eff480a38dfc7ec00759c7f639
5
5
  SHA512:
6
- metadata.gz: 51aa602b82e3b6ae577b5a4b5b46835a3a69c9f2d694f8dbb33566247605f639cde5ceb5a8ca02ad342750389b3cd2ac08cc1834117252f0e52d0d3d78981084
7
- data.tar.gz: 1a19a58d76067444c10c4ae62539617c7674fa701432b4893dae723092008ad65a0462179d56e9ea84e11d72f6828a5817ea7f92055d7dafb2c48170518ef955
6
+ metadata.gz: ee03ed1ac3f843fcf4dfdb38fccde9389fa909cb8fd3f9285f1c22c9a308d995f75ba8e329945999e1fcb47c61c72e1d4c199594daa63b00fc5c19e5477e6777
7
+ data.tar.gz: d3e35673499d33c8322afd2eda3d1dc43f70dce238650b8acd192830e41f9f8fd65a8f5df89c727b8a933967e13a53c824ac80a40660912ad675405b46f48cad
@@ -30,13 +30,6 @@ module Ecoportal
30
30
  (a.order_matters? == b.order_matters?) && (a.uniq? == b.uniq?)
31
31
  end
32
32
 
33
- # Creates a new object with `doc` where `original_doc` is `[]`
34
- def create(doc)
35
- self.new([]).tap do |page|
36
- page.replace_doc(doc)
37
- end
38
- end
39
-
40
33
  end
41
34
 
42
35
  def initialize(doc = {}, parent: self, key: nil)
@@ -226,7 +219,6 @@ module Ecoportal
226
219
  end
227
220
  end
228
221
 
229
- # TODO
230
222
  def insert_one(value, pos: NOT_USED, before: NOT_USED, after: NOT_USED)
231
223
  i = index(value)
232
224
  return i if (i && uniq?)
@@ -80,13 +80,6 @@ module Ecoportal
80
80
  end
81
81
  end
82
82
 
83
- # Creates a new object with `doc` where `original_doc` is `[]`
84
- def create(doc)
85
- self.new([]).tap do |page|
86
- page.replace_doc(doc)
87
- end
88
- end
89
-
90
83
  end
91
84
 
92
85
  include Enumerable
@@ -152,18 +145,20 @@ module Ecoportal
152
145
  # Tries to find the element `value`, if it exists, it updates it
153
146
  # Otherwise it pushes it to the end
154
147
  # @return the element
155
- def upsert!(value)
148
+ def upsert!(value, pos: NOT_USED, before: NOT_USED, after: NOT_USED)
156
149
  unless value.is_a?(Hash) || value.is_a?(Content::DoubleModel)
157
150
  raise "'Content::DoubleModel' or 'Hash' doc required"
158
151
  end
159
152
  item_doc = value.is_a?(Content::DoubleModel)? value.doc : value
153
+ item_doc = JSON.parse(item_doc.to_json)
160
154
  if item = self[value]
161
- item.replace_doc(JSON.parse(item_doc.to_json))
155
+ item.replace_doc(item_doc)
162
156
  else
163
- item = new_item(item_doc)
164
- _doc_items << item.doc
157
+ _doc_upsert(item_doc, pos: pos, before: before, after: after)
158
+ end
159
+ (item || self[item_doc]).tap do |item|
160
+ yield(item) if block_given?
165
161
  end
166
- item
167
162
  end
168
163
 
169
164
  protected
@@ -172,6 +167,10 @@ module Ecoportal
172
167
  def uniq?; self.class.uniq; end
173
168
  def items_key; self.class.items_key; end
174
169
 
170
+ def on_change
171
+ variables_remove!
172
+ end
173
+
175
174
  # Gets the `key` of the object
176
175
  def get_key(value)
177
176
  case value
@@ -205,11 +204,9 @@ module Ecoportal
205
204
  self.class.new_item(value, parent: self)
206
205
  end
207
206
 
208
- private
209
-
210
207
  # Helper to remove tracked down instance variables
211
208
  def variable_remove!(key)
212
- if (k = get_key(key)) && (item = @items_by_key[k])
209
+ if @items_by_key && (k = get_key(key)) && (item = @items_by_key[k])
213
210
  _items.delete(item) if _items.include?(item)
214
211
  @items_by_key.delete(k)
215
212
  else
@@ -223,6 +220,36 @@ module Ecoportal
223
220
  super
224
221
  end
225
222
 
223
+ def _doc_upsert(value, pos: NOT_USED, before: NOT_USED, after: NOT_USED)
224
+ current_pos = _doc_key(value)
225
+ pos = case
226
+ when used_param?(pos)
227
+ pos
228
+ when used_param?(before)
229
+ _doc_key(before)
230
+ when used_param?(after)
231
+ puts "to add after #{after.id}"
232
+ if i = _doc_key(after)
233
+ i + 1
234
+ end
235
+ end
236
+
237
+ pos ||= current_pos
238
+
239
+ if current_pos && pos
240
+ _doc_items.delete(current_pos)
241
+ pos = (pos <= current_pos)? pos : pos - 1
242
+ end
243
+
244
+ pos = (pos && pos < _doc_items.length)? pos : _doc_items.length
245
+
246
+ pos.tap do |i|
247
+ _doc_items.insert(pos, value)
248
+ on_change
249
+ end
250
+
251
+ end
252
+
226
253
  end
227
254
  end
228
255
  end
@@ -1,3 +1,5 @@
1
+ require 'securerandom'
2
+
1
3
  module Ecoportal
2
4
  module API
3
5
  module Common
@@ -28,6 +30,10 @@ module Ecoportal
28
30
  @key = value.to_s.freeze
29
31
  end
30
32
 
33
+ def new_uuid(length: 12)
34
+ SecureRandom.hex(length)
35
+ end
36
+
31
37
  # Same as `attr_reader` but links to a subjacent `Hash` model property
32
38
  # @note it does **not** create an _instance variable_
33
39
  def pass_reader(*methods)
@@ -154,13 +160,6 @@ module Ecoportal
154
160
  embeds_one(method, key: key, multiple: true, klass: dim_class)
155
161
  end
156
162
 
157
- # Creates a new object with `doc` where `original_doc` is `{}`
158
- def create(doc)
159
- self.new({}).tap do |page|
160
- page.replace_doc(doc)
161
- end
162
- end
163
-
164
163
  end
165
164
 
166
165
  attr_reader :_parent, :_key
@@ -306,7 +305,7 @@ module Ecoportal
306
305
 
307
306
  # Removes all the persistent variables
308
307
  def variables_remove!
309
- @_dim_vars.map {|k| variable_remove!(k)}
308
+ @_dim_vars.dup.map {|k| variable_remove!(k)}
310
309
  end
311
310
 
312
311
  private
@@ -108,10 +108,15 @@ module Ecoportal
108
108
  end
109
109
 
110
110
  def patch_data_array(a, b)
111
+ original_b = b
111
112
  a ||= []; b ||= []
112
113
  if !nested_array?(a, b)
113
114
  if a.length == b.length && (a & b).length == b.length
114
- NO_CHANGES
115
+ if original_b
116
+ NO_CHANGES
117
+ else
118
+ a
119
+ end
115
120
  else
116
121
  a
117
122
  end
@@ -23,6 +23,13 @@ module Ecoportal
23
23
  class_resolver :law_field_class, "Ecoportal::API::V2::Page::Component::LawField"
24
24
 
25
25
  class << self
26
+ def new_doc(type:)
27
+ {
28
+ "id" => new_uuid,
29
+ "type" => type
30
+ }
31
+ end
32
+
26
33
  def get_class(doc)
27
34
  if doc.is_a?(Hash)
28
35
  case doc["type"]
@@ -33,6 +33,42 @@ module Ecoportal
33
33
  end
34
34
  end
35
35
 
36
+ # a server bug prevents to add new options to an existing field
37
+ def add_option(name:, value:, pos: NOT_USED, before: NOT_USED, after: NOT_USED)
38
+ opt_doc = Ecoportal::API::V2::Page::Component::SelectionOption.new_doc
39
+ options.upsert!(opt_doc, pos: pos, before: before, after: after) do |option|
40
+ option.name = name
41
+ option.value = value
42
+ if prev = previous_option(option)
43
+ option.weight = prev.weight
44
+ end
45
+ yield(option) if block_given?
46
+ fix_option_weights!
47
+ end
48
+ end
49
+
50
+ def ordered_options
51
+ options.each_with_index.sort_by do |option, index|
52
+ (option.weight >= 9999) ? [index, index] : [option.weight, index]
53
+ end.map(&:first)
54
+ end
55
+
56
+ private
57
+
58
+ def fix_option_weights!
59
+ ordered_options.each_with_index do |option, index|
60
+ option.weight = index
61
+ end
62
+ end
63
+
64
+ def previous_option(value)
65
+ opts = ordered_options
66
+ pos = opts.index(value) - 1
67
+ return if pos < 0
68
+ opts[pos]
69
+ end
70
+
71
+
36
72
  end
37
73
  end
38
74
  end
@@ -4,6 +4,15 @@ module Ecoportal
4
4
  class Page
5
5
  class Component
6
6
  class SelectionOption < Common::Content::DoubleModel
7
+ class << self
8
+ def new_doc
9
+ {
10
+ "id" => new_uuid,
11
+ "weight" => 9999
12
+ }
13
+ end
14
+ end
15
+
7
16
  passkey :id
8
17
  passthrough :patch_ver, :name, :value
9
18
  passthrough :weight, :selected
@@ -27,6 +27,14 @@ module Ecoportal
27
27
  end.first
28
28
  end
29
29
 
30
+ def add(label:, type:)
31
+ fld_doc = Ecoportal::API::V2::Page::Component.new_doc(type: type)
32
+ upsert!(fld_doc) do |fld|
33
+ fld.label = label
34
+ yield(fld) if block_given?
35
+ end
36
+ end
37
+
30
38
  end
31
39
  end
32
40
  end
@@ -3,6 +3,28 @@ module Ecoportal
3
3
  class V2
4
4
  class Page
5
5
  class Section < Common::Content::DoubleModel
6
+ class << self
7
+ def new_doc(split: false)
8
+ {
9
+ "id" => new_uuid,
10
+ "type" => split ? "split" : "content",
11
+ "weight" => 9999
12
+ }.tap do |out|
13
+ component_ids = if split
14
+ {
15
+ "left_component_ids" => [],
16
+ "right_component_ids" => []
17
+ }
18
+ else
19
+ {
20
+ "component_ids" => []
21
+ }
22
+ end
23
+ out.merge!(component_ids)
24
+ end
25
+ end
26
+ end
27
+
6
28
  passkey :id
7
29
  passthrough :patch_ver, :weight, :type
8
30
  passthrough :heading, :left_heading, :right_heading
@@ -22,9 +44,9 @@ module Ecoportal
22
44
  end
23
45
 
24
46
  def components
25
- sec_ids = all_component_ids
26
- root.components.values_at(*sec_ids).select.with_index do |fld, i|
27
- puts "Warning: section #{id} points to missing field #{sec_ids[i]}" if !fld
47
+ fld_ids = all_component_ids
48
+ root.components.values_at(*fld_ids).select.with_index do |fld, i|
49
+ puts "Warning: field #{id} points to missing field #{fld_ids[i]}" if !fld
28
50
  fld && (!block_given? || yield(fld))
29
51
  end
30
52
  end
@@ -7,6 +7,39 @@ module Ecoportal
7
7
 
8
8
  self.klass = :section_class
9
9
 
10
+ def add(name: nil, split: false, pos: NOT_USED, before: NOT_USED, after: NOT_USED)
11
+ sec_doc = Ecoportal::API::V2::Page::Section.new_doc(split: split)
12
+ upsert!(sec_doc, pos: pos, before: before, after: after) do |section|
13
+ section.heading = name
14
+ if prev = previous_section(section)
15
+ section.weight = prev.weight
16
+ end
17
+ yield(section) if block_given?
18
+ #fix_weights! # a server bug prevents to set the weight of existing sections
19
+ end
20
+ end
21
+
22
+ def ordered
23
+ each_with_index.sort_by do |section, index|
24
+ (section.weight >= 9999) ? [index, index] : [section.weight, index]
25
+ end.map(&:first)
26
+ end
27
+
28
+ private
29
+
30
+ def fix_weights!
31
+ ordered.each_with_index do |section, index|
32
+ section.weight = index
33
+ end
34
+ end
35
+
36
+ def previous_section(value)
37
+ secs = ordered
38
+ pos = secs.index(value) - 1
39
+ return if pos < 0
40
+ secs[pos]
41
+ end
42
+
10
43
  end
11
44
  end
12
45
  end
@@ -9,6 +9,19 @@ module Ecoportal
9
9
  passarray :subtags, order_matters: false
10
10
  passarray :section_ids
11
11
  passthrough :can
12
+
13
+ def sections
14
+ sec_ids = section_ids.to_a
15
+ root.sections.values_at(*sec_ids).select.with_index do |sec, i|
16
+ puts "Warning: section #{id} points to missing section #{sec_ids[i]}" if !sec
17
+ fld && (!block_given? || yield(sec))
18
+ end.sort_by {|sec| sec.weight}
19
+ end
20
+
21
+ def attach_section(section)
22
+ section_ids.insert_one(section.id)
23
+ end
24
+
12
25
  end
13
26
  end
14
27
  end
@@ -13,12 +13,6 @@ module Ecoportal
13
13
  end
14
14
  end
15
15
 
16
- def sections
17
- section_ids.map do |id|
18
- root.sections.find {|sec| sec.id == id}
19
- end
20
- end
21
-
22
16
  end
23
17
  end
24
18
  end
@@ -53,7 +53,6 @@ module Ecoportal
53
53
  wrapped = Common::Content::WrappedResponse.new(response, page_class)
54
54
 
55
55
  return wrapped.result if wrapped.success?
56
- #return page_class.create(response.body["data"]) if response.success?
57
56
  raise "Could not get new page from template #{id} - Error #{response.status}: #{response.body}"
58
57
  end
59
58
 
@@ -1,7 +1,7 @@
1
1
  module Ecoportal
2
2
  module API
3
3
  class V2
4
- GEM_VERSION = "0.5.7"
4
+ GEM_VERSION = "0.5.8"
5
5
  end
6
6
  end
7
7
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: ecoportal-api-oozes
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.5.7
4
+ version: 0.5.8
5
5
  platform: ruby
6
6
  authors:
7
7
  - Oscar Segura
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2020-04-20 00:00:00.000000000 Z
11
+ date: 2020-06-23 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler