dxlite 0.2.2 → 0.2.7

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 +110 -19
  5. metadata +2 -2
  6. metadata.gz.sig +0 -0
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: bf76970719d297956cab4003755b984e7b7849a9f0f8dbc61c7e99ce20ca96d8
4
- data.tar.gz: acd9d7cd693d355134ca1c33863203f8991cc3b42b988ae91e4449c8d6174a82
3
+ metadata.gz: 6258ec8d0f416cd7236549ab79736b74d4acd4a452af8fd5d1cc87332a56d90b
4
+ data.tar.gz: bfb040a5d78c81ea0e1a0461783e3d89f9603b5360387c5a1d9305da515abd0a
5
5
  SHA512:
6
- metadata.gz: 11fba990077d4ae039878410b876a0d61d8a57c9dfe40333b68ce5b50ad0d4e88b698b3d33711d9a899807cf3194a6e29b6d08aff8c53f2cfae7bf5dbfab6b67
7
- data.tar.gz: 32142c4b0ec6ef0db19daa9f807b6723b5087aac6665abbb7979a4c39e05ff196691a745fcd439c57d780e45d2edf3e1dd54b8db1a3d2629570896df21997107
6
+ metadata.gz: 6f80a21604960566b022c4adca5ce6f53d5514d746551498802037291287df866833de858f6d069236e191ace3cee225d4f059288495ed9ba3194967d4acde9c
7
+ data.tar.gz: 41e9874cb37039d9464d2a1e299b95c0415d46a8bb2d64bfda3b0175c0038e2067fb5e76b25b980b1dae89df5f711c9c6e227417b77d230de79b7db1c67aaf5a
Binary file
data.tar.gz.sig CHANGED
Binary file
@@ -7,6 +7,7 @@ require 'json'
7
7
  require 'recordx'
8
8
  require 'rxfhelper'
9
9
 
10
+
10
11
  class DxLite
11
12
  using ColouredText
12
13
 
@@ -19,25 +20,34 @@ class DxLite
19
20
 
20
21
  buffer, type = RXFHelper.read(s)
21
22
  puts 'type: ' + type.inspect if @debug
22
-
23
+ puts 'buffer: ' + buffer.inspect if @debug
24
+
23
25
  case type
24
26
 
25
27
  when :file
26
28
 
27
- h = JSON.parse(buffer, symbolize_names: true)
28
- @filepath = s
29
+ read buffer
29
30
 
30
- @summary = h[:summary]
31
- @records = h[:records]
32
-
33
31
  when :text
34
32
 
35
33
  @summary = {schema: s}
36
34
  @records = []
37
35
 
36
+ when :url
37
+
38
+ read buffer
39
+
38
40
  end
39
41
 
42
+
40
43
  @schema = @summary[:schema]
