dxlite 0.2.3 → 0.3.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.tar.gz.sig +0 -0
  4. data/lib/dxlite.rb +188 -53
  5. metadata +12 -11
  6. metadata.gz.sig +0 -0
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 6268d88624bc2c37694a40bcd196ef5653e67ee4f49c2e3aa57762e0c24eaf74
4
- data.tar.gz: ff5277f2debc53311cc5b4d59eebb04d568199ff28df0e77bf4a731ddc89408d
3
+ metadata.gz: 6638bca4c2fef3679ec9d456318f51b51eeeb49230afadbd1baa8af4308620ca
4
+ data.tar.gz: 1fcaedf6a4692a244e5930ed1d8ec10e7784b5003958faafb45b8e32cdaa707a
5
5
  SHA512:
6
- metadata.gz: 4b639ab519450eee10073783acdcf0ed5c6cdf3013c54fb5251e06dc30f85147629d90572de01e322861e16443542e26f124390f3616b47c327191aafbed78da
7
- data.tar.gz: 36fc53ce6d3bb42ee0c07bd072b19cd83a61a72445b63f3c8b8de98dd20dc061cca69e71e1987ae0ead27d23286a901e719807f473b9e0b8bb25795deec647a1
6
+ metadata.gz: 54a8938acfd2116234f3a750c9e007adae6e2bc7cb894379702aebe2730b8a601c88f178a490f4b260275e5682c211efee2c5c9da57b9fa645f5146b66a5c7d9
7
+ data.tar.gz: ec2fde50afa20bbea953fc3ffb503ae07f160580248ecde88e99f1c272f64404cf6bea099c75e4286f411f3c83df27516df39394c27e78d3bc1846e2fce2b88d
Binary file
data.tar.gz.sig CHANGED
Binary file
@@ -7,90 +7,98 @@ require 'json'
7
7
  require 'recordx'
8
8
  require 'rxfhelper'
9
9
 
10
+
10
11
  class DxLite
11
12
  using ColouredText
12
13
 
13
14
  attr_accessor :summary
14
15
  attr_reader :records
15
16
 
16
- def initialize(s, filepath: nil, debug: false)
17
+ def initialize(s=nil, filepath: nil, debug: false)
17
18
 
18
19
  @filepath, @debug = filepath, debug
19
20
 
21
+ return unless s
20
22
  buffer, type = RXFHelper.read(s)
21
23
  puts 'type: ' + type.inspect if @debug
22
-
24
+ puts 'buffer: ' + buffer.inspect if @debug
25
+
26
+ @records = []
27
+
23
28
  case type
24
29
 
25
30
  when :file
26
31
 
27
- h = JSON.parse(buffer, symbolize_names: true)
28
- @filepath = s
32
+ read buffer
29
33
 
30
- @summary = h[:summary]
31
- @records = h[:records]
32
-
33
34
  when :text
34
35
 
35
36
  @summary = {schema: s}
36
- @records = []
37
-
38
- end
39
-
40
- @schema = @summary[:schema]
41
37
 
42
- summary = @summary[:schema][/(?<=\[)[^\]]+/]
43
-
44
- if summary then
45
- @summary.merge! summary.split(',').map {|x|[x.strip, nil] }.to_h
46
- end
47
-
48
- # for each summary item create get and set methods
49
-
50
- @summary.each do |key, value|
51
38
 
52
- define_singleton_method(key) { @summary[key] }
39
+ when :url
53
40
 
54
- define_singleton_method (key.to_s + '=').to_sym do |value|
55
- @summary[key] = value
56
- end
41
+ read buffer
57
42
 
58
43
  end
59
44
 
