dxlite 0.2.5 → 0.3.2

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.tar.gz.sig +0 -0
  4. data/lib/dxlite.rb +156 -52
  5. metadata +10 -10
  6. metadata.gz.sig +0 -0
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 4224fc9854dcb1157666fca8b3a307f6e5228a7eb2a442ed4511b4a0df3c14fc
4
- data.tar.gz: aa9ff2cd93b0ef7cf9b46f135c541d9406de49b54fe1e7a11125e67cebc63ec8
3
+ metadata.gz: a11055ac19ac37f39d446b07bb28d1cc907a368e5c37a9535d59fe85d5c176a9
4
+ data.tar.gz: 2764eddbddbae683431653acd277bf32fcbbaa681ca0d6205619f0518c0bbc3a
5
5
  SHA512:
6
- metadata.gz: 2f175b4810c7a749170b43839d70bbfc06e925959401696780f91b11c086c2365ca1319f307f0f2f2fcee71a62e20265326f6fd68337b4774115041d11ad0b44
7
- data.tar.gz: 7903b9ef0094847037f8f12789ef0c6e4c5926e640f2c2660b0abe8fed1d8519ebcbbe936a7507f4fa5386ef24a185fc580203ef88a4c7905fd486c6e5e4caa8
6
+ metadata.gz: d05fee5c4205047d1dc7eb84cd30a148ad662f1ce5f68bc939cd6d8fa52f6fa8ed296a6b460c92b3205d55f66951aa961105df4b5540b158f6326022a2feed5f
7
+ data.tar.gz: 40bdd8815580e774d5068b8373e3bc2656eadd3b39a9f63c7d271bad412304a134d6957b817f7441c27c1ce47b34f6e447d3ce9b67870714b6b780f329edc38b
Binary file
data.tar.gz.sig CHANGED
Binary file
@@ -11,80 +11,62 @@ require 'rxfhelper'
11
11
  class DxLite
12
12
  using ColouredText
13
13
 
14
- attr_accessor :summary
14
+ attr_accessor :summary, :filepath
15
15
  attr_reader :records
16
16
 
17
- def initialize(s, filepath: nil, debug: false)
17
+ def initialize(s=nil, autosave: false, debug: false)
18
18
 
19
- @filepath, @debug = filepath, debug
19
+ @autosave, @debug = autosave, debug
20
20
 
21
+ return unless s
21
22
  buffer, type = RXFHelper.read(s)
22
- puts 'type: ' + type.inspect if @debug
23
23
 
24
+ @filepath = s if type == :file or type == :dfs
25
+
26
+ puts 'type: ' + type.inspect if @debug
27
+ puts 'buffer: ' + buffer.inspect if @debug
28
+
29
+ @records = []
30
+
24
31
  case type
25
32
 
26
33
  when :file
27
34
 
28
- h1 = JSON.parse(buffer, symbolize_names: true)
29
- puts 'h1:' + h1.inspect if @debug
30
- @filepath = s
31
-
32
- h = h1[h1.keys.first]
35
+ read buffer
33
36
 
34
- @summary = h[:summary]
35
- @records = h[:records].map {|x| x[x.keys.first]}
36
-
37
37
  when :text
38
38
 
39
39
  @summary = {schema: s}
40
- @records = []
40
+
41
+
42
+ when :url
43
+
44
+ read buffer
41
45
 
42
46
  end
43
47
 
48
+ puts '@summary: ' + @summary.inspect
44
49
  @schema = @summary[:schema]
45
50
 
