ecoportal-api-v2 2.0.10 → 2.0.11

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: 3df004ef45ea643952802e19774bfaa2f6941ea21904f5d28a00fa0eb4a252a1
4
- data.tar.gz: e491e2111d01d551225289e91a32129ee1dd91638951315429b6598a0ca872ad
3
+ metadata.gz: 0a93753cf1640abe95935d227eb0518f39e4dc007da9c2771d872bbcc7f0f7ff
4
+ data.tar.gz: 7fc6475b6d05011ab94bc3774922afe0e6c0501420633556c7580455dc26724e
5
5
  SHA512:
6
- metadata.gz: 3dabbbe1c8c62ba1aef3f632e8d2cf38b468699c5a6e6eb6dd5d052a2a206ff81d8d60a4cee0a01263a90c7a8ba11c6531a484b647069d9008410c8e9966819f
7
- data.tar.gz: cf999bb89aa0d15b4f210fa11e6ae7cf44efe780434a7cc35acaaa7df49e3a7a107f08ca43e832661a8236b87ecc8654433f1374014e70725675ee1af513bb47
6
+ metadata.gz: 210a1b99d67ee7aa17e5ae3050a1c23d65a4a41c625dfa6d5cd016e8a32f1d346aff9c2ce94a846188a47a74f97ffde28df05af7425d9269aebdc5a84bb8b0c8
7
+ data.tar.gz: 841d3d7e2b8396f673e76d7b6e14eeea773acda0bc323f60af86741032ff2e6a6bc4c79a6bce873f8b7a51f699718b67d0103773b690cd9236421b7ff75cff27
data/CHANGELOG.md CHANGED
@@ -2,6 +2,25 @@
2
2
 
3
3
  All notable changes to this project will be documented in this file.
4
4
 
5
+ ## [2.0.12] - 2024-10-xx
6
+
7
+ ### Added
8
+
9
+ ### Changed
10
+
11
+ ### Fixed
12
+
13
+ ## [2.0.11] - 2024-10-18
14
+
15
+ ### Changed
16
+
17
+ - upgrade `ecoportal-api` core gem
18
+
19
+ ### Fixed
20
+
21
+ - `Ecoportal::API::V2::Page::Component::ReferenceField#delete`
22
+ - It wasn't deleting references
23
+
5
24
  ## [2.0.10] - 2024-10-01
6
25
 
7
26
  ### Added
@@ -12,8 +31,6 @@ All notable changes to this project will be documented in this file.
12
31
 
13
32
  - upgraded core `ecoportal-api` **gem** dependency
14
33
 
15
- ### Fixed
16
-
17
34
  ## [2.0.9] - 2024-08-22
18
35
 
19
36
  ### Added
@@ -25,15 +25,15 @@ Gem::Specification.new do |spec|
25
25
  spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
26
26
  spec.require_paths = ['lib']
27
27
 
28
- spec.add_development_dependency 'pry' , '~> 0.14'
28
+ spec.add_development_dependency 'pry', '~> 0.14'
29
29
  spec.add_development_dependency 'rake', '>= 13.0.3', '< 14'
30
30
  spec.add_development_dependency 'redcarpet', '>= 3.6.0', '< 4'
31
31
  spec.add_development_dependency 'rspec', '>= 3.12.0', '< 4'
32
32
  spec.add_development_dependency 'rubocop', '~> 1'
33
33
  spec.add_development_dependency 'rubocop-rake', '~> 0'
34
- spec.add_development_dependency 'yard', '~> 0.9'
34
+ spec.add_development_dependency 'yard', '~> 0.9'
35
35
 
36
- spec.add_dependency 'ecoportal-api', '~> 0.10', '>= 0.10.2'
36
+ spec.add_dependency 'ecoportal-api', '~> 0.10', '>= 0.10.5'
37
37
  spec.add_dependency 'mime-types', '~> 3.5', '>= 3.5.2'
38
38
  end
39
39
 
@@ -1,3 +1,4 @@
1
+ # rubocop:disable Naming/MethodParameterName
1
2
  module Ecoportal
2
3
  module API
3
4
  module Common
@@ -5,9 +6,9 @@ module Ecoportal
5
6
  module HashDiffPatch
6
7
  extend DocHelpers
7
8
 
