dxlite 0.2.4 → 0.3.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 +3 -1
  3. data.tar.gz.sig +2 -1
  4. data/lib/dxlite.rb +149 -55
  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: 17e3592686fa48db902f98d0d7dce8ed09b6bca4d2f42b36994bb940d47b5fd4
4
- data.tar.gz: 6b2cb9ae5b94c25880b369e6bd558ca02005564bfaa96a810532963faba6fabd
3
+ metadata.gz: 90e750d486323e8a58475cb5471ce9724fb1e74c723aade42c9ed8e8b8eeb146
4
+ data.tar.gz: 1fa49e26bd6b74fcbc68cfde7a7ff38746adb1227de26bd7241ff2c494eb2127
5
5
  SHA512:
6
- metadata.gz: 241329fd8c70eb330cd70ea488c25f6fdbcc6ca0a062b3a3f3ce40bc43b65bb0207d4a25ce9ba8184ad457ebd574bbc4ff7025a6095d9d231eff6184b99ea80c
7
- data.tar.gz: d57fac1b246db2e6915a4f8d4ce64341aba08a9e683a7a26d1692a13828756bafc75995664fde5369ff8e0ec9aafb7bf2ec50588c15ea6ded2c9d9b37e149ee6
6
+ metadata.gz: 7fc7a7fe58be371ba3ae9120cba0cad446917c55c8e5e438ebe9be4b74eefca0f2bf36b48813683d406fda740c45b1de9aed0b773a1815c624344b8d978f6bc7
7
+ data.tar.gz: c82bde55b5410360b3e2114750e24bf8ce6b7747913b6e9dbcc6e38e47549407171ea836cfcc251d0025d6a91bd6b8c160109711d69687e2d975898fbdf57fe7
@@ -1 +1,3 @@
1
- �����b,�l"�~�b8 |��\^+��JKK�ĸlR�C��|���`V�sв�"�kn��,4�#a���gh�D��S��#�9��N�8J�·ʥ���e[E�� �� r���K�˩u���u�׆&�b7��+(����/�uyl�����/�:�;C?�+8"�/P���~�F��xџ�������u� `T�v��"/�#s)��e4u����,i��.�zvA��#
1
+ &�ar��r��]k����N��f'Vh'a�T��=��A}�h������J��>U���
2
+ �F�>ǭU�D��� d�ߛl2�|�2ukE�uQ�)��{���7���Kʵ%�3���D۠o��{h4)���w�J��}K���v?�u��� %u?��x�|���u.���ֱ֏S�(�?iȺ���B�z G�7�U���A)9w'@��JJ̞j
3
+ Aq /��'L֗ͯ�R� X��e�� ����rʛ2A=:q2,������lD6$EYz.f��eL�z]��2e��ѫQ6��ҠJ*������P��bPW��c��q����]FcD�(ʗ ��l��?�
data.tar.gz.sig CHANGED
@@ -1 +1,2 @@
1
- 0�����8������s#��K�g�LJ� �Yc����/6�Nē֜.h������ە�!�`�Q&!�9@נFrz�+���F%��,8Pޡ����h���"��k�t/煕nF񍸓Z —S#G�%\�.������qS�Z��[TL�ѭR�"f��Ti�|�Gbѻ$���f�����˦�P=�\��zy�&��ĺ�˗��Cz�/C��,�����$7����hl�7���K'�k-M�@�r�XH�lp�����(yj@E�eDD�7����tl�h8Θ����q@[2��(��ęk�>*d��p/����c�ܭ��R�m��<�9���iĞ#s坍S�� pW�\%:�>��.h���O�� � -�G��
1
+ ~3�����N��;�q���t�p55��s�Qs��C>��#�v���Hhj~VV96ð���az�﷋#�!��:-�y���ScbD
2
+ yr.�_x��|O29��w4�0�<?�¥��v�J�J�a���ե��Ӭ�މ����t��x�ק�3��T�s�F�t~,��l�Po@��L�����ֈA�N�C~�Tلz"�k������L�����sU�Y;��xM����ff��>�E��Y)���<e���9J�?�
@@ -14,81 +14,59 @@ class DxLite
14
14
  attr_accessor :summary
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]
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 do |rec|
78
- value.is_a?(Regexp) ? rec[x.to_sym] =~ value : rec[x.to_sym] == value
79
- end
80
-
63
+ summary.split(/ *, */).each do |x|
64
+ @summary[x] = nil unless @summary[x]
81
65
  end
