dxlite 0.3.2 → 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 +0 -0
  3. data/lib/dxlite.rb +167 -122
  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: a11055ac19ac37f39d446b07bb28d1cc907a368e5c37a9535d59fe85d5c176a9
4
- data.tar.gz: 2764eddbddbae683431653acd277bf32fcbbaa681ca0d6205619f0518c0bbc3a
3
+ metadata.gz: 0e7ca783b4730eb45379b1e06eed84ad925b0c07fcffa430bdcffbe5e6eb4f58
4
+ data.tar.gz: abe705038e2ba9de443ec9d05ccc6f9ad0a6373c2f666e074ed663fde4c8d486
5
5
  SHA512:
6
- metadata.gz: d05fee5c4205047d1dc7eb84cd30a148ad662f1ce5f68bc939cd6d8fa52f6fa8ed296a6b460c92b3205d55f66951aa961105df4b5540b158f6326022a2feed5f
7
- data.tar.gz: 40bdd8815580e774d5068b8373e3bc2656eadd3b39a9f63c7d271bad412304a134d6957b817f7441c27c1ce47b34f6e447d3ce9b67870714b6b780f329edc38b
6
+ metadata.gz: ecf2d3105cf6e647a3218282369801206cce0711f7502a67b20fde8623dcb40dbe4e0c072e20f60d54a82b55e7479d6e4563f83dff43d53f603d50e26ab0f7a8
7
+ data.tar.gz: 3673db0fe254541450bc167d351af2992d2706923b6383dba8f61ffaf9ac2d47c2d2696a70e00f4cd55093d93f01e6e9ea6acb28a6392f21569113716d3180f1
checksums.yaml.gz.sig CHANGED
Binary file
data/lib/dxlite.rb CHANGED
@@ -11,7 +11,7 @@ require 'rxfhelper'
11
11
  class DxLite
12
12
  using ColouredText
13
13
 
14
- attr_accessor :summary, :filepath
14
+ attr_accessor :summary, :filepath, :schema
15
15
  attr_reader :records
16
16
 
17
17
  def initialize(s=nil, autosave: false, debug: false)
@@ -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,131 +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
-
96
- id ||= @records.map {|x| x[:id].to_i}.max.to_i + 1
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
+
113
+ if id.nil? then
114
+
115
+ puts '@records: ' + @records.inspect if @debug
116
+
117
+ if @records then
118
+ id = @records.map {|x| x[:id].to_i}.max.to_i + 1
119
+ else
120
+ @records = []
121
+ id = 1
122
+ end
123
+
124
+ end
125
+
97
126
  h2 = custom_attributes
98
- h = fields.map {|x| [x.to_sym, nil] }.to_h.merge(rawh)
99
- @records << {id: id.to_s, created: h2[:created], last_modified: nil,
100
- body: h}
101
-
127
+
128
+ fields = rawh.keys
129
+ puts 'fields: ' + fields.inspect if @debug
130
+
131
+ h3 = fields.map {|x| [x.to_sym, nil] }.to_h.merge(rawh)
132
+ h = {id: id.to_s, created: h2[:created], last_modified: nil, body: h3}
133
+ @records << h
134
+
102
135
  save() if @autosave
136
+
137
+ RecordX.new(h[:body], self, h[:id], h[:created], h[:last_modified])
138
+
103
139
  end
104
-
140
+
105
141
  def fields()
106
142
  @fields
107
143
  end
108
-
144
+
109
145
  def inspect()