44
+
45
+ summary_attributes = {
46
+ recordx_type: 'dynarex',
47
+ default_key: @schema[/(?<=\()\w+/],
48
+ }
49
+
50
+ @summary.merge!(summary_attributes)
41
51
 
42
52
  summary = @summary[:schema][/(?<=\[)[^\]]+/]
43
53
 
@@ -63,17 +73,13 @@ class DxLite
63
73
 
64
74
  define_singleton_method ('find_all_by_' + x).to_sym do |value|
65
75
 
66
- @records.select do |rec|
67
- value.is_a?(Regexp) ? rec[x.to_sym] =~ value : rec[x.to_sym] == value
68
- end
76
+ @records.select {|rec| find_record(rec[:body], value, x) }
69
77
 
70
78
  end
71
79
 
72
80
  define_singleton_method ('find_by_' + x).to_sym do |value|
73
81
 
74
- @records.find do |rec|
75
- value.is_a?(Regexp) ? rec[x.to_sym] =~ value : rec[x.to_sym] == value
76
- end
82
+ @records.find {|rec| find_record(rec[:body], value, x) }
77
83
 
78
84
  end
79
85
 
@@ -84,13 +90,29 @@ class DxLite
84
90
  def all()
85
91
 
86
92
  @records.map do |h|
87
- RecordX.new(h, self, h.object_id, h[:created], h[:last_modified])
93
+
94
+ puts 'h: ' + h.inspect if @debug
95
+ RecordX.new(h[:body], self, h[:id], h[:created],
96
+ h[:last_modified])
88
97
  end
89
98
 
90
99
  end
91
100
 
92
- def create(h)
93
- @records << h
101
+ def delete(id)
102
+ found = @records.find {|x| x[:id] == id}
103
+ @records.delete found if found
104
+ end
105
+
106
+ def create(h, id: nil, custom_attributes: {created: Time.now})
107
+
108
+ id ||= @records.map {|x| x[:id].to_i}.max.to_i + 1
109
+ h2 = custom_attributes
110
+ @records << {id: id.to_s, created: h2[:created], last_modified: nil, body: h}
111
+ end
112
+
113
+ def inspect()
114
+ "#<DxLite:%s @debug=%s, @summary=%s, ...>" % [object_id, @debug,
115
+ @summary.inspect]
94
116
  end
95
117
 
96
118
  # Parses 1 or more lines of text to create or update existing records.
@@ -104,7 +126,11 @@ class DxLite
104
126
  self.schema = "items/item(%s)" % cols.join(', ')
105
127
  end
106
128
 
107
- obj.each {|x| self.create x }
129
+ obj.each do |x|
130
+ puts 'x: ' + x.inspect if @debug
131
+ self.create x, id: nil
132
+ end
133
+
108
134
  return self
109
135
 
110
136
  end
@@ -113,11 +139,58 @@ class DxLite
113
139
  alias import parse
114
140
 
115
141
  def save(file=@filepath)
116
- File.write file, {summary: @summary, records: @records}.to_json
142
+ File.write file, to_json()
117
143
  end
118
144
 
119
145
  alias to_a records
120
146
 
147
+ def to_h()
148
+
149
+ root_name = schema()[/^\w+/]
150
+ record_name = schema()[/(?<=\/)[^\(]+/]
151
+
152
+ h = {
153
+ root_name.to_sym =>
154
+ {
155
+ summary: @summary,
156
+ records: @records.map {|h| {record_name.to_sym => h} }
157
+ }
158
+ }
159
+
160
+ end
161
+
162
+ def to_json(pretty: true)
163
+ pretty ? JSON.pretty_generate(to_h()) : to_h()
164
+ end
165
+
166
+ def to_xml()
167
+
168
+ root_name = schema()[/^\w+/]
169
+ record_name = schema()[/(?<=\/)[^\(]+/]
170
+
171
+ a = RexleBuilder.build do |xml|
172
+
173
+ xml.send(root_name.to_sym) do
174
+ xml.summary({}, @summary)
175
+ xml.records do
176
+
177
+ all().each do |x|
178
+
179
+ h = {id: x.id, created: x.created, last_modified: x.last_modified}
180
+ puts 'x.to_h: ' + x.to_h.inspect if @debug
181
+ xml.send(record_name.to_sym, h, x.to_h)
182
+
183
+ end
184
+
185
+ end
186
+ end
187
+ end
188
+
189
+ Rexle.new(a).xml pretty: true
190
+
191
+
192
+ end
193
+
121
194
  # Updates a record from an id and a hash containing field name and field value.
122
195
  # dynarex.update 4, name: Jeff, age: 38
123
196
 
@@ -129,8 +202,26 @@ class DxLite
129
202
  puts ('obj.class: ' + obj.class.inspect).debug
130
203
  end
131
204
 
132
- r = @records.find {|x| x.object_id == id}
133
- r.merge!(obj)
205
+ r = @records.find {|x| x[:id] == id}
206
+ r[:body].merge!(obj)
207
+ end
208
+
209
+ private
210
+
211
+ def find_record(rec, value, x)
212
+ value.is_a?(Regexp) ? rec[x.to_sym] =~ value : rec[x.to_sym] == value
213
+ end
214
+
215
+ def read(buffer)
216
+
217
+ h1 = JSON.parse(buffer, symbolize_names: true)
218
+ puts 'h1:' + h1.inspect if @debug
219
+
220
+ h = h1[h1.keys.first]
221
+
222
+ @summary = h[:summary]
223
+ @records = h[:records].map {|x| x[x.keys.first]}
224
+
134
225
  end
135
226
 
136
227
  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.2
4
+ version: 0.2.7
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: 2020-06-23 00:00:00.000000000 Z
39
39
  dependencies:
40
40
  - !ruby/object:Gem::Dependency
41
41
  name: recordx
metadata.gz.sig CHANGED
Binary file