dxlite 0.3.3 → 0.4.1

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 +166 -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: b07aaa0e99b004c084971d3d47bec08bd7dc4b2b00ed1744516c8a936d3a770d
4
- data.tar.gz: 1354917117fe1e45a811e1e609980dbd83638a04a3cd940d5c88d80a2513b5bd
3
+ metadata.gz: a826f1bbdc7b1560d8a66ff303a87a1cf3a789cf0281b1aa8130ce0cdaf4893e
4
+ data.tar.gz: e4e5401e812f9b69def5f87f6cd2178292c7fb58d604bf6a091da1a8d634ea64
5
5
  SHA512:
6
- metadata.gz: 1d32f0315a748d91738e7650c4f9636535563732feb283805d3ed0ec45e4ff9efcba278315ae9c276afbc7cb6b19d847df17db99622198715363f6324a4c0337
7
- data.tar.gz: '07339c65f3bcc16f9ba435981a0f32d71348778cfdd3f2bcc6e117c51b9988d5773f0e1a4457998b57225bc52c3cd8450849996c9427f8196a5f3d55862889c8'
6
+ metadata.gz: fc84a8ef5723964235e215ceaad244725dc882917a4475da3b5f8ca702aacf375b0d8bd8536fe297580a5a8be321367867332a3aff2839636ccb0113beaf8cba
7
+ data.tar.gz: 21daf4162a732639f0b8c5c0b41f9e09edf552db9a55aa632a8c0f082273074b54b2a588bab166804fc9079452340a0956baf98a790abaa1214553e85b16eeb5
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,38 @@ 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
+ when :dfs
34
+
35
+ read buffer
36
+
33
37
  when :file
34
-
38
+
35
39
  read buffer
36
-
40
+
37
41
  when :text
38
-
42
+
39
43
  @summary = {schema: s}
40
44
 
41
-
45
+
42
46
  when :url
43
-
44
- read buffer
45
-
47
+
48
+ read buffer
49
+
46
50
  end
47
-
48
- puts '@summary: ' + @summary.inspect
51
+
52
+ puts '@summary: ' + @summary.inspect if @debug
49
53
  @schema = @summary[:schema]