8
- ID_KEYS = %w[id]
9
- META_KEYS = %w[id patch_ver]
10
- NO_CHANGES = "%not-changed!%"
9
+ ID_KEYS = %w[id].freeze
10
+ META_KEYS = %w[id patch_ver].freeze
11
+ NO_CHANGES = "%not-changed!%".freeze
11
12
 
12
13
  class << self
13
14
  # The `patch data` is built as follows:
@@ -38,14 +39,13 @@ module Ecoportal
38
39
  # @param b [Hash] previous hash model
39
40
  # @return [Hash] a `patch data`
40
41
  def patch_diff(a, b)
41
- case
42
- when b.is_a?(Hash) && !empty?(b) && empty?(a)
42
+ if b.is_a?(Hash) && !empty?(b) && empty?(a)
43
43
  patch_delete(b)
44
- when a.is_a?(Hash) && !empty?(a) && empty?(b)
44
+ elsif a.is_a?(Hash) && !empty?(a) && empty?(b)
45
45
  patch_new(a)
46
- when a.is_a?(Hash) && b.is_a?(Hash)
46
+ elsif a.is_a?(Hash) && b.is_a?(Hash)
47
47
  patch_update(a, b)
48
- when any_array?(a, b)
48
+ elsif any_array?(a, b)
49
49
  patch_data_array(a, b)
50
50
  else
51
51
  a
@@ -54,9 +54,12 @@ module Ecoportal
54
54
 
55
55
  private
56
56
 
57
- def equal_values(a, b)
57
+ def equal_values?(a, b)
58
58
  if a.is_a?(String) || b.is_a?(String)
59
- return true if a.to_s.strip.empty? && b.to_s.strip.empty?
59
+ a_empty = a.to_s.strip.empty?
60
+ b_empty = b.to_s.strip.empty?
61
+
62
+ return true if a_empty && b_empty
60
63
  end
61
64
  a == b
62
65
  end
@@ -66,124 +69,137 @@ module Ecoportal
66
69
  def patch_data(a, b = nil, delete: false)
67
70
  {}.tap do |data_hash|
68
71
  if delete
69
- patch_ver = (a && a["patch_ver"]) || 1
70
- data_hash["patch_ver"] = patch_ver
72
+ patch_ver = (a && a['patch_ver']) || 1
73
+ data_hash['patch_ver'] = patch_ver
71
74
  next
72
75
  end
76
+
73
77
  a.each do |key, a_value|
74
- b_value = b[key] if b_has_key = b && b.key?(key)
75
- is_meta_key = META_KEYS.include?(key)
76
- skip_equals = b_has_key && equal_values(a_value, b_value)
77
- next if is_meta_key || skip_equals
78
+ next if META_KEYS.include?(key)
79
+
80
+ b_value = nil
81
+
82
+ if b&.key?(key)
83
+ b_value = b[key]
84
+ next if equal_values?(a_value, b_value)
85
+ end
86
+
78
87
  data_hash[key] = patch_diff(a_value, b_value)
88
+
79
89
  data_hash.delete(key) if data_hash[key] == NO_CHANGES
80
90
  end
91
+
81
92
  #if (data_hash.keys - ID_KEYS).empty?
82
- if (data_hash.keys - META_KEYS).empty?
83
- return NO_CHANGES
84
- else
85
- #patch_ver = (b && b["patch_ver"]) || 1
86
- #data_hash["patch_ver"] = patch_ver
87
- if b && b.key?("patch_ver")
88
- data_hash["patch_ver"] = b["patch_ver"]
89
- elsif a && a.key?("patch_ver")
90
- data_hash["patch_ver"] = a["patch_ver"]
91
- end
92
- data_hash.delete("force_patch")
93
+ return NO_CHANGES if (data_hash.keys - META_KEYS).empty?
94
+
95
+ if b&.key?('patch_ver')
96
+ data_hash['patch_ver'] = b['patch_ver']
97
+ elsif a&.key?('patch_ver')
98
+ data_hash['patch_ver'] = a['patch_ver']
93
99
  end
100
+
101
+ data_hash.delete("force_patch")
94
102
  end
95
103
  end
96
104
 
97
105
  def patch_delete(b)
98
106
  return NO_CHANGES unless b.is_a?(Hash)
