dxlite 0.2.0 → 0.2.5

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
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 +108 -15
  5. metadata +4 -3
  6. metadata.gz.sig +0 -0
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 1e0e8b4f265fe12323cca22407f3b06ce13f090192fb0abd83588aa4bd2cf13b
4
- data.tar.gz: e9b725095dde2698fbd7a6afed799fec375f0d4009901b2021bfad73371ad0cf
3
+ metadata.gz: 4224fc9854dcb1157666fca8b3a307f6e5228a7eb2a442ed4511b4a0df3c14fc
4
+ data.tar.gz: aa9ff2cd93b0ef7cf9b46f135c541d9406de49b54fe1e7a11125e67cebc63ec8
5
5
  SHA512:
6
- metadata.gz: 439ffafee850ff7f07dd8915099c97cc24f4f9914780ce03231a57f200b43a734c3ebafeaa49d21f0b850a25bd5ce34b505e669bfac55bd911ea26c343313e6f
7
- data.tar.gz: 370a4daca23b52c6a0b2e06c9abcd653cb061e14d93dfd4195250f41cb8c6b220261388f9812ee977bfe09963c37f7a1d3ef3fc7f20b9dc110f2fccfcdbedd0d
6
+ metadata.gz: 2f175b4810c7a749170b43839d70bbfc06e925959401696780f91b11c086c2365ca1319f307f0f2f2fcee71a62e20265326f6fd68337b4774115041d11ad0b44
7
+ data.tar.gz: 7903b9ef0094847037f8f12789ef0c6e4c5926e640f2c2660b0abe8fed1d8519ebcbbe936a7507f4fa5386ef24a185fc580203ef88a4c7905fd486c6e5e4caa8
Binary file
data.tar.gz.sig CHANGED
Binary file
@@ -7,15 +7,16 @@ 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, debug: false)
17
+ def initialize(s, filepath: nil, debug: false)
17
18
 
18
- @debug = debug
19
+ @filepath, @debug = filepath, debug
19
20
 
20
21
  buffer, type = RXFHelper.read(s)
21
22
  puts 'type: ' + type.inspect if @debug
@@ -24,11 +25,14 @@ class DxLite
24
25
 
25
26
  when :file
26
27
 
27
- h = JSON.parse(buffer, symbolize_names: true)
28
+ h1 = JSON.parse(buffer, symbolize_names: true)
29
+ puts 'h1:' + h1.inspect if @debug
28
30
  @filepath = s
29
31
 
32
+ h = h1[h1.keys.first]
33
+
30
34
  @summary = h[:summary]
31
- @records = h[:records]
35
+ @records = h[:records].map {|x| x[x.keys.first]}
32
36
 
33
37
  when :text
34
38
 
@@ -38,9 +42,19 @@ class DxLite
38
42
  end
39
43
 
40
44
  @schema = @summary[:schema]
