dynarex 1.1.1 → 1.1.2

Sign up to get free protection for your applications and to get access to all the features.
Files changed (2) hide show
  1. data/lib/dynarex.rb +49 -33
  2. metadata +2 -2
@@ -25,10 +25,11 @@ class Dynarex
25
25
  def initialize(location=nil)
26
26
  @delimiter = ' '
27
27
  open(location) if location
28
+
28
29
  end
29
30
 
30
31
  def add(x)
31
- @doc.add x
32
+ @doc.root.add x
32
33
  load_records
33
34
  self
34
35
  end
@@ -42,6 +43,7 @@ class Dynarex
42
43
  o = {xml: '', schema: ''}.merge(options)
43
44
  h = {xml: o[:xml], schema: @schema, foreign_schema: o[:schema]}
44
45
  buffer = DynarexImport.new(h).to_xml
46
+
45
47
  open(buffer)
46
48
  self
47
49
  end
@@ -100,17 +102,22 @@ xsl_buffer =<<EOF
100
102
  </xsl:stylesheet>
101
103
  EOF
102
104
 
103
- #format_mask = XPath.first(@doc.root, 'summary/format_mask/text()').to_s
104
- format_mask = @doc.element('summary/format_mask/text()')
105
105
 
106
+ #format_mask = XPath.first(@doc.root, 'summary/format_mask/text()').to_s
107
+ format_mask = @doc.root.element('summary/format_mask/text()')
106
108
  xslt_format = format_mask.to_s.gsub(/\s(?=\[!\w+\])/,'<xsl:text> </xsl:text>').gsub(/\[!(\w+)\]/, '<xsl:value-of select="\1"/>')
109
+
107
110
  xsl_buffer.sub!(/\[!regex_values\]/, xslt_format)
108
111
 
109
- # jr 211111 the following statement has not yet been tested.
110
- Rexslt.new(xsl_buffer, @doc.to_s).to_s
112
+ #jr250711 xslt = Nokogiri::XSLT(xsl_buffer)
113
+ #jr250711 out = xslt.transform(Nokogiri::XML(@doc.to_s))
114
+ #jr250811 puts 'xsl_buffer: ' + xsl_buffer
115
+ #jr250811 puts 'doc_to_s: ' + @doc.to_s
116
+ #jr260711 out.text
117
+ #jr231211 Rexslt.new(xsl_buffer, @doc.to_s).to_s
111
118
 
112
119
  end
113
-
120
+
114
121
  def to_xml()
115
122
  display_xml()
116
123
  end
@@ -144,7 +151,7 @@ EOF
144
151
  load_records
145
152
  self
146
153
  end
147
-
154
+
148
155
  #Create a record from a string, given the dynarex document contains a format mask.
149
156
  # dynarex = Dynarex.new 'contacts/contact(name,age,dob)'
150
157
  # dynarex.create_from_line 'Tracy 37 15-Jun-1972'
@@ -167,7 +174,7 @@ EOF
167
174
 
168
175
 
169
176
  # for each field update each record field
170
- record = @doc.element("records/#{@record_name}[@id='#{id.to_s}']")
177
+ record = @doc.root.element("records/#{@record_name}[@id='#{id.to_s}']")
171
178
  fields.each {|k,v| record.element(k.to_s).text = v if v}
172
179
  record.add_attribute(last_modified: Time.now.to_s)
173
180
 
@@ -177,6 +184,8 @@ EOF
177
184
 
178
185
  end
179
186
 
187
+
188
+
180
189
  #Delete a record.
181
190
  # dyarex.delete 3 # deletes record with id 3
182
191
 
@@ -192,16 +201,18 @@ EOF
192
201
  end
193
202
 
194
203
  def element(x)
195
- @doc.element x
204
+ @doc.root.element x
196
205
  end
197
206
 
198
207
  def sort_by!(&element_blk)
199
208
  refresh_doc
200
- a = @doc.xpath('records/*').sort_by &element_blk
201
- @doc.delete('records')
209
+ a = @doc.root.xpath('records/*').sort_by &element_blk
210
+ @doc.root.delete('records')
202
211
 
203
212
  records = Rexle::Element.new 'records'
213
+
204
214
  a.each {|record| records.add record}
215
+
205
216
  @doc.root.add records
206
217
 
207
218
  load_records
@@ -209,15 +220,15 @@ EOF
209
220
  end
210
221
 
211
222
  def record(id)
212
- recordx_to_record @doc.element("records/*[@id='#{id}']")
223
+ recordx_to_record @doc.root.element("records/*[@id='#{id}']")
213
224
  end
214
225
 
215
226
  def record_exists?(id)
216
- !@doc.element("records/*[@id='#{id}']").nil?
227
+ !@doc.root.element("records/*[@id='#{id}']").nil?
217
228
  end
218
229
 
219
230
  def xpath(x)
220
- @doc.xpath x
231
+ @doc.root.xpath x
221
232
  end
222
233
 
223
234
  private
@@ -232,12 +243,12 @@ EOF
232
243
  end
233
244
 
234
245
  def findx_by(field, value)
235
- r = @doc.element("records/*[#{field}='#{value}']")
246
+ r = @doc.root.element("records/*[#{field}='#{value}']")
236
247
  r ? recordx_to_record(r) : nil
237
248
  end
238
249
 