60
- @fields = @summary[:schema][/(?<=\()[^\)]+/].split(',').map(&:strip)
45
+ puts '@summary: ' + @summary.inspect
46
+ @schema = @summary[:schema]
47
+
48
+ summary_attributes = {
49
+ recordx_type: 'dynarex',
50
+ default_key: @schema[/(?<=\()\w+/]
51
+ }
61
52
 
62
- @fields.each do |x|
53
+ puts 'before merge' if @debug
54
+ @summary.merge!(summary_attributes)
63
55
 
64
- define_singleton_method ('find_all_by_' + x).to_sym do |value|
65
-
66
- @records.select do |rec|
67
- value.is_a?(Regexp) ? rec[x.to_sym] =~ value : rec[x.to_sym] == value
68
- end
69
-
70
- end
56
+ summary = @summary[:schema][/(?<=\[)[^\]]+/]
57
+
58
+ if summary then
71
59
 
72
- define_singleton_method ('find_by_' + x).to_sym do |value|
73
-
74
- @records.find do |rec|
75
- value.is_a?(Regexp) ? rec[x.to_sym] =~ value : rec[x.to_sym] == value
76
- end
77
-
60
+ summary.split(/ *, */).each do |x|
61
+ @summary[x] = nil unless @summary[x]
78
62
  end
79
-
80
- end
63
+
64
+ end
65
+
66
+ make_methods()
81
67
 
82
68
  end
83
69
 
84
70
  def all()
85
71
 
86
72
  @records.map do |h|
87
- RecordX.new(h, self, h.object_id, h[:created], h[:last_modified])
73
+
74
+ puts 'h: ' + h.inspect if @debug
75
+ RecordX.new(h[:body], self, h[:id], h[:created],
76
+ h[:last_modified])
88
77
  end
89
78
 
90
79
  end
91
80
 
92
- def create(h)
93
- @records << h
81
+ def delete(id)
82
+ found = @records.find {|x| x[:id] == id}
83
+ @records.delete found if found
84
+ end
85
+
86
+ def create(rawh, id: nil, custom_attributes: {created: Time.now})
87
+
88
+ id ||= @records.map {|x| x[:id].to_i}.max.to_i + 1
89
+ h2 = custom_attributes
90
+ h = fields.map {|x| [x.to_sym, nil] }.to_h.merge(rawh)
91
+ @records << {id: id.to_s, created: h2[:created], last_modified: nil,
92
+ body: h}
93
+ end
94
+
95
+ def fields()
96
+ @fields
97
+ end
98
+
99
+ def inspect()
100
+ "#<DxLite:%s @debug=%s, @summary=%s, ...>" % [object_id, @debug,
101
+ @summary.inspect]
94
102
  end
95
103
 
96
104
  # Parses 1 or more lines of text to create or update existing records.
@@ -104,22 +112,59 @@ class DxLite
104
112
  self.schema = "items/item(%s)" % cols.join(', ')
105
113
  end
106
114
 
107
- obj.each {|x| self.create x }
115
+ obj.each do |x|
116
+ #puts 'x: ' + x.inspect if @debug
117
+ self.create x, id: nil
118
+ end
119
+
108
120
  return self
109
121
 
110
122
  end
111
123
  end
112
124
 
113
125
  alias import parse
126
+
127
+ def parse_xml(buffer)
128
+
129
+ doc = Rexle.new(buffer)
130
+
131
+ asummary = doc.root.xpath('summary/*').map do |node|
132
+ puts 'node: ' + node.xml.inspect if @debug
133
+ [node.name, node.text.to_s]
134
+ end
135
+
136
+ summary = Hash[asummary]
137
+ summary[:schema] = summary['schema']
138
+ %w(recordx_type format_mask schema).each {|x| summary.delete x}
139
+
140
+ schema = summary[:schema]
141
+ puts 'schema: ' + schema.inspect if @debug
142
+
143
+ @fields = schema[/\(([^\)]+)/,1].split(/ *, +/)
144
+ puts 'fields: ' + @fields.inspect if @debug
145
+
146
+ a = doc.root.xpath('records/*').each do |node|
147
+
148
+ h = Hash[@fields.map {|field| [field.to_sym, node.text(field).to_s] }]
149
+ self.create h, id: nil
150
+
151
+ end
152
+
153
+ @summary = summary
154
+
155
+ end
114
156
 
115
157
  def save(file=@filepath)
116
- File.write file, {summary: @summary, records: @records}.to_json
158
+ s = File.extname(file) == '.json' ? to_json() : to_xml()
159
+ File.write file, s
117
160
  end
118
161
 
119
- alias to_a records
162
+ def to_a()
163
+ @records.map {|x| x[:body]}
164
+ end
120
165
 
121
- def to_xml()
122
-
166
+ def to_h()
167
+
123
168
  root_name = schema()[/^\w+/]
124
169
  record_name = schema()[/(?<=\/)[^\(]+/]
125
170
 
@@ -130,8 +175,38 @@ class DxLite
130
175
  records: @records.map {|h| {record_name.to_sym => h} }
131
176
  }
132
177
  }