45
+
46
+ summary_attributes = {
47
+ recordx_type: 'dynarex',
48
+ default_key: @schema[/(?<=\()\w+/],
49
+ }
50
+
51
+ @summary.merge!(summary_attributes)
41
52
 
42
- @summary.merge! @summary[:schema][/(?<=\[)[^\]]+/].split(',')\
43
- .map {|x|[x.strip, nil] }.to_h
53
+ summary = @summary[:schema][/(?<=\[)[^\]]+/]
54
+
55
+ if summary then
56
+ @summary.merge! summary.split(',').map {|x|[x.strip, nil] }.to_h
57
+ end
44
58
 
45
59
  # for each summary item create get and set methods
46
60
 
@@ -59,11 +73,15 @@ class DxLite
59
73
  @fields.each do |x|
60
74
 
61
75
  define_singleton_method ('find_all_by_' + x).to_sym do |value|
62
- @records.select {|rec| rec[x.to_sym] == value }
76
+
77
+ @records.select {|rec| find_record(rec[:body], value, x) }
78
+
63
79
  end
64
80
 
65
81
  define_singleton_method ('find_by_' + x).to_sym do |value|
66
- @records.find {|rec| rec[x.to_sym] == value }
82
+
83
+ @records.find {|rec| find_record(rec[:body], value, x) }
84
+
67
85
  end
68
86
 
69
87
  end
@@ -73,13 +91,29 @@ class DxLite
73
91
  def all()
74
92
 
75
93
  @records.map do |h|
76
- RecordX.new(h, self, h.object_id, h[:created], h[:last_modified])
94
+
95
+ puts 'h: ' + h.inspect if @debug
96
+ RecordX.new(h[:body], self, h[:id], h[:created],
97
+ h[:last_modified])
77
98
  end
78
99
 
79
100
  end
80
101
 
81
- def create(h)
82
- @records << h
102
+ def delete(id)
103
+ found = @records.find {|x| x[:id] == id}
104
+ @records.delete found if found
105
+ end
106
+
107
+ def create(h, id: nil, custom_attributes: {created: Time.now})
108
+
109
+ id ||= @records.map {|x| x[:id].to_i}.max.to_i + 1
110
+ h2 = custom_attributes
111
+ @records << {id: id.to_s, created: h2[:created], last_modified: nil, body: h}
112
+ end
113
+
114
+ def inspect()
115
+ "#<DxLite:%s @debug=%s, @summary=%s, ...>" % [object_id, @debug,
116
+ @summary.inspect]
83
117
  end
84
118
 
85
119
  # Parses 1 or more lines of text to create or update existing records.
@@ -93,7 +127,11 @@ class DxLite
93
127
  self.schema = "items/item(%s)" % cols.join(', ')
94
128
  end
95
129
 
96
- obj.each {|x| self.create x }
130
+ obj.each do |x|
131
+ puts 'x: ' + x.inspect if @debug
132
+ self.create x, id: nil
133
+ end
134
+
97
135
  return self
98
136
 
99
137
  end
@@ -102,7 +140,56 @@ class DxLite
102
140
  alias import parse
103
141
 
104
142
  def save(file=@filepath)
105
- File.write file, @records.to_json
143
+ File.write file, to_json()
144
+ end
145
+
146
+ alias to_a records
147
+
148
+ def to_h()
149
+
150
+ root_name = schema()[/^\w+/]
151
+ record_name = schema()[/(?<=\/)[^\(]+/]
152
+
153
+ h = {
154
+ root_name.to_sym =>
155
+ {
156
+ summary: @summary,
157
+ records: @records.map {|h| {record_name.to_sym => h} }
158
+ }
159
+ }
160
+
161
+ end
162
+
163
+ def to_json(pretty: true)
164
+ pretty ? JSON.pretty_generate(to_h()) : to_h()
165
+ end
166
+
167
+ def to_xml()
168
+
169
+ root_name = schema()[/^\w+/]
170
+ record_name = schema()[/(?<=\/)[^\(]+/]
171
+
172
+ a = RexleBuilder.build do |xml|
173
+
174
+ xml.send(root_name.to_sym) do
175
+ xml.summary({}, @summary)
176
+ xml.records do
177
+
178
+ all().each do |x|
179
+
180
+ h = {id: x.id, created: x.created, last_modified: x.last_modified}
181
+ puts 'x.to_h: ' + x.to_h.inspect if @debug
182
+ xml.send(record_name.to_sym, h, x.to_h)
183
+
184
+ end
185
+
186
+ end
187
+ end
188
+ end
189
+
190
+ Rexle.new(a).xml pretty: true
191
+
192
+
106
193
  end
107
194
 
108
195
  # Updates a record from an id and a hash containing field name and field value.
@@ -116,8 +203,14 @@ class DxLite
116
203
  puts ('obj.class: ' + obj.class.inspect).debug
117
204
  end
118
205
 
119
- r = @records.find {|x| x.object_id == id}
120
- r.merge!(obj)
206
+ r = @records.find {|x| x[:id] == id}
207
+ r[:body].merge!(obj)
208
+ end
209
+
210
+ private
211
+
212
+ def find_record(rec, value, x)
213
+ value.is_a?(Regexp) ? rec[x.to_sym] =~ value : rec[x.to_sym] == value
121
214
  end
122
215
 
123
216
  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.0
4
+ version: 0.2.5
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-21 00:00:00.000000000 Z
38
+ date: 2020-06-22 00:00:00.000000000 Z
39
39
  dependencies:
40
40
  - !ruby/object:Gem::Dependency
41
41
  name: recordx
@@ -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