46
51
  summary_attributes = {
47
52
  recordx_type: 'dynarex',
48
- default_key: @schema[/(?<=\()\w+/],
53
+ default_key: @schema[/(?<=\()\w+/]
49
54
  }
50
-
55
+
56
+ puts 'before merge' if @debug
51
57
  @summary.merge!(summary_attributes)
52
58
 
53
59
  summary = @summary[:schema][/(?<=\[)[^\]]+/]
54
60
 
55
61
  if summary then
56
- @summary.merge! summary.split(',').map {|x|[x.strip, nil] }.to_h
57
- end
58
-
59
- # for each summary item create get and set methods
60
-
61
- @summary.each do |key, value|
62
-
63
- define_singleton_method(key) { @summary[key] }
64
-
65
- define_singleton_method (key.to_s + '=').to_sym do |value|
66
- @summary[key] = value
67
- end
68
-
69
- end
70
-
71
- @fields = @summary[:schema][/(?<=\()[^\)]+/].split(',').map(&:strip)
72
-
73
- @fields.each do |x|
74
62
 
75
- define_singleton_method ('find_all_by_' + x).to_sym do |value|
76
-
77
- @records.select {|rec| find_record(rec[:body], value, x) }
78
-
79
- end
80
-
81
- define_singleton_method ('find_by_' + x).to_sym do |value|
82
-
83
- @records.find {|rec| find_record(rec[:body], value, x) }
84
-
63
+ summary.split(/ *, */).each do |x|
64
+ @summary[x] = nil unless @summary[x]
85
65
  end
86
-
87
- end
66
+
67
+ end
68
+
69
+ make_methods()
88
70
 
89
71
  end
90
72
 
@@ -101,14 +83,27 @@ class DxLite
101
83
 
102
84
  def delete(id)
103
85
  found = @records.find {|x| x[:id] == id}
104
- @records.delete found if found
86
+
87
+ if found then
88
+ @records.delete found
89
+ save() if @autosave
90
+ end
91
+
105
92
  end
106
93
 
107
- def create(h, id: nil, custom_attributes: {created: Time.now})
94
+ def create(rawh, id: nil, custom_attributes: {created: Time.now})
108
95
 
109
96
  id ||= @records.map {|x| x[:id].to_i}.max.to_i + 1
110
97
  h2 = custom_attributes
111
- @records << {id: id.to_s, created: h2[:created], last_modified: nil, body: h}
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
+
102
+ save() if @autosave
103
+ end
104
+
105
+ def fields()
106
+ @fields
112
107
  end
113
108
 
114
109
  def inspect()
@@ -128,7 +123,7 @@ class DxLite
128
123
  end
129
124
 
130
125
  obj.each do |x|
131
- puts 'x: ' + x.inspect if @debug
126
+ #puts 'x: ' + x.inspect if @debug
132
127
  self.create x, id: nil
133
128
  end
134
129
 
@@ -138,12 +133,49 @@ class DxLite
138
133
  end
139
134
 
140
135
  alias import parse
136
+
137
+ def parse_xml(buffer)
138
+
139
+ doc = Rexle.new(buffer)
140
+
141
+ asummary = doc.root.xpath('summary/*').map do |node|
142
+ puts 'node: ' + node.xml.inspect if @debug
143
+ [node.name, node.text.to_s]
144
+ end
145
+
146
+ summary = Hash[asummary]
147
+ summary[:schema] = summary['schema']
148
+ %w(recordx_type format_mask schema).each {|x| summary.delete x}
149
+
150
+ schema = summary[:schema]
151
+ puts 'schema: ' + schema.inspect if @debug
152
+
153
+ @fields = schema[/\(([^\)]+)/,1].split(/ *, +/)
154
+ puts 'fields: ' + @fields.inspect if @debug
155
+
156
+ a = doc.root.xpath('records/*').each do |node|
157
+
158
+ h = Hash[@fields.map {|field| [field.to_sym, node.text(field).to_s] }]
159
+ self.create h, id: nil
160
+
161
+ end
162
+
163
+ @summary = summary
164
+
165
+ end
141
166
 
142
167
  def save(file=@filepath)
143
- File.write file, to_json()
168
+
169
+ return unless file
170
+ @filepath = file
171
+
172
+ s = File.extname(file) == '.json' ? to_json() : to_xml()
173
+ File.write file, s
144
174
  end
145
175
 
146
- alias to_a records
176
+ def to_a()
177
+ @records.map {|x| x[:body]}
178
+ end
147
179
 
148
180
  def to_h()
149
181
 
@@ -204,7 +236,14 @@ class DxLite
204
236
  end
205
237
 
206
238
  r = @records.find {|x| x[:id] == id}
207
- r[:body].merge!(obj)
239
+
240
+ if r then
241
+
242
+ r[:body].merge!(obj)
243
+ save() if @autosave
244
+
245
+ end
246
+
208
247
  end
209
248
 
210
249
  private
@@ -213,4 +252,69 @@ class DxLite
213
252
  value.is_a?(Regexp) ? rec[x.to_sym] =~ value : rec[x.to_sym] == value
214
253
  end
215
254
 
255
+ def make_methods()
256
+
257
+ # for each summary item create get and set methods
258
+
259
+ @summary.each do |key, value|
260
+
261
+ define_singleton_method(key) { @summary[key] }
262
+
263
+ define_singleton_method (key.to_s + '=').to_sym do |value|
264
+ @summary[key] = value
265
+ end
266
+
267
+ end
268
+
269
+ @fields = @summary[:schema][/(?<=\()[^\)]+/].split(',').map(&:strip)
270
+
271
+ @fields.each do |x|
272
+
273
+ define_singleton_method ('find_all_by_' + x).to_sym do |value|
274
+
275
+ @records.select {|rec| find_record(rec[:body], value, x) }
276
+
277
+ end
278
+
279
+ define_singleton_method ('find_by_' + x).to_sym do |value|
280
+
281
+ @records.find {|rec| find_record(rec[:body], value, x) }
282
+
283
+ end
284
+
285
+ end
286
+
287
+ end
288
+
289
+ def read(buffer)
290
+
291
+ if buffer[0] == '<' then
292
+
293
+ parse_xml buffer
294
+
295
+ else
296
+
297
+ h1 = JSON.parse(buffer, symbolize_names: true)
298
+ #puts 'h1:' + h1.inspect if @debug
299
+
300
+ h = h1[h1.keys.first]
301
+
302
+ @summary = {}
303
+
304
+ h[:summary].each do |key, value|
305
+
306
+ if %i(recordx_type format_mask schema).include? key then
307
+ @summary[key] = value
308
+ else
309
+ @summary[key.to_s] = value
310
+ end
311
+
312
+ end
313
+
314
+ @records = h[:records].map {|x| x[x.keys.first]}
315
+
316
+ end
317
+
318
+ end
319
+
216
320
  end
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.2.5
4
+ version: 0.3.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - James Robertson
@@ -35,7 +35,7 @@ cert_chain:
35
35
  dwbAN6/wokoLVZAiMRG1vZlI6RhtSOTHvFHbfBE5JI9rhjRtui8yeV+t8iI8G4Zs
36
36
  2NszuYzdlVfzlrUiPWY5jL9P
37
37
  -----END CERTIFICATE-----
38
- date: 2020-06-22 00:00:00.000000000 Z
38
+ date: 2021-01-28 00:00:00.000000000 Z
39
39
  dependencies:
40
40
  - !ruby/object:Gem::Dependency
41
41
  name: recordx
@@ -61,22 +61,22 @@ dependencies:
61
61
  name: rxfhelper
62
62
  requirement: !ruby/object:Gem::Requirement
63
63
  requirements:
64
- - - ">="
65
- - !ruby/object:Gem::Version
66
- version: 1.0.0
67
64
  - - "~>"
68
65
  - !ruby/object:Gem::Version
69
- version: '1.0'
66
+ version: '1.1'
67
+ - - ">="
68
+ - !ruby/object:Gem::Version
69
+ version: 1.1.3
70
70
  type: :runtime
71
71
  prerelease: false
72
72
  version_requirements: !ruby/object:Gem::Requirement
73
73
  requirements:
74
- - - ">="
75
- - !ruby/object:Gem::Version
76
- version: 1.0.0
77
74
  - - "~>"
78
75
  - !ruby/object:Gem::Version
79
- version: '1.0'
76
+ version: '1.1'
77
+ - - ">="
78
+ - !ruby/object:Gem::Version
79
+ version: 1.1.3
80
80
  description:
81
81
  email: james@jamesrobertson.eu
82
82
  executables: []
metadata.gz.sig CHANGED
Binary file