178
+
179
+ end
180
+
181
+ def to_json(pretty: true)
182
+ pretty ? JSON.pretty_generate(to_h()) : to_h()
183
+ end
184
+
185
+ def to_xml()
186
+
187
+ root_name = schema()[/^\w+/]
188
+ record_name = schema()[/(?<=\/)[^\(]+/]
189
+
190
+ a = RexleBuilder.build do |xml|
191
+
192
+ xml.send(root_name.to_sym) do
193
+ xml.summary({}, @summary)
194
+ xml.records do
195
+
196
+ all().each do |x|
197
+
198
+ h = {id: x.id, created: x.created, last_modified: x.last_modified}
199
+ puts 'x.to_h: ' + x.to_h.inspect if @debug
200
+ xml.send(record_name.to_sym, h, x.to_h)
201
+
202
+ end
203
+
204
+ end
205
+ end
206
+ end
133
207
 
134
- Rexle.new(RexleBuilder.new(h, debug: false).to_a).xml pretty: true
208
+ Rexle.new(a).xml pretty: true
209
+
135
210
 
136
211
  end
137
212
 
@@ -146,8 +221,68 @@ class DxLite
146
221
  puts ('obj.class: ' + obj.class.inspect).debug
147
222
  end
148
223
 
149
- r = @records.find {|x| x.object_id == id}
150
- r.merge!(obj)
224
+ r = @records.find {|x| x[:id] == id}
225
+ r[:body].merge!(obj)
226
+ end
227
+
228
+ private
229
+
230
+ def find_record(rec, value, x)
231
+ value.is_a?(Regexp) ? rec[x.to_sym] =~ value : rec[x.to_sym] == value
232
+ end
233
+
234
+ def make_methods()
235
+
236
+ # for each summary item create get and set methods
237
+
238
+ @summary.each do |key, value|
239
+
240
+ define_singleton_method(key) { @summary[key] }
241
+
242
+ define_singleton_method (key.to_s + '=').to_sym do |value|
243
+ @summary[key] = value
244
+ end
245
+
246
+ end
247
+
248
+ @fields = @summary[:schema][/(?<=\()[^\)]+/].split(',').map(&:strip)
249
+
250
+ @fields.each do |x|
251
+
252
+ define_singleton_method ('find_all_by_' + x).to_sym do |value|
253
+
254
+ @records.select {|rec| find_record(rec[:body], value, x) }
255
+
256
+ end
257
+
258
+ define_singleton_method ('find_by_' + x).to_sym do |value|
259
+
260
+ @records.find {|rec| find_record(rec[:body], value, x) }
261
+
262
+ end
263
+
264
+ end
265
+
266
+ end
267
+
268
+ def read(buffer)
269
+
270
+ if buffer[0] == '<' then
271
+
272
+ parse_xml buffer
273
+
274
+ else
275
+
276
+ h1 = JSON.parse(buffer, symbolize_names: true)
277
+ #puts 'h1:' + h1.inspect if @debug
278
+
279
+ h = h1[h1.keys.first]
280
+
281
+ @summary = h[:summary]
282
+ @records = h[:records].map {|x| x[x.keys.first]}
283
+
284
+ end
285
+
151
286
  end
152
287
 
153
288
  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.3
4
+ version: 0.3.0
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-23 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: []
@@ -103,7 +103,8 @@ required_rubygems_version: !ruby/object:Gem::Requirement
103
103
  - !ruby/object:Gem::Version
104
104
  version: '0'
105
105
  requirements: []
106
- rubygems_version: 3.0.3
106
+ rubyforge_project:
107
+ rubygems_version: 2.7.10
107
108
  signing_key:
108
109
  specification_version: 4
109
110
  summary: Handles Dynarex documents (in JSON format) faster and with less overheads.
metadata.gz.sig CHANGED
Binary file