239
250
  def findx_all_by(field, value)
240
- @doc.xpath("records/*[#{field}='#{value}']").map {|x| recordx_to_record x}
251
+ @doc.root.xpath("records/*[#{field}='#{value}']").map {|x| recordx_to_record x}
241
252
  end
242
253
 
243
254
  def recordx_to_record(recordx)
@@ -248,21 +259,25 @@ EOF
248
259
 
249
260
  params = Hash[raw_params.keys.map(&:to_sym).zip(raw_params.values)]
250
261
 
251
- fields = capture_fields(params)
252
-
262
+ fields = capture_fields(params)
253
263
  record = Rexle::Element.new @record_name
264
+
254
265
  fields.each do |k,v|
255
266
  element = Rexle::Element.new(k.to_s)
256
267
  element.text = v if v
257
- record.add element
268
+ record.add element if record
258
269
  end
259
270
 
260
- id = (@doc.xpath('max(records/*/attribute::id)') || '0').succ unless id
271
+ #jr250811 puts 'id : ' + @doc.root.xpath("max(records/*/attribute::id)").inspect
272
+ #jr250811 puts '@doc ' + @doc.xml
273
+ #jr250811 puts 'company ' + @doc.root.xpath("records/company/@id").inspect
274
+ #jr250811 puts
275
+ id = (@doc.root.xpath('max(records/*/attribute::id)') || '0').succ unless id
276
+ #jr250811 puts 'id2 : ' + id.inspect
261
277
 
262
278
  attributes = {id: id, created: Time.now.to_s, last_modified: nil}
263
279
  attributes.each {|k,v| record.add_attribute(k, v)}
264
-
265
- @doc.element('records').add record
280
+ @doc.root.element('records').add record
266
281
 
267
282
  end
268
283
 
@@ -303,7 +318,6 @@ EOF
303
318
  end
304
319
 
305
320
  @doc = Rexle.new a
306
-
307
321
  end
308
322
 
309
323
  alias refresh_doc display_xml
@@ -317,7 +331,7 @@ EOF
317
331
  end
318
332
 
319
333
  # if records already exist find the max id
320
- i = @doc.xpath('max(records/*/attribute::id)').to_i
334
+ i = @doc.root.xpath('max(records/*/attribute::id)').to_i
321
335
 
322
336
 
323
337
  # 'a' and 'a_split' just used for validation
@@ -417,14 +431,16 @@ EOF
417
431
  end
418
432
 
419
433
 
434
+ #@doc = Rexle.new buffer
435
+
420
436
  @doc = Rexle.new(buffer) unless @doc
421
437
 
422
438
  @schema = @doc.root.text('summary/schema')
423
439
  @root_name = @doc.root.name
424
-
425
440
  @summary = summary_to_h
426
- @default_key = @doc.element('summary/default_key/text()')
427
- @format_mask = @doc.element('summary/format_mask/text()')
441
+
442
+ @default_key = @doc.root.element('summary/default_key/text()')
443
+ @format_mask = @doc.root.element('summary/format_mask/text()')
428
444
 
429
445
  @fields = @format_mask.to_s.scan(/\[!(\w+)\]/).flatten.map(&:to_sym) if @format_mask
430
446
 
@@ -438,11 +454,11 @@ EOF
438
454
  # load the record query handler methods
439
455
  attach_record_methods
440
456
  else
441
- @default_key = @doc.xpath('records/*/*').first.name
457
+ @default_key = @doc.root.xpath('records/*/*').first.name
442
458
  end
443
459
 
444
- if @doc.xpath('records/*').length > 0 then
445
- @record_name = @doc.element('records/*[1]').name
460
+ if @doc.root.xpath('records/*').length > 0 then
461
+ @record_name = @doc.root.element('records/*[1]').name
446
462
  load_records
447
463
  end
448
464
 
@@ -466,9 +482,9 @@ EOF
466
482
 
467
483
  def records_to_h()
468
484
 
469
- i = @doc.xpath('max(records/*/attribute::id)') || 0
485
+ i = @doc.root.xpath('max(records/*/attribute::id)') || 0
470
486
 
471
- @doc.xpath('records/*').inject({}) do |result,row|
487
+ @doc.root.xpath('records/*').inject({}) do |result,row|
472
488
 
473
489
  created = Time.now.to_s
474
490
  last_modified = ''
@@ -492,7 +508,7 @@ EOF
492
508
 
493
509
  def summary_to_h
494
510
 
495
- @doc.xpath('summary/*').inject({}) do |r,node|
511
+ @doc.root.xpath('summary/*').inject({}) do |r,node|
496
512
  r.merge node.name.to_s.to_sym => node.text.to_s
497
513
  end
498
514
  end
metadata CHANGED
@@ -2,7 +2,7 @@
2
2
  name: dynarex
3
3
  version: !ruby/object:Gem::Version
4
4
  prerelease:
5
- version: 1.1.1
5
+ version: 1.1.2
6
6
  platform: ruby
7
7
  authors:
8
8
  - James Robertson
@@ -10,7 +10,7 @@ autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
12
 
13
- date: 2011-11-21 00:00:00 +00:00
13
+ date: 2011-12-25 00:00:00 +00:00
14
14
  default_executable:
15
15
  dependencies:
16
16
  - !ruby/object:Gem::Dependency