99
- if id = get_id(b, exception: false)
100
- {
101
- "id" => id,
102
- "operation" => "deleted",
103
- "data" => patch_data(b, delete: true)
104
- }
105
- else
106
- nil
107
- end
107
+ return nil unless (id = get_id(b, exception: false))
108
+
109
+ {
110
+ 'id' => id,
111
+ 'operation' => 'deleted',
112
+ 'data' => patch_data(b, delete: true)
113
+ }
108
114
  end
109
115
 
110
116
  def patch_new(a)
111
117
  return NO_CHANGES unless a.is_a?(Hash)
112
- if id = get_id(a, exception: false)
113
- {
114
- "id" => id,
115
- "operation" => "new",
116
- "data" => patch_data(a)
117
- }
118
- else
119
- a
120
- end
118
+ return a unless (id = get_id(a, exception: false))
119
+
120
+ {
121
+ 'id' => id,
122
+ 'operation' => 'new',
123
+ 'data' => patch_data(a)
124
+ }
121
125
  end
122
126
 
123
127
  def patch_update(a, b)
124
128
  return NO_CHANGES unless a.is_a?(Hash)
125
- if id = get_id(a, exception: false)
126
- {
127
- "id" => id,
128
- "operation" => "changed",
129
- "data" => patch_data(a, b)
130
- }.tap do |update_hash|
131
- return nil unless update_hash["data"] != NO_CHANGES
132
- end
133
- else
134
- a
129
+ return a unless (id = get_id(a, exception: false))
130
+
131
+ {
132
+ 'id' => id,
133
+ 'operation' => 'changed',
134
+ 'data' => patch_data(a, b)
135
+ }.tap do |update_hash|
136
+ return nil unless update_hash['data'] != NO_CHANGES
135
137
  end
136
138
  end
137
139
 
138
140
  def patch_data_array(a, b)
141
+ return patch_data_nested_array(a, b) if nested_array?(a, b)
142
+
143
+ patch_data_flat_array(a, b)
144
+ end
145
+
146
+ def patch_data_flat_array(a, b)
139
147
  original_b = b
140
- a ||= []; b ||= []
141
- if !nested_array?(a, b)
142
- if a.length == b.length && (a & b).length == b.length
143
- if original_b
144
- NO_CHANGES
145
- else
146
- a
147
- end
148
- else
149
- a
150
- end
151
- else # array with nested elements
152
- a_ids = array_ids(a)
153
- b_ids = array_ids(b)
154
-
155
- del_ids = b_ids - a_ids
156
- oth_ids = b_ids & a_ids
157
- new_ids = a_ids - b_ids
158
-
159
- arr_delete = del_ids.map do |id|
160
- patch_delete(array_id_item(b, id))
161
- end.compact
162
-
163
- arr_update = oth_ids.map do |id|
164
- patch_update(array_id_item(a, id), array_id_item(b, id))
165
- end.compact
166
-
167
- arr_new = new_ids.map do |id|
168
- patch_new(array_id_item(a, id))
169
- end.compact
170
-
171
- (arr_delete.concat(arr_update).concat(arr_new)).tap do |patch_array|
172
- # remove data with no `id`
173
- patch_array.reject! {|item| !item.is_a?(Hash)}
174
- return NO_CHANGES if patch_array.empty?
175
- end
148
+ a ||= []
149
+ b ||= []
150
+
151
+ same_elements =
152
+ a.length == b.length &&
153
+ (a & b).length == b.length
154
+
155
+ return a unless same_elements
156
+ return NO_CHANGES if original_b
157
+
158
+ a
159
+ end
160
+
161
+ def patch_data_nested_array(a, b)
162
+ a ||= []
163
+ b ||= []
164
+
165
+ # array with nested elements
166
+ a_ids = array_ids(a)
167
+ b_ids = array_ids(b)
168
+
169
+ del_ids = b_ids - a_ids
170
+ oth_ids = b_ids & a_ids
171
+ new_ids = a_ids - b_ids
172
+
173
+ arr_delete = del_ids.map do |id|
174
+ patch_delete(array_id_item(b, id))
175
+ end.compact
176
+
177
+ arr_update = oth_ids.map do |id|
178
+ patch_update(array_id_item(a, id), array_id_item(b, id))
179
+ end.compact
180
+
181
+ arr_new = new_ids.map do |id|
182
+ patch_new(array_id_item(a, id))
183
+ end.compact
184
+
185
+ arr_delete.concat(arr_update).concat(arr_new).tap do |patch_array|
186
+ # remove data with no `id`
187
+ patch_array.select! {|item| item.is_a?(Hash)}
188
+ return NO_CHANGES if patch_array.empty?
176
189
  end