110
- "#<DxLite:%s @debug=%s, @summary=%s, ...>" % [object_id, @debug,
146
+ "#<DxLite:%s @debug=%s, @summary=%s, ...>" % [object_id, @debug,
111
147
  @summary.inspect]
112
148
  end
113
-
149
+
114
150
  # Parses 1 or more lines of text to create or update existing records.
115
151
 
116
152
  def parse(obj=nil)
117
-
153
+
118
154
  if obj.is_a? Array then
119
-
155
+
120
156
  unless schema() then
121
- cols = obj.first.keys.map {|c| c == 'id' ? 'uid' : c}
157
+ puts 'obj.first: ' + obj.first.inspect if @debug
158
+ cols = obj.first.keys.map {|c| c == 'id' ? 'uid' : c}
159
+ puts 'after cols' if @debug
122
160
  self.schema = "items/item(%s)" % cols.join(', ')
123
161
  end
124
-
162
+
125
163
  obj.each do |x|
126
164
  #puts 'x: ' + x.inspect if @debug
127
165
  self.create x, id: nil
128
166
  end
129
-
130
- return self
131
-
132
- end
167
+
168
+ return self
169
+
170
+ end
133
171
  end
134
-
172
+
135
173
  alias import parse
136
-
174
+
137
175
  def parse_xml(buffer)
138
-
176
+
139
177
  doc = Rexle.new(buffer)
140
-
178
+
141
179
  asummary = doc.root.xpath('summary/*').map do |node|
142
180
  puts 'node: ' + node.xml.inspect if @debug
143
181
  [node.name, node.text.to_s]
144
182
  end
145
-
183
+
146
184
  summary = Hash[asummary]
147
185
  summary[:schema] = summary['schema']
148
186
  %w(recordx_type format_mask schema).each {|x| summary.delete x}
149
-
187
+
150
188
  schema = summary[:schema]
151
189
  puts 'schema: ' + schema.inspect if @debug
152
-
190
+
153
191
  @fields = schema[/\(([^\)]+)/,1].split(/ *, +/)
154
192
  puts 'fields: ' + @fields.inspect if @debug
155
-
193
+
194
+ @summary = summary
195
+
156
196
  a = doc.root.xpath('records/*').each do |node|
157
-
197
+
158
198
  h = Hash[@fields.map {|field| [field.to_sym, node.text(field).to_s] }]
159
- self.create h, id: nil
160
-
199
+ self.create h, id: nil, custom_attributes: node.attributes
200
+
161
201
  end
162
202
 
163
- @summary = summary
164
-
165
203
  end
166
204
 
167
205
  def save(file=@filepath)
168
-
206
+
169
207
  return unless file
170
208
  @filepath = file
171
-
209
+
172
210
  s = File.extname(file) == '.json' ? to_json() : to_xml()
173
211
  File.write file, s
174
212
  end
175
-
213
+
176
214
  def to_a()
177
215
  @records.map {|x| x[:body]}
178
216
  end
179
-
217
+
180
218
  def to_h()
181
-
219
+
182
220
  root_name = schema()[/^\w+/]
183
221
  record_name = schema()[/(?<=\/)[^\(]+/]
184
-
222
+
185
223
  h = {
186
224
  root_name.to_sym =>
187
225
  {
@@ -189,132 +227,139 @@ class DxLite
189
227
  records: @records.map {|h| {record_name.to_sym => h} }
190
228
  }
191
229
  }
192
-
230
+
193
231
  end
194
-
232
+
195
233
  def to_json(pretty: true)
196
234
  pretty ? JSON.pretty_generate(to_h()) : to_h()
197
235
  end
198
-
236
+
199
237
  def to_xml()
200
-
238
+
201
239
  root_name = schema()[/^\w+/]
202
240
  record_name = schema()[/(?<=\/)[^\(]+/]
203
-
241
+
204
242
  a = RexleBuilder.build do |xml|
205
-
243
+
206
244
  xml.send(root_name.to_sym) do
207
245
  xml.summary({}, @summary)
208
246
  xml.records do
209
-
247
+
210
248
  all().each do |x|
211
-
212
- 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}
213
251
  puts 'x.to_h: ' + x.to_h.inspect if @debug
214
252
  xml.send(record_name.to_sym, h, x.to_h)
215
-
253
+
216
254
  end
217
-
255
+
218
256
  end
219
257
  end
220
258
  end
221
259
 
222
- Rexle.new(a).xml pretty: true
223
-
224
-
260
+ Rexle.new(a).xml pretty: true
261
+
262
+
225
263
  end
226
-
264
+
227
265
  # Updates a record from an id and a hash containing field name and field value.
228
- # dynarex.update 4, name: Jeff, age: 38
229
-
266
+ # dynarex.update 4, name: Jeff, age: 38
267
+
230
268
  def update(id, obj)
231
-
269
+
232
270
  if @debug then
233
271
  puts 'inside update'.info
234
272
  puts ('id: ' + id.inspect).debug
235
273
  puts ('obj.class: ' + obj.class.inspect).debug
236
274
  end
237
-
275
+
238
276
  r = @records.find {|x| x[:id] == id}
239
-
277
+
240
278
  if r then
241
-
279
+
242
280
  r[:body].merge!(obj)
243
281
  save() if @autosave
244
-
282
+
245
283
  end
246
-
284
+
247
285
  end
248
-
286
+
249
287
  private
250
-
288
+
251
289
  def find_record(rec, value, x)
252
- 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
253
291
  end
254
-
292
+
255
293
  def make_methods()
256
-
294
+
257
295
  # for each summary item create get and set methods
258
-
296
+
259
297
  @summary.each do |key, value|
260
-
298
+
261
299
  define_singleton_method(key) { @summary[key] }
262
-
300
+
263
301
  define_singleton_method (key.to_s + '=').to_sym do |value|
264
302
  @summary[key] = value
265
- end
266
-
303
+ end
304
+
267
305
  end
268
-
306
+
269
307
  @fields = @summary[:schema][/(?<=\()[^\)]+/].split(',').map(&:strip)
270
308
 
271
309
  @fields.each do |x|
272
310
 
273
311
  define_singleton_method ('find_all_by_' + x).to_sym do |value|
274
-
275
- @records.select {|rec| find_record(rec[:body], value, x) }
276
-
312
+
313
+ a = @records.select {|rec| find_record(rec[:body], value, x) }
314
+
315
+ a.map do |h|
316
+ RecordX.new(h[:body], self, h[:id], h[:created], h[:last_modified])
317
+ end
318
+
277
319
  end
278
320
 
279
321
  define_singleton_method ('find_by_' + x).to_sym do |value|
280
-
281
- @records.find {|rec| find_record(rec[:body], value, x) }
282
-
322
+
323
+ h = @records.find {|rec| find_record(rec[:body], value, x) }
324
+ return nil unless h
325
+
326
+ RecordX.new(h[:body], self, h[:id], h[:created], h[:last_modified])
327
+
283
328
  end
284
329
 
285
330
  end
286
-
331
+
287
332
  end
288
-
333
+
289
334
  def read(buffer)
290
-
335
+
291
336
  if buffer[0] == '<' then
292
-
337
+
293
338
  parse_xml buffer
294
-
339
+
295
340
  else
296
-
341
+
297
342
  h1 = JSON.parse(buffer, symbolize_names: true)
298
343
  #puts 'h1:' + h1.inspect if @debug
299
-
344
+
300
345
  h = h1[h1.keys.first]
301
-
346
+
302
347
  @summary = {}
303
-
348
+
304
349
  h[:summary].each do |key, value|
305
-
350
+
306
351
  if %i(recordx_type format_mask schema).include? key then
307
352
  @summary[key] = value
308
- else
353
+ else
309
354
  @summary[key.to_s] = value
310
355
  end
311
-
356
+
312
357
  end
313
-
358
+
314
359
  @records = h[:records].map {|x| x[x.keys.first]}
315
-
360
+
316
361
  end
317
-
362
+
318
363
  end
319
-
364
+
320
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.2
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
- zpSfryHT41gM3vUyVyfkLsC8St6oI2j6R45YJIL2Hz1UBZsFk8ICn8MKL84b+nyX
21
- YiW3BoOhB5x4M+tORhMqIYwSW///P3ztFVuZCoZsZm7KDlmZ5Q+kbtg2O6WJqOeX
22
- 2CLAag6UYpWUJd9SBDOOLvNAzdzKHOlD/tJQnHrLTJzNSQQZcCLa7xjAob2vooFi
23
- zO7nNoPnRb7MFAdnjEgIPra6GZ6BxSzeWKmkXX6vFCs8Uy8N0ijHyWg4NKDlgHzl
24
- VCS9RW6U/gIhciP3kabmxcu2R67I79UDJU4tf8fYN+7tZLfU7g5S0QLQfrsCAwEA
25
- AaOBijCBhzAJBgNVHRMEAjAAMAsGA1UdDwQEAwIEsDAdBgNVHQ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sc6YMS7ALxEujTuwTWyxmWAXnYft4ff2O1Zh2sPmdOlgf
36
+ camg7d8q+VZZzAtz0cFc4pip
37
37
  -----END CERTIFICATE-----
38
- date: 2021-01-28 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