50
-
54
+
51
55
  summary_attributes = {
52
56
  recordx_type: 'dynarex',
53
57
  default_key: @schema[/(?<=\()\w+/]
@@ -57,131 +61,169 @@ class DxLite
57
61
  @summary.merge!(summary_attributes)
58
62
 
59
63
  summary = @summary[:schema][/(?<=\[)[^\]]+/]
60
-
64
+
61
65
  if summary then
62
66
 
63
67
  summary.split(/ *, */).each do |x|
64
68
  @summary[x] = nil unless @summary[x]
65
69
  end
66
-
67
- end
68
-
69
- make_methods()
70
70
 
71
- end
71
+ end
72
+
73
+ make_methods()
74
+
75
+ end
72
76
 
73
77
  def all()
74
78
 
75
79
  @records.map do |h|
76
-
80
+
77
81
  puts 'h: ' + h.inspect if @debug
78
- RecordX.new(h[:body], self, h[:id], h[:created],
82
+ RecordX.new(h[:body], self, h[:id], h[:created],
79
83
  h[:last_modified])
80
84
  end
81
85
 
82
- end
83
-
86
+ end
87
+
84
88
  def delete(id)
85
89
  found = @records.find {|x| x[:id] == id}
86
-
90
+
87
91
  if found then
88
- @records.delete found
92
+ @records.delete found
89
93
  save() if @autosave
90
94
  end
91
-
95
+
92
96
  end
93
-
97
+
94
98
  def create(rawh, id: nil, custom_attributes: {created: Time.now})
95
-
96
- id ||= @records.map {|x| x[:id].to_i}.max.to_i + 1
99
+
100
+ if @debug then
101
+ puts 'create:: rawh: ' + rawh.inspect
102
+ puts 'custom_attributes: ' + custom_attributes.inspect
103
+ end
104
+
105
+ key = @summary[:default_key]
106
+
107
+ if key then
108
+
109
+ r = records.find {|x| x[:body][key.to_sym] == rawh[key.to_sym]}
110
+ if r then
111
+ r[:last_modified] = Time.now.to_s
112
+ return false
113
+ end
114
+
115
+ end
116
+
117
+ if id.nil? then
118
+
119
+ puts '@records: ' + @records.inspect if @debug
120
+
121
+ if @records then
122
+ id = @records.map {|x| x[:id].to_i}.max.to_i + 1
123
+ else
124
+ @records = []
125
+ id = 1
126
+ end
127
+
128
+ end
129
+
97
130
  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
-
131
+
132
+ fields = rawh.keys
133
+ puts 'fields: ' + fields.inspect if @debug
134
+
135
+ h3 = fields.map {|x| [x.to_sym, nil] }.to_h.merge(rawh)
136
+ h = {id: id.to_s, created: h2[:created], last_modified: nil, body: h3}
137
+ @records << h
138
+
102
139
  save() if @autosave
140
+
141
+ RecordX.new(h[:body], self, h[:id], h[:created], h[:last_modified])
142
+
103
143
  end
104
-
144
+
105
145
  def fields()
106
146
  @fields
107
147
  end
108
-
148
+
109
149
  def inspect()
110
- "#<DxLite:%s @debug=%s, @summary=%s, ...>" % [object_id, @debug,
150
+ "#<DxLite:%s @debug=%s, @summary=%s, ...>" % [object_id, @debug,
111
151
  @summary.inspect]
112
152
  end
113
-
153
+
114
154
  # Parses 1 or more lines of text to create or update existing records.
115
155
 
116
156
  def parse(obj=nil)
117
-
157
+
118
158
  if obj.is_a? Array then
119
-
159
+
120
160
  unless schema() then
121
- cols = obj.first.keys.map {|c| c == 'id' ? 'uid' : c}
161
+ puts 'obj.first: ' + obj.first.inspect if @debug
162
+ cols = obj.first.keys.map {|c| c == 'id' ? 'uid' : c}
163
+ puts 'after cols' if @debug
122
164
  self.schema = "items/item(%s)" % cols.join(', ')
123
165
  end
124
-
166
+
125
167
  obj.each do |x|
126
168
  #puts 'x: ' + x.inspect if @debug
127
169
  self.create x, id: nil
128
170
  end
129
-
130
- return self
131
-
132
- end
171
+
172
+ return self
173
+
174
+ end
133
175
  end
134
-
176
+
135
177
  alias import parse
136
-
178
+
137
179
  def parse_xml(buffer)
138
-
180
+
139
181
  doc = Rexle.new(buffer)
140
-
182
+
141
183
  asummary = doc.root.xpath('summary/*').map do |node|
142
184
  puts 'node: ' + node.xml.inspect if @debug
143
185
  [node.name, node.text.to_s]
144
186
  end
145
-
187
+
146
188
  summary = Hash[asummary]
147
189
  summary[:schema] = summary['schema']
148
190
  %w(recordx_type format_mask schema).each {|x| summary.delete x}
149
-
191
+
150
192
  schema = summary[:schema]
151
193
  puts 'schema: ' + schema.inspect if @debug
152
-
194
+
153
195
  @fields = schema[/\(([^\)]+)/,1].split(/ *, +/)
154
196
  puts 'fields: ' + @fields.inspect if @debug
155
-
197
+
198
+ @summary = summary
199
+
156
200
  a = doc.root.xpath('records/*').each do |node|
157
-
201
+
158
202
  h = Hash[@fields.map {|field| [field.to_sym, node.text(field).to_s] }]
159
- self.create h, id: nil
160
-
203
+ self.create h, id: nil, custom_attributes: node.attributes
204
+
161
205
  end
162
206
 
163
- @summary = summary
164
-
165
207
  end
166
208
 
167
209
  def save(file=@filepath)
168
-
210
+
169
211
  return unless file
170
212
  @filepath = file
171
-
213
+
172
214
  s = File.extname(file) == '.json' ? to_json() : to_xml()
173
215
  File.write file, s
174
216
  end
175
-
217
+
176
218
  def to_a()
177
219
  @records.map {|x| x[:body]}
178
220
  end
179
-
221
+
180
222
  def to_h()
181
-
223
+
182
224
  root_name = schema()[/^\w+/]
183
225
  record_name = schema()[/(?<=\/)[^\(]+/]
184
-
226
+
185
227
  h = {
186
228
  root_name.to_sym =>
187
229
  {
@@ -189,137 +231,139 @@ class DxLite
189
231
  records: @records.map {|h| {record_name.to_sym => h} }
190
232
  }
191
233
  }
192
-
234
+
193
235
  end
194
-
236
+
195
237
  def to_json(pretty: true)
196
238
  pretty ? JSON.pretty_generate(to_h()) : to_h()
197
239
  end
198
-
240
+
199
241
  def to_xml()
200
-
242
+
201
243
  root_name = schema()[/^\w+/]
202
244
  record_name = schema()[/(?<=\/)[^\(]+/]
203
-
245
+
204
246
  a = RexleBuilder.build do |xml|
205
-
247
+
206
248
  xml.send(root_name.to_sym) do
207
249
  xml.summary({}, @summary)
208
250
  xml.records do
209
-
251
+
210
252
  all().each do |x|
211
-
212
- h = {id: x.id, created: x.created, last_modified: x.last_modified}
253
+
254
+ h = {id: x.id, created: x.created, last_modified: x.last_modified}
213
255
  puts 'x.to_h: ' + x.to_h.inspect if @debug
214
256
  xml.send(record_name.to_sym, h, x.to_h)
215
-
257
+
216
258
  end
217
-
259
+
218
260
  end
219
261
  end
220
262
  end
221
263
 
222
- Rexle.new(a).xml pretty: true
223
-
224
-
264
+ Rexle.new(a).xml pretty: true
265
+
266
+
225
267
  end
226
-
268
+
227
269
  # Updates a record from an id and a hash containing field name and field value.
228
- # dynarex.update 4, name: Jeff, age: 38
229
-
270
+ # dynarex.update 4, name: Jeff, age: 38
271
+
230
272
  def update(id, obj)
231
-
273
+
232
274
  if @debug then
233
275
  puts 'inside update'.info
234
276
  puts ('id: ' + id.inspect).debug
235
277
  puts ('obj.class: ' + obj.class.inspect).debug
236
278
  end
237
-
279
+
238
280
  r = @records.find {|x| x[:id] == id}
239
-
281
+
240
282
  if r then
241
-
283
+
242
284
  r[:body].merge!(obj)
243
285
  save() if @autosave
244
-
286
+
245
287
  end
246
-
288
+
247
289
  end
248
-
290
+
249
291
  private
250
-
292
+
251
293
  def find_record(rec, value, x)
252
- r = value.is_a?(Regexp) ? rec[x.to_sym] =~ value : rec[x.to_sym] == value
294
+ r = value.is_a?(Regexp) ? rec[x.to_sym] =~ value : rec[x.to_sym] == value
253
295
  end
254
-
296
+
255
297
  def make_methods()
256
-
298
+
257
299
  # for each summary item create get and set methods
258
-
300
+
259
301
  @summary.each do |key, value|
260
-
302
+
261
303
  define_singleton_method(key) { @summary[key] }
262
-
304
+
263
305
  define_singleton_method (key.to_s + '=').to_sym do |value|
264
306
  @summary[key] = value
265
- end
266
-
307
+ end
308
+
267
309
  end
268
-
310
+
269
311
  @fields = @summary[:schema][/(?<=\()[^\)]+/].split(',').map(&:strip)
270
312
 
271
313
  @fields.each do |x|
272
314
 
273
315
  define_singleton_method ('find_all_by_' + x).to_sym do |value|
274
-
316
+
275
317
  a = @records.select {|rec| find_record(rec[:body], value, x) }
276
-
318
+
277
319
  a.map do |h|
278
320
  RecordX.new(h[:body], self, h[:id], h[:created], h[:last_modified])
279
321
  end
280
-
322
+
281
323
  end
282
324
 
283
325
  define_singleton_method ('find_by_' + x).to_sym do |value|
284
-
326
+
285
327
  h = @records.find {|rec| find_record(rec[:body], value, x) }
328
+ return nil unless h
329
+
286
330
  RecordX.new(h[:body], self, h[:id], h[:created], h[:last_modified])
287
-
331
+
288
332
  end
289
333
 
290
334
  end
291
-
335
+
292
336
  end
293
-
337
+
294
338
  def read(buffer)
295
-
339
+
296
340
  if buffer[0] == '<' then
297
-
341
+
298
342
  parse_xml buffer
299
-
343
+
300
344
  else
301
-
345
+
302
346
  h1 = JSON.parse(buffer, symbolize_names: true)
303
- #puts 'h1:' + h1.inspect if @debug
304
-
347
+ puts 'h1:' + h1.inspect if @debug
348
+
305
349
  h = h1[h1.keys.first]
306
-
350
+
307
351
  @summary = {}
308
-
352
+
309
353
  h[:summary].each do |key, value|
310
-
354
+
311
355
  if %i(recordx_type format_mask schema).include? key then
312
356
  @summary[key] = value
313
- else
357
+ else
314
358
  @summary[key.to_s] = value
315
359
  end
316
-
360
+
317
361
  end
318
-
362
+
319
363
  @records = h[:records].map {|x| x[x.keys.first]}
320
-
364
+
321
365
  end
322
-
366
+
323
367
  end
324
-
368
+
325
369
  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.3
4
+ version: 0.4.1
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
- AaOBijCBhzAJBgNVHRMEAjAAMAsGA1UdDwQEAwIEsDAdBgNVHQ4EFgQUDodJ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xEujTuwTWyxmWAXnYft4ff2O1Zh2sPmdOlgf
36
+ camg7d8q+VZZzAtz0cFc4pip
37
37
  -----END CERTIFICATE-----
38
- date: 2021-01-29 00:00:00.000000000 Z
38
+ date: 2021-12-29 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