dxlite 0.3.5 → 0.4.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (6) hide show
  1. checksums.yaml +4 -4
  2. checksums.yaml.gz.sig +3 -2
  3. data/lib/dxlite.rb +143 -126
  4. data.tar.gz.sig +0 -0
  5. metadata +25 -25
  6. metadata.gz.sig +0 -0
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 52a6f519b3142ed9207a881bf65f9df9afceed0bc0eccdb258aa46b61988f20e
4
- data.tar.gz: 1df9a6b682dc8a05fea3d88152c2d8fdb6042cc75aa246731fa69374568bacfd
3
+ metadata.gz: 0e7ca783b4730eb45379b1e06eed84ad925b0c07fcffa430bdcffbe5e6eb4f58
4
+ data.tar.gz: abe705038e2ba9de443ec9d05ccc6f9ad0a6373c2f666e074ed663fde4c8d486
5
5
  SHA512:
6
- metadata.gz: 641a55501dcada49b36db49c38cbbaf66d94283b9c96f5572a7cfce098b651e899d0d3cdede7561cd2b0ce53b42f3a0b1c97518e39cf12ed75b0ca4ead920cef
7
- data.tar.gz: 1b5abceae389e71fd4f813e6cc841abf212eab8db0a56800dc1f627452b454593cf4b86ced042b8ce14a33c6ae34908f3ef90b968f39979fb268cf1feb2781f0
6
+ metadata.gz: ecf2d3105cf6e647a3218282369801206cce0711f7502a67b20fde8623dcb40dbe4e0c072e20f60d54a82b55e7479d6e4563f83dff43d53f603d50e26ab0f7a8
7
+ data.tar.gz: 3673db0fe254541450bc167d351af2992d2706923b6383dba8f61ffaf9ac2d47c2d2696a70e00f4cd55093d93f01e6e9ea6acb28a6392f21569113716d3180f1
checksums.yaml.gz.sig CHANGED
@@ -1,2 +1,3 @@
1
- oU!05<Zz��4C4&Psep�+Em�+�E���mOd4>�~�AW?C/(���&�m|��
2
- N�Ň� �CÍ�T�I��F'�د2vh�<���]���\B)Ú�Q�����9���GDHIT�̦�\Za��*�ͷwo��*�������^�@����f�� ��������hߋ��n��=J��4hm��/���Y��i+}r3"`�e��I��9?���nr'X?�ʅ6/=����](�ij�0��������Y�o*��z�1�=n�Ը��*�c*l�S�hv�$�޽bHc���O�V �wB���Dcһ+�'�b�r�Ƞ���E(���
1
+ %��э��[^X��l���IO����Џ)-��6�Ӿ� ��S�ԉ���`�k`��e�fJ�ۣ��ū%�C�Z��=m,-�)m���$�j[W���;�Ο��96��RNy��,Q�ׂ�n���41$�m���b�'��?�~9����?����6�%�]H�����aB2�C`�*z7��f�����pσ�����+S�*˞�?����`S�;Y
2
+ N_�T>��Ss|f�)9�5PX�[���
3
+ ����.q�� 5;���G��S ��®�ee.���m�1揜���5��Tؗ*�#��ӤH߻���@�Xw�����R�ǵ B ���y�C0EX�Z2�[��Q\p��w�����Y\v�9�B�آ؈
data/lib/dxlite.rb CHANGED
@@ -20,34 +20,34 @@ class DxLite
20
20
 
21
21
  return unless s
22
22
  buffer, type = RXFHelper.read(s)
23
-
23
+
24
24
  @filepath = s if type == :file or type == :dfs
25
-
25
+
26
26
  puts 'type: ' + type.inspect if @debug
27
27
  puts 'buffer: ' + buffer.inspect if @debug
28
28
 
29
29
  @records = []
30
-
30
+
31
31
  case type
32
-
32
+
33
33
  when :file
34
-
34
+
35
35
  read buffer
36
-
36
+
37
37
  when :text
38
-
38
+
39
39
  @summary = {schema: s}
40
40
 
41
-
41
+
42
42
  when :url
43
-
44
- read buffer
45
-
43
+
44
+ read buffer
45
+
46
46
  end
47
-
48
- puts '@summary: ' + @summary.inspect
47
+
48
+ puts '@summary: ' + @summary.inspect if @debug
49
49
  @schema = @summary[:schema]
50
-
50
+
51
51
  summary_attributes = {
52
52
  recordx_type: 'dynarex',
53
53
  default_key: @schema[/(?<=\()\w+/]
@@ -57,152 +57,169 @@ class DxLite
57
57
  @summary.merge!(summary_attributes)
58
58
 
59
59
  summary = @summary[:schema][/(?<=\[)[^\]]+/]
60
-
60
+
61
61
  if summary then
62
62
 
63
63
  summary.split(/ *, */).each do |x|
64
64
  @summary[x] = nil unless @summary[x]
65
65
  end
66
-
67
- end
68
-
69
- make_methods()
70
66
 
71
- end
67
+ end
68
+
69
+ make_methods()
70
+
71
+ end
72
72
 
73
73
  def all()
74
74
 
75
75
  @records.map do |h|
76
-
76
+
77
77
  puts 'h: ' + h.inspect if @debug
78
- RecordX.new(h[:body], self, h[:id], h[:created],
78
+ RecordX.new(h[:body], self, h[:id], h[:created],
79
79
  h[:last_modified])
80
80
  end
81
81
 
82
- end
83
-
82
+ end
83
+
84
84
  def delete(id)
85
85
  found = @records.find {|x| x[:id] == id}
86
-
86
+
87
87
  if found then
88
- @records.delete found
88
+ @records.delete found
89
89
  save() if @autosave
90
90
  end
91
-
91
+
92
92
  end
93
-
93
+
94
94
  def create(rawh, id: nil, custom_attributes: {created: Time.now})
95
95
 
96
+ if @debug then
97
+ puts 'create:: rawh: ' + rawh.inspect
98
+ puts 'custom_attributes: ' + custom_attributes.inspect
99
+ end
100
+
101
+ key = @summary[:default_key]
102
+
103
+ if key then
104
+
105
+ r = records.find {|x| x[:body][key.to_sym] == rawh[key.to_sym]}
106
+ if r then
107
+ r[:last_modified] = Time.now.to_s
108
+ return false
109
+ end
110
+
111
+ end
112
+
96
113
  if id.nil? then
97
-
114
+
98
115
  puts '@records: ' + @records.inspect if @debug
99
-
116
+
100
117
  if @records then
101
118
  id = @records.map {|x| x[:id].to_i}.max.to_i + 1
102
119
  else
103
120
  @records = []
104
121
  id = 1
105
122
  end
106
-
123
+
107
124
  end
108
-
125
+
109
126
  h2 = custom_attributes
110
-
111
- fields = rawh.keys
112
- puts 'fields: ' + fields.inspect if @debug
127
+
128
+ fields = rawh.keys
129
+ puts 'fields: ' + fields.inspect if @debug
113
130
 
114
131
  h3 = fields.map {|x| [x.to_sym, nil] }.to_h.merge(rawh)
115
132
  h = {id: id.to_s, created: h2[:created], last_modified: nil, body: h3}
116
133
  @records << h
117
-
134
+
118
135
  save() if @autosave
119
-
136
+
120
137
  RecordX.new(h[:body], self, h[:id], h[:created], h[:last_modified])
121
-
138
+
122
139
  end
123
-
140
+
124
141
  def fields()
125
142
  @fields
126
143
  end
127
-
144
+
128
145
  def inspect()
129
- "#<DxLite:%s @debug=%s, @summary=%s, ...>" % [object_id, @debug,
146
+ "#<DxLite:%s @debug=%s, @summary=%s, ...>" % [object_id, @debug,
130
147
  @summary.inspect]
131
148
  end
132
-
149
+
133
150
  # Parses 1 or more lines of text to create or update existing records.
134
151
 
135
152
  def parse(obj=nil)
136
-
153
+
137
154
  if obj.is_a? Array then
138
-
155
+
139
156
  unless schema() then
140
157
  puts 'obj.first: ' + obj.first.inspect if @debug
141
- cols = obj.first.keys.map {|c| c == 'id' ? 'uid' : c}
158
+ cols = obj.first.keys.map {|c| c == 'id' ? 'uid' : c}
142
159
  puts 'after cols' if @debug
143
160
  self.schema = "items/item(%s)" % cols.join(', ')
144
161
  end
145
-
162
+
146
163
  obj.each do |x|
147
164
  #puts 'x: ' + x.inspect if @debug
148
165
  self.create x, id: nil
149
166
  end
150
-
151
- return self
152
-
153
- end
167
+
168
+ return self
169
+
170
+ end
154
171
  end
155
-
172
+
156
173
  alias import parse
157
-
174
+
158
175
  def parse_xml(buffer)
159
-
176
+
160
177
  doc = Rexle.new(buffer)
161
-
178
+
162
179
  asummary = doc.root.xpath('summary/*').map do |node|
163
180
  puts 'node: ' + node.xml.inspect if @debug
164
181
  [node.name, node.text.to_s]
165
182
  end
166
-
183
+
167
184
  summary = Hash[asummary]
168
185
  summary[:schema] = summary['schema']
169
186
  %w(recordx_type format_mask schema).each {|x| summary.delete x}
170
-
187
+
171
188
  schema = summary[:schema]
172
189
  puts 'schema: ' + schema.inspect if @debug
173
-
190
+
174
191
  @fields = schema[/\(([^\)]+)/,1].split(/ *, +/)
175
192
  puts 'fields: ' + @fields.inspect if @debug
176
-
193
+
194
+ @summary = summary
195
+
177
196
  a = doc.root.xpath('records/*').each do |node|
178
-
197
+
179
198
  h = Hash[@fields.map {|field| [field.to_sym, node.text(field).to_s] }]
180
- self.create h, id: nil
181
-
199
+ self.create h, id: nil, custom_attributes: node.attributes
200
+
182
201
  end
183
202
 
184
- @summary = summary
185
-
186
203
  end
187
204
 
188
205
  def save(file=@filepath)
189
-
206
+
190
207
  return unless file
191
208
  @filepath = file
192
-
209
+
193
210
  s = File.extname(file) == '.json' ? to_json() : to_xml()
194
211
  File.write file, s
195
212
  end
196
-
213
+
197
214
  def to_a()
198
215
  @records.map {|x| x[:body]}
199
216
  end
200
-
217
+
201
218
  def to_h()
202
-
219
+
203
220
  root_name = schema()[/^\w+/]
204
221
  record_name = schema()[/(?<=\/)[^\(]+/]
205
-
222
+
206
223
  h = {
207
224
  root_name.to_sym =>
208
225
  {
@@ -210,139 +227,139 @@ class DxLite
210
227
  records: @records.map {|h| {record_name.to_sym => h} }
211
228
  }
212
229
  }
213
-
230
+
214
231
  end
215
-
232
+
216
233
  def to_json(pretty: true)
217
234
  pretty ? JSON.pretty_generate(to_h()) : to_h()
218
235
  end
219
-
236
+
220
237
  def to_xml()
221
-
238
+
222
239
  root_name = schema()[/^\w+/]
223
240
  record_name = schema()[/(?<=\/)[^\(]+/]
224
-
241
+
225
242
  a = RexleBuilder.build do |xml|
226
-
243
+
227
244
  xml.send(root_name.to_sym) do
228
245
  xml.summary({}, @summary)
229
246
  xml.records do
230
-
247
+
231
248
  all().each do |x|
232
-
233
- h = {id: x.id, created: x.created, last_modified: x.last_modified}
249
+
250
+ h = {id: x.id, created: x.created, last_modified: x.last_modified}
234
251
  puts 'x.to_h: ' + x.to_h.inspect if @debug
235
252
  xml.send(record_name.to_sym, h, x.to_h)
236
-
253
+
237
254
  end
238
-
255
+
239
256
  end
240
257
  end
241
258
  end
242
259
 
243
- Rexle.new(a).xml pretty: true
244
-
245
-
260
+ Rexle.new(a).xml pretty: true
261
+
262
+
246
263
  end
247
-
264
+
248
265
  # Updates a record from an id and a hash containing field name and field value.
249
- # dynarex.update 4, name: Jeff, age: 38
250
-
266
+ # dynarex.update 4, name: Jeff, age: 38
267
+
251
268
  def update(id, obj)
252
-
269
+
253
270
  if @debug then
254
271
  puts 'inside update'.info
255
272
  puts ('id: ' + id.inspect).debug
256
273
  puts ('obj.class: ' + obj.class.inspect).debug
257
274
  end
258
-
275
+
259
276
  r = @records.find {|x| x[:id] == id}
260
-
277
+
261
278
  if r then
262
-
279
+
263
280
  r[:body].merge!(obj)
264
281
  save() if @autosave
265
-
282
+
266
283
  end
267
-
284
+
268
285
  end
269
-
286
+
270
287
  private
271
-
288
+
272
289
  def find_record(rec, value, x)
273
- r = value.is_a?(Regexp) ? rec[x.to_sym] =~ value : rec[x.to_sym] == value
290
+ r = value.is_a?(Regexp) ? rec[x.to_sym] =~ value : rec[x.to_sym] == value
274
291
  end
275
-
292
+
276
293
  def make_methods()
277
-
294
+
278
295
  # for each summary item create get and set methods
279
-
296
+
280
297
  @summary.each do |key, value|
281
-
298
+
282
299
  define_singleton_method(key) { @summary[key] }
283
-
300
+
284
301
  define_singleton_method (key.to_s + '=').to_sym do |value|
285
302
  @summary[key] = value
286
- end
287
-
303
+ end
304
+
288
305
  end
289
-
306
+
290
307
  @fields = @summary[:schema][/(?<=\()[^\)]+/].split(',').map(&:strip)
291
308
 
292
309
  @fields.each do |x|
293
310
 
294
311
  define_singleton_method ('find_all_by_' + x).to_sym do |value|
295
-
312
+
296
313
  a = @records.select {|rec| find_record(rec[:body], value, x) }
297
-
314
+
298
315
  a.map do |h|
299
316
  RecordX.new(h[:body], self, h[:id], h[:created], h[:last_modified])
300
317
  end
301
-
318
+
302
319
  end
303
320
 
304
321
  define_singleton_method ('find_by_' + x).to_sym do |value|
305
-
322
+
306
323
  h = @records.find {|rec| find_record(rec[:body], value, x) }
307
324
  return nil unless h
308
-
309
- RecordX.new(h[:body], self, h[:id], h[:created], h[:last_modified])
310
-
325
+
326
+ RecordX.new(h[:body], self, h[:id], h[:created], h[:last_modified])
327
+
311
328
  end
312
329
 
313
330
  end
314
-
331
+
315
332
  end
316
-
333
+
317
334
  def read(buffer)
318
-
335
+
319
336
  if buffer[0] == '<' then
320
-
337
+
321
338
  parse_xml buffer
322
-
339
+
323
340
  else
324
-
341
+
325
342
  h1 = JSON.parse(buffer, symbolize_names: true)
326
343
  #puts 'h1:' + h1.inspect if @debug
327
-
344
+
328
345
  h = h1[h1.keys.first]
329
-
346
+
330
347
  @summary = {}
331
-
348
+
332
349
  h[:summary].each do |key, value|
333
-
350
+
334
351
  if %i(recordx_type format_mask schema).include? key then
335
352
  @summary[key] = value
336
- else
353
+ else
337
354
  @summary[key.to_s] = value
338
355
  end
339
-
356
+
340
357
  end
341
-
358
+
342
359
  @records = h[:records].map {|x| x[x.keys.first]}
343
-
360
+
344
361
  end
345
-
362
+
346
363
  end
347
-
364
+
348
365
  end
data.tar.gz.sig CHANGED
Binary file
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: dxlite
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.3.5
4
+ version: 0.4.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - James Robertson
@@ -11,31 +11,31 @@ cert_chain:
11
11
  - |
12
12
  -----BEGIN CERTIFICATE-----
13
13
  MIIEXjCCAsagAwIBAgIBATANBgkqhkiG9w0BAQsFADAsMSowKAYDVQQDDCFnZW1t
14
- YXN0ZXIvREM9amFtZXNyb2JlcnRzb24vREM9ZXUwHhcNMjAwNjIwMDk1OTIyWhcN
15
- MjEwNjIwMDk1OTIyWjAsMSowKAYDVQQDDCFnZW1tYXN0ZXIvREM9amFtZXNyb2Jl
16
- cnRzb24vREM9ZXUwggGiMA0GCSqGSIb3DQEBAQUAA4IBjwAwggGKAoIBgQCcnuff
17
- 5MVmDII05CloWme8XkTcCkGTFTCGqacPAa5803163oHfU0CovEbdSR8KNVXQ0mVz
18
- ZkE1kEi/xlAbZHKZTgJNjzXKcTfW0zNI/1l9Jyz59fjOUgO26oWgEJX1iJBUkzAx
19
- 1jHBvCGvAgKi1gmPovfrnPkXJ4e39Kklay4mrsfRO1qAkyfoUCn8TOc4/xVyB9UV
20
- zpSfryHT41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kGdYVNMp2rbLd
30
+ exukFDYOuLStjGnAl5KUfIj3fTKC9T9HGDvSaJec2KwnY2RIQhal8FGicLX5kkUx
31
+ BO3X5S9uHFZP2HY0VZq4CpGvuhz3ikCWxRLSVfypsJR9EXod8SkPpHhtP9JHUfAV
32
+ 7+dqSt7LPuHaYoiEWezPyfaZEn0oHFTFkLaDPtUDN1UvkMNaDwIQ9UBeQrscz3rm
33
+ 8tXnf5YUbiM9Ngft4jVa4FjEqjARjRB/l4OlO8cxIHZG+7KU7PhlbsqT/2ISjo+f
34
+ k0RwIfy2uAZ/no0qaVtnTkbeFBw4JKNnH5jrNEMUyEsbWkkeSg67vCHjZ+YLxdgn
35
+ 9pK+pqislEMFrWscGeMsc6YMS7ALxEujTuwTWyxmWAXnYft4ff2O1Zh2sPmdOlgf
36
+ camg7d8q+VZZzAtz0cFc4pip
37
37
  -----END CERTIFICATE-----
38
- date: 2021-02-20 00:00:00.000000000 Z
38
+ date: 2021-12-14 00:00:00.000000000 Z
39
39
  dependencies:
40
40
  - !ruby/object:Gem::Dependency
41
41
  name: recordx
@@ -78,7 +78,7 @@ dependencies:
78
78
  - !ruby/object:Gem::Version
79
79
  version: 1.1.3
80
80
  description:
81
- email: james@jamesrobertson.eu
81
+ email: digital.robertson@gmail.com
82
82
  executables: []
83
83
  extensions: []
84
84
  extra_rdoc_files: []
metadata.gz.sig CHANGED
Binary file