ecoportal-api-oozes 0.5.6 → 0.6.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 3b995a0d1c80408bc83f797ffe14f0bf8457d1e1be99eac780f3924c19d6efbe
4
- data.tar.gz: f06fae1cf1aaf5fbcc7fdc102a3f78f55da137d3ab9c5f12a5852c6d74d99af3
3
+ metadata.gz: 169653f85a26c45f2b60eb95358dd7e547185b07a5a722bb56643a63daa1fb1f
4
+ data.tar.gz: 696de7a2f37b8fcf7409be373bfb491b4fa70bfc504cace816a4b64076cb9bd7
5
5
  SHA512:
6
- metadata.gz: a2cf8d4e48892f6f31e6b852f8aeff840af65729d5d811596561f4841d77a2063a23d2dd0b111a67a1e8feafee12cf892c02fa4acab46e1b3a55af39554ab54e
7
- data.tar.gz: 3fe6beb0b48d961544e227328eeb43676470feab25a0713987cab4d4a44d0ec52a52f8f512a27be183efdbd1de4ac8cdddccd929ddbe859f8e1169ee3107bb95
6
+ metadata.gz: 6b134337977269a67e49d451bad2a854da602724326483e72cfa3c95cbb012d6f8fbbd88742d9f58fba019869c7c11ae5e14f74e2ae8756daaa8035ab9658d09
7
+ data.tar.gz: 342dbf60de74361387b383428a8b066184c9b0ce73dba98f3761da5c83b5efd8393da8cc7e836b58bf47aacac660493d316b93be0a88a3876c7d7195ab7afb49
@@ -0,0 +1,24 @@
1
+ # Change Log
2
+ All notable changes to this project will be documented in this file.
3
+
4
+ ## [0.6.1] - 2020-07-11
5
+
6
+ ### Added
7
+ - `Ecoportal::API::Common::Content::DoubleModel#pretty_print`
8
+ ### Changed
9
+ ### Fixed
10
+
11
+ ## [0.6.0] - 2020-07-11
12
+
13
+ ### Added
14
+ ### Changed
15
+ - upgraded `ecoportal-api` gem
16
+ ### Fixed
17
+
18
+ ## [0.5.9] - 2020-07-02
19
+
20
+ ### Added
21
+ - helper `Ecoportal::API::Common::Content::StringDigest#indexable_label`: to see the part of a label that gets indexed
22
+ - this `CHANGELOG.md` file
23
+ ### Changed
24
+ ### Fixed
@@ -27,5 +27,5 @@ Gem::Specification.new do |spec|
27
27
  spec.add_development_dependency "redcarpet", "~> 3.5", ">= 3.5.0"
28
28
  spec.add_development_dependency "pry"
29
29
 
30
- spec.add_dependency 'ecoportal-api', '~> 0.5', '>= 0.5.5'
30
+ spec.add_dependency 'ecoportal-api', '~> 0.6', '>= 0.6.0'
31
31
  end
@@ -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
@@ -253,10 +252,10 @@ module Ecoportal
253
252
  end
254
253
  end
255
254
 
256
- #def print
257
- # puts JSON.pretty_generate(as_json)
258
- # self
259
- #end
255
+ def pretty_print
256
+ puts JSON.pretty_generate(as_json)
257
+ self
258
+ end
260
259
 
261
260
  def replace_doc(new_doc)
262
261
  raise UnlinkedModel.new(from: "#{self.class}#replace_doc", key: _key) unless linked?
@@ -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
@@ -7,12 +7,17 @@ module Ecoportal
7
7
  module StringDigest
8
8
  MAX_HASH_LABEL = 64
9
9
 
10
+ def indexable_label(str)
11
+ return nil unless str
12
+ lbl = str.downcase.gsub(/[^A-Za-z]+/,"-").slice(0, MAX_HASH_LABEL)
13
+ return nil unless lbl.length >= 3
14
+ lbl
15
+ end
16
+
10
17
  # Calculates the Hash of the field based on label
11
18
  def hash_label(str)
12
- return false unless str
13
- label = str.downcase.gsub(/[^A-Za-z]+/,"-").slice(0, MAX_HASH_LABEL)
14
- return false unless label.length >= 3
15
- return "z" + Digest::MD5.hexdigest(label).slice(0, 8);
19
+ return nil unless lbl = indexable_label(str)
20
+ "z" + Digest::MD5.hexdigest(lbl).slice(0, 8);
16
21
  end
17
22
 
18
23
  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"]
@@ -81,6 +88,10 @@ module Ecoportal
81
88
  root.sections.find {|sec| sec.component?(id)}
82
89
  end
83
90
 
91
+ def indexable_label
92
+ self.class.indexable_label(label)
93
+ end
94
+
84
95
  end
85
96
  end
86
97
  end
@@ -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
@@ -41,7 +41,7 @@ module Ecoportal
41
41
  return stages.get(pid: pid, sid: sid)
42
42
  end
43
43
  end
44
- raise "Could not get page #{id} - Error #{response.status}: #{response.body}"
44
+ raise "Could not get page #{pid} - Error #{response.status}: #{response.body}"
45
45
  end
46
46
 
47
47
  # Gets a `new` non-existing page via api with all the ids initialized.
@@ -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.6"
4
+ GEM_VERSION = "0.6.1"
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.6
4
+ version: 0.6.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: 2020-04-14 00:00:00.000000000 Z
11
+ date: 2020-07-11 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -110,22 +110,22 @@ dependencies:
110
110
  name: ecoportal-api
111
111
  requirement: !ruby/object:Gem::Requirement
112
112
  requirements:
113
- - - "~>"
114
- - !ruby/object:Gem::Version
115
- version: '0.5'
116
113
  - - ">="
117
114
  - !ruby/object:Gem::Version
118
- version: 0.5.5
115
+ version: 0.6.0
116
+ - - "~>"
117
+ - !ruby/object:Gem::Version
118
+ version: '0.6'
119
119
  type: :runtime
120
120
  prerelease: false
121
121
  version_requirements: !ruby/object:Gem::Requirement
122
122
  requirements:
123
- - - "~>"
124
- - !ruby/object:Gem::Version
125
- version: '0.5'
126
123
  - - ">="
127
124
  - !ruby/object:Gem::Version
128
- version: 0.5.5
125
+ version: 0.6.0
126
+ - - "~>"
127
+ - !ruby/object:Gem::Version
128
+ version: '0.6'
129
129
  description:
130
130
  email:
131
131
  - rien@ecoportal.co.nz
@@ -140,6 +140,7 @@ files:
140
140
  - ".rubocop.yml"
141
141
  - ".travis.yml"
142
142
  - ".yardopts"
143
+ - CHANGELOG.md
143
144
  - Gemfile
144
145
  - LICENSE
145
146
  - README.md