177
190
  end
178
191
 
179
192
  def nested_array?(*arr)
180
- case
181
- when arr.length > 1
193
+ if arr.length > 1
182
194
  arr.any? {|a| nested_array?(a)}
183
- when arr.length == 1
184
- arr = arr.first
195
+ elsif arr.length == 1
196
+ arr = arr.first || []
185
197
  arr.any? do |item|
186
- item.is_a?(Hash) && item.has_key?("patch_ver")
198
+ next false unless item.is_a?(Hash)
199
+ next true if item.key?('patch_ver')
200
+ # next true if item.key?('id')
201
+
202
+ false
187
203
  end
188
204
  else
189
205
  false
@@ -197,11 +213,13 @@ module Ecoportal
197
213
  def empty?(a)
198
214
  bool = !a
199
215
  bool ||= a.respond_to?(:empty?) && a.empty?
216
+ bool
200
217
  end
201
-
202
218
  end
203
219
  end
204
220
  end
205
221
  end
206
222
  end
207
223
  end
224
+
225
+ # rubocop:enable Naming/MethodParameterName
@@ -16,18 +16,18 @@ module Ecoportal
16
16
 
17
17
  def reference_ids
18
18
  references.map do |ref|
19
- ref["id"]
19
+ ref['id']
20
20
  end
21
21
  end
22
22
 
23
23
  def add(*ref_ids)
24
- doc["references"].tap do |refs|
24
+ doc['references'].tap do |refs|
25
25
  ref_ids.each do |ref_id|
26
26
  next if reference_ids.include?(ref_id)
27
27
  refs.push({
28
- "id" => ref_id,
29
- "weight" => 0,
30
- "patch_ver" => 0
28
+ 'id' => ref_id,
29
+ 'weight' => 0,
30
+ 'patch_ver' => 0
31
31
  })
32
32
  end
33
33
  end
@@ -39,13 +39,20 @@ module Ecoportal
39
39
 
40
40
  def delete(*ref_ids)
41
41
  ref_ids.each do |ref_id|
42
- doc_ref = doc["references"].find do |df|
43
- df["id"] == ref_id
42
+ doc_ref = doc['references'].find do |df|
43
+ df['id'] == ref_id
44
44
  end
45
45
 
46
46
  next unless doc_ref
47
47
 
48
- doc["references"].delete(doc_ref)
48
+ ori_ref = original_doc['references'].find do |od|
49
+ od['id'] == ref_id
50
+ end
51
+
52
+ # force patch version
53
+ ori_ref['patch_ver'] ||= 0
54
+
55
+ doc['references'].delete(doc_ref)
49
56
  end
50
57
  end
51
58
 
@@ -1,5 +1,5 @@
1
1
  module Ecoportal
2
2
  module API
3
- GEM2_VERSION = '2.0.10'.freeze
3
+ GEM2_VERSION = '2.0.11'.freeze
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: 2.0.10
4
+ version: 2.0.11
5
5
  platform: ruby
6
6
  authors:
7
7
  - Oscar Segura
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2024-09-30 00:00:00.000000000 Z
11
+ date: 2024-10-17 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: pry
@@ -135,7 +135,7 @@ dependencies:
135
135
  version: '0.10'
136
136
  - - ">="
137
137
  - !ruby/object:Gem::Version
138
- version: 0.10.2
138
+ version: 0.10.5
139
139
  type: :runtime
140
140
  prerelease: false
141
141
  version_requirements: !ruby/object:Gem::Requirement
@@ -145,7 +145,7 @@ dependencies:
145
145
  version: '0.10'
146
146
  - - ">="
147
147
  - !ruby/object:Gem::Version
148
- version: 0.10.2
148
+ version: 0.10.5
149
149
  - !ruby/object:Gem::Dependency
150
150
  name: mime-types
151
151
  requirement: !ruby/object:Gem::Requirement