dxlite 0.2.1 → 0.2.6

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 +114 -15
  5. metadata +8 -8
  6. metadata.gz.sig +0 -0
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: e036712cdb12ebc28440edcfff5e89a9d7e3cd4bc92c5eeca4545b2158b3ad8d
4
- data.tar.gz: '020807c1206d1388f60fc55facbe865fc2be92ee9ca9c4d8d4af8741073492cc'
3
+ metadata.gz: 4e41d21c786667b36de7cfd0411d9ca2cac88e69d79f19e9e1aaef453cd8c298
4
+ data.tar.gz: 23d18cda9ff1bd35797370937726b2a20aa4b67a76f12fcbf3f5c4b2c88bc405
5
5
  SHA512:
6
- metadata.gz: 33fabd7df1cbd07065b2627c4c6a56941b6ba68812b86ae3672fba34bc8f0ed06afc940af162217ed34385337f310b4154e5c900cea4d2cb7fbc8f12339be048
7
- data.tar.gz: f16f8874284397716679de4b6f6a7e79e6145767b46d12fca47aca0895a1221ce592fd4e38bf0c8d5751fc81a3bf1a3fa4a978e50eb6b6c63e8e606de6b427d3
6
+ metadata.gz: 2eb01b1e8d31818db54860679979a4401b248d58cf027b505344b98bcebcd21076b8c7d1473fbe1f3870c70a18ad24a99da469c461aba04b6e5f1677e318168d
7
+ data.tar.gz: 5e08527479ad8d18a3fa9deecdfc1960042bcd4f8aa74bbc642cdd08c4b7ec15b8f0f876de42abaf5b56cfc87b82e827198c563f48e159a877ed315196c21237
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
 
@@ -62,11 +72,15 @@ class DxLite
62
72
  @fields.each do |x|
63
73
 
64
74
  define_singleton_method ('find_all_by_' + x).to_sym do |value|
65
- @records.select {|rec| rec[x.to_sym] == value }
75
+
76
+ @records.select {|rec| find_record(rec[:body], value, x) }
77
+
66
78
  end
67
79
 
68
80
  define_singleton_method ('find_by_' + x).to_sym do |value|
69
- @records.find {|rec| rec[x.to_sym] == value }
81
+
82
+ @records.find {|rec| find_record(rec[:body], value, x) }
83
+
70
84
  end
71
85
 
72
86
  end
@@ -76,13 +90,29 @@ class DxLite
76
90
  def all()
77
91
 
78
92
  @records.map do |h|
79
- 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])
80
97
  end
81
98
 
82
99
  end
83
100
 
84
- def create(h)
85
- @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]
86
116
  end
87
117
 
88
118
  # Parses 1 or more lines of text to create or update existing records.
@@ -96,7 +126,11 @@ class DxLite
96
126
  self.schema = "items/item(%s)" % cols.join(', ')
97
127
  end
98
128
 
99
- 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
+
100
134
  return self
101
135
 
102
136
  end
@@ -105,11 +139,58 @@ class DxLite
105
139
  alias import parse
106
140
 
107
141
  def save(file=@filepath)
108
- File.write file, {summary: @summary, records: @records}.to_json
142
+ File.write file, to_json()
109
143
  end
110
144
 
111
145
  alias to_a records
112
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
+
113
194
  # Updates a record from an id and a hash containing field name and field value.
114
195
  # dynarex.update 4, name: Jeff, age: 38
115
196
 
@@ -121,8 +202,26 @@ class DxLite
121
202
  puts ('obj.class: ' + obj.class.inspect).debug
122
203
  end
123
204
 
124
- r = @records.find {|x| x.object_id == id}
125
- 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
+
126
225
  end
127
226
 
128
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.1
4
+ version: 0.2.6
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-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
66
  version: '1.0'
67
+ - - ">="
68
+ - !ruby/object:Gem::Version
69
+ version: 1.0.2
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
76
  version: '1.0'
77
+ - - ">="
78
+ - !ruby/object:Gem::Version
79
+ version: 1.0.2
80
80
  description:
81
81
  email: james@jamesrobertson.eu
82
82
  executables: []
metadata.gz.sig CHANGED
Binary file