82
-
83
- define_singleton_method ('find_by_' + x).to_sym do |value|
84
-
85
- @records.find do |rec|
86
- value.is_a?(Regexp) ? rec[x.to_sym] =~ value : rec[x.to_sym] == value
87
- end
88
-
89
- end
90
-
91
- end
66
+
67
+ end
68
+
69
+ make_methods()
92
70
 
93
71
  end
94
72
 
@@ -105,14 +83,27 @@ class DxLite
105
83
 
106
84
  def delete(id)
107
85
  found = @records.find {|x| x[:id] == id}
108
- @records.delete found if found
86
+
87
+ if found then
88
+ @records.delete found
89
+ save() if @autosave
90
+ end
91
+
109
92
  end
110
93
 
111
- def create(h, id: nil, custom_attributes: {created: Time.now})
94
+ def create(rawh, id: nil, custom_attributes: {created: Time.now})
112
95
 
113
96
  id ||= @records.map {|x| x[:id].to_i}.max.to_i + 1
114
97
  h2 = custom_attributes
115
- @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
116
107
  end
117
108
 
118
109
  def inspect()
@@ -132,7 +123,7 @@ class DxLite
132
123
  end
133
124
 
134
125
  obj.each do |x|
135
- puts 'x: ' + x.inspect if @debug
126
+ #puts 'x: ' + x.inspect if @debug
136
127
  self.create x, id: nil
137
128
  end
138
129
 
@@ -142,12 +133,48 @@ class DxLite
142
133
  end
143
134
 
144
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
145
166
 
146
167
  def save(file=@filepath)
147
- File.write file, to_json()
168
+
169
+ return unless file
170
+
171
+ s = File.extname(file) == '.json' ? to_json() : to_xml()
172
+ File.write file, s
148
173
  end
149
174
 
150
- alias to_a records
175
+ def to_a()
176
+ @records.map {|x| x[:body]}
177
+ end
151
178
 
152
179
  def to_h()
153
180
 
@@ -208,7 +235,74 @@ class DxLite
208
235
  end
209
236
 
210
237
  r = @records.find {|x| x[:id] == id}
211
- r[:body].merge!(obj)
238
+
239
+ if r then
240
+
241
+ r[:body].merge!(obj)
242
+ save() if @autosave
243
+
244
+ end
245
+
246
+ end
247
+
248
+ private
249
+
250
+ def find_record(rec, value, x)
251
+ value.is_a?(Regexp) ? rec[x.to_sym] =~ value : rec[x.to_sym] == value
252
+ end
253
+
254
+ def make_methods()
255
+
256
+ # for each summary item create get and set methods
257
+
258
+ @summary.each do |key, value|
259
+
260
+ define_singleton_method(key) { @summary[key] }
261
+
262
+ define_singleton_method (key.to_s + '=').to_sym do |value|
263
+ @summary[key] = value
264
+ end
265
+
266
+ end
267
+
268
+ @fields = @summary[:schema][/(?<=\()[^\)]+/].split(',').map(&:strip)
269
+
270
+ @fields.each do |x|
271
+
272
+ define_singleton_method ('find_all_by_' + x).to_sym do |value|
273
+
274
+ @records.select {|rec| find_record(rec[:body], value, x) }
275
+
276
+ end
277
+
278
+ define_singleton_method ('find_by_' + x).to_sym do |value|
279
+
280
+ @records.find {|rec| find_record(rec[:body], value, x) }
281
+
282
+ end
283
+
284
+ end
285
+
286
+ end
287
+
288
+ def read(buffer)
289
+
290
+ if buffer[0] == '<' then
291
+
292
+ parse_xml buffer
293
+
294
+ else
295
+
296
+ h1 = JSON.parse(buffer, symbolize_names: true)
297
+ #puts 'h1:' + h1.inspect if @debug
298
+
299
+ h = h1[h1.keys.first]
300
+
301
+ @summary = h[:summary]
302
+ @records = h[:records].map {|x| x[x.keys.first]}
303
+
304
+ end
305
+
212
306
  end
213
307
 
214
308
  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.4
4
+ version: 0.3.1
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-27 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