Class: Dynarex

Inherits:
Object
  • Object
show all
Includes:
RXFReadWriteModule
Defined in:
lib/dynarex.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(rawx = nil, username: nil, password: nil, schema: nil, default_key: nil, json_out: true, debug: false, delimiter: ' # ', autosave: false, order: 'ascending', unique: false, filepath: nil) ⇒ Dynarex

Create a new dynarex document from 1 of the following options:

  • a local file path

  • a URL

  • a schema string

    Dynarex.new 'contacts[title,description]/contact(name,age,dob)'
    
  • an XML string

    Dynarex.new '<contacts><summary><schema>contacts/contact(name,age,dob)</schema></summary><records/></contacts>'
    


90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
# File 'lib/dynarex.rb', line 90

def initialize(rawx=nil, username: nil, password: nil, schema: nil, 
            default_key: nil, json_out: true, debug: false,
               delimiter: ' # ', autosave: false, order: 'ascending', 
               unique: false, filepath: nil)


  puts 'inside Dynarex::initialize' if debug
  @username, @password, @schema,  = username,  password, schema
  @default_key, @json_out, @debug = default_key, json_out, debug
  @autosave, @unique = autosave, unique
  @local_filepath = filepath
  
  puts ('@debug: ' + @debug.inspect).debug if debug
  @delimiter = delimiter
  @spaces_delimited = false
  @order = order
  @limit = nil
  @records, @flat_records = [], []
  rawx ||= schema if schema

  if rawx then
    
    return import(rawx) if rawx =~ /\.txt$/
    openx(rawx.clone)

  end
  
  self.order = @order unless @order.to_sym == :ascending

end

Instance Attribute Details

#delimiterObject

Returns the value of attribute delimiter.



78
79
80
# File 'lib/dynarex.rb', line 78

def delimiter
  @delimiter
end

#format_maskObject

Returns the value of attribute format_mask.



78
79
80
# File 'lib/dynarex.rb', line 78

def format_mask
  @format_mask
end

#json_outObject

Returns the value of attribute json_out.



78
79
80
# File 'lib/dynarex.rb', line 78

def json_out
  @json_out
end

#limitObject

Returns the value of attribute limit.



78
79
80
# File 'lib/dynarex.rb', line 78

def limit
  @limit
end

#linkedObject

Returns the value of attribute linked.



78
79
80
# File 'lib/dynarex.rb', line 78

def linked
  @linked
end

#orderObject

Returns the value of attribute order.



78
79
80
# File 'lib/dynarex.rb', line 78

def order
  @order
end

#schemaObject

Returns the value of attribute schema.



78
79
80
# File 'lib/dynarex.rb', line 78

def schema
  @schema
end

#typeObject

Returns the value of attribute type.



78
79
80
# File 'lib/dynarex.rb', line 78

def type
  @type
end

#uniqueObject

Returns the value of attribute unique.



78
79
80
# File 'lib/dynarex.rb', line 78

def unique
  @unique
end

#xsltObject

Returns the value of attribute xslt.



78
79
80
# File 'lib/dynarex.rb', line 78

def xslt
  @xslt
end

#xslt_schemaObject

Returns the value of attribute xslt_schema.



78
79
80
# File 'lib/dynarex.rb', line 78

def xslt_schema
  @xslt_schema
end

Instance Method Details

#add(x) ⇒ Object



121
122
123
124
125
# File 'lib/dynarex.rb', line 121

def add(x)
  @doc.root.add x
  @dirty_flag = true
  self
end

#allObject



127
128
129
130
131
132
133
# File 'lib/dynarex.rb', line 127

def all()
  
  refresh! if @dirty_flag
  a = @doc.root.xpath("records/*").map {|x| recordx_to_record x}
  DynarexRecordset.new(a, self)
  
end

#cloneObject



135
136
137
# File 'lib/dynarex.rb', line 135

def clone()
  Dynarex.new(self.to_xml)
end

#create(obj, id: nil, custom_attributes: {}) ⇒ Object

Create a record from a hash containing the field name, and the field value.

dynarex = Dynarex.new 'contacts/contact(name,age,dob)'
dynarex.create name: Bob, age: 52


554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
# File 'lib/dynarex.rb', line 554

def create(obj, id: nil, custom_attributes: {})

  puts 'inside create' if @debug
  raise 'Dynarex#create(): input error: no arg provided' unless obj
  
  case obj.class.to_s.downcase.to_sym    
  when :hash
    hash_create  obj, id, attr: custom_attributes
  when :string
    create_from_line obj, id, attr: custom_attributes
  when :recordx
    hash_create  obj.to_h, id || obj.id, attr: custom_attributes
  else
    hash_create  obj.to_h, id, attr: custom_attributes
  end

  @dirty_flag = true
  
  puts 'before save ' + @autosave.inspect if @debug
  save() if @autosave

  self
end

#create_from_line(line, id = nil, attr: '') ⇒ Object

Create a record from a string, given the dynarex document contains a format mask.

dynarex = Dynarex.new 'contacts/contact(name,age,dob)'
dynarex.create_from_line 'Tracy 37 15-Jun-1972'


582
583
584
585
586
587
588
589
590
591
# File 'lib/dynarex.rb', line 582

def create_from_line(line, id=nil, attr: '')
  t = @format_mask.to_s.gsub(/\[!(\w+)\]/, '(.*)').sub(/\[/,'\[')\
                                                              .sub(/\]/,'\]')
  line.match(/#{t}/).captures
  
  a = line.match(/#{t}/).captures
  h = Hash[@fields.zip(a)]
  create h
  self
end

#default_keyObject



139
140
141
# File 'lib/dynarex.rb', line 139

def default_key()
  self.summary[:default_key]
end

#default_key=(id) ⇒ Object



593
594
595
596
597
# File 'lib/dynarex.rb', line 593

def default_key=(id)
  @default_key = id.to_sym
  @summary[:default_key] = id
  @fields << id.to_sym
end

#delete(x) ⇒ Object

Delete a record.

dyarex.delete 3      # deletes record with id 3


635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
# File 'lib/dynarex.rb', line 635

def delete(x)    

  return x.each {|id| self.delete id} if x.is_a? Array

  if x.to_i.to_s == x.to_s and x[/[0-9]/] then
    @doc.root.delete("records/*[@id='#{x}']")
  else
    @doc.delete x
  end
  
  @dirty_flag = true
  save() if @autosave
  
  self
end

#docObject



162
163
164
165
# File 'lib/dynarex.rb', line 162

def doc
  (load_records; rebuild_doc) if @dirty_flag == true
  @doc
end

#element(x) ⇒ Object



651
652
653
# File 'lib/dynarex.rb', line 651

def element(x)
  @doc.root.element x
end

#fieldsObject



178
179
180
# File 'lib/dynarex.rb', line 178

def fields
  @fields
end

#filter(&blk) ⇒ Object



715
716
717
718
719
720
721
# File 'lib/dynarex.rb', line 715

def filter(&blk)
  
  dx = Dynarex.new @schema
  self.all.select(&blk).each {|x| dx.create x}
  dx
  
end

#firstObject



182
183
184
185
# File 'lib/dynarex.rb', line 182

def first
  r = @doc.root.element("records/*")
  r ? recordx_to_record(r) : nil  
end

#flat_records(select: nil) ⇒ Object Also known as: to_a

Returns a ready-only snapshot of records as a simple Hash.



251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
# File 'lib/dynarex.rb', line 251

def flat_records(select: nil)
  
  fields = select
  
  load_records if @dirty_flag == true

  if fields then
    
    case fields.class.to_s.downcase.to_sym
    when :string
      field = fields.to_sym
      @flat_records.map {|row| {field => row[field]}}
    when :symbol
      field = fields.to_sym
      @flat_records.map {|row| {field => row[field]} }
    when :array
      @flat_records.map {|row| fields.inject({})\
                         {|r,x| r.merge(x.to_sym => row[x.to_sym])}}
    end
    
  else
    @flat_records
  end
  
end

#foreign_import(options = {}) ⇒ Object

XML import



169
170
171
172
173
174
175
176
# File 'lib/dynarex.rb', line 169

def foreign_import(options={})
  o = {xml: '', schema: ''}.merge(options)
  h = {xml: o[:xml], schema: @schema, foreign_schema: o[:schema]}
  buffer = DynarexImport.new(h).to_xml

  openx(buffer)
  self
end

#insert(raw_params) ⇒ Object



192
193
194
195
196
# File 'lib/dynarex.rb', line 192

def insert(raw_params)
  record = make_record(raw_params)
  @doc.root.element('records/*').insert_before record
  @dirty_flag = true
end

#inspectObject



198
199
200
# File 'lib/dynarex.rb', line 198

def inspect()
  "<object #%s>" % [self.object_id]
end

#parse(x = nil) ⇒ Object Also known as: import

Parses 1 or more lines of text to create or update existing records.



519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
# File 'lib/dynarex.rb', line 519

def parse(x=nil)

  @dirty_flag = true
  
  if x.is_a? Array then
    
    unless @schema then
      cols = x.first.keys.map {|c| c == 'id' ? 'uid' : c} 
      self.schema = "items/item(%s)" % cols.join(', ')
    end
      
    x.each {|record| self.create record }
    return self 
    
  end
  raw_buffer, type = RXFReader.read(x, auto: false)

  if raw_buffer.is_a? String then

    buffer = block_given? ? yield(raw_buffer) : raw_buffer.clone
    string_parse buffer.force_encoding('UTF-8')
    
  else
    foreign_import x
  end
  
end

#record(id) ⇒ Object Also known as: find, find_by_id



665
666
667
668
# File 'lib/dynarex.rb', line 665

def record(id)
  e = @doc.root.element("records/*[@id='#{id}']")    
  recordx_to_record e if e
end

#record_exists?(id) ⇒ Boolean

Returns:

  • (Boolean)


673
674
675
# File 'lib/dynarex.rb', line 673

def record_exists?(id)
  !@doc.root.element("records/*[@id='#{id}']").nil?
end

#recordsObject

Return a Hash (which can be edited) containing all records.



235
236
237
238
239
240
241
242
243
244
245
246
247
# File 'lib/dynarex.rb', line 235

def records
  
  load_records if @dirty_flag == true
  
  if block_given? then
    yield(@records)
    rebuild_doc
    @dirty_flag = true 
  else
    @records
  end
  
end

#recordx_typeObject



213
214
215
# File 'lib/dynarex.rb', line 213

def recordx_type()
  @summary[:recordx_type]
end

#refreshObject



677
678
679
# File 'lib/dynarex.rb', line 677

def refresh()
  @dirty_flag = true
end

#refresh!Object



681
682
683
# File 'lib/dynarex.rb', line 681

def refresh!()
  (load_records; rebuild_doc) if @dirty_flag == true    
end

#rm(force: false) ⇒ Object



285
286
287
288
289
290
291
292
293
294
# File 'lib/dynarex.rb', line 285

def rm(force: false)
  
  if force or all.empty? then
    FileX.rm @local_filepath if @local_filepath
    'file ' + @local_filepath + ' deleted'
  else
    'unable to rm file: document not empty'
  end
  
end

#ro_recordsObject

Returns an array snapshot of OpenStruct records



281
282
283
# File 'lib/dynarex.rb', line 281

def ro_records
  flat_records.map {|record| OpenStruct.new record }
end

#rss_xslt(opt = {}) ⇒ Object

used internally by to_rss()



687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
# File 'lib/dynarex.rb', line 687

def rss_xslt(opt={})

  h = {limit: 11}.merge(opt)
  doc = Rexle.new(self.to_xslt)
  e = doc.element('//xsl:apply-templates[2]')
  
  e2 = doc.root.element('xsl:template[3]')
  item = e2.element('item')
  new_item = item.deep_clone
  item.delete
  
  pubdate = @xslt_schema[/pubDate:/]
  xslif = Rexle.new("<xsl:if test='position() &lt; #{h[:limit]}'/>").root

  if pubdate.nil? then
    pubdate = Rexle.new("<pubDate><xsl:value-of select='pubDate'>" + \
                            "</xsl:value-of></pubDate>").root
    new_item.add pubdate      
  end

  xslif.add new_item      
  e2.add xslif.root
  xslt = doc.xml      

  xslt    
  
end

#save(filepath = @local_filepath, options = {}) ⇒ Object Also known as: write

Save the document to a file.



490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
# File 'lib/dynarex.rb', line 490

def save(filepath=@local_filepath, options={})
  
  if @debug then
    puts 'inside Dynarex::save' 
    puts 'filepath: '  + filepath.inspect

  end
  
  return unless filepath
  
  opt = {pretty: true}.merge options

  @local_filepath = filepath || 'dx.xml'
  xml = display_xml(opt)
  buffer = block_given? ? yield(xml) : xml
  
  if @debug then
    puts 'before write; filepath: ' + filepath.inspect
    puts 'buffer: ' + buffer.inspect
  end
  
  FileX.write filepath, buffer
  FileX.write(filepath.sub(/\.xml$/,'.json'), self.to_json) if @json_out
end

#sort_by!(field = nil, &element_blk) ⇒ Object



655
656
657
658
659
660
661
662
# File 'lib/dynarex.rb', line 655

def sort_by!(field=nil, &element_blk)

  blk = field ? ->(x){ x.text(field.to_s).to_s} : element_blk
  r = sort_records_by! &blk
  @dirty_flag = true    
  r

end

#summaryObject

Returns the hash representation of the document summary.



229
230
231
# File 'lib/dynarex.rb', line 229

def summary
  @summary
end

#to_docObject



297
298
299
# File 'lib/dynarex.rb', line 297

def to_doc  
  self.clone().doc
end

#to_hObject

Typically uses the 1st field as a key and the remaining fields as the value



303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
# File 'lib/dynarex.rb', line 303

def to_h()
  
  key = self.default_key.to_sym
  fields = self.fields() - [key]
  puts 'fields: ' + fields.inspect if @debug
  
  flat_records.inject({}) do |r, h| 
    
    puts 'h: ' + h.inspect if @debug
    
    value = if h.length == 2 then
      h[fields[0]].to_s
    else
      fields.map {|x| h[x]}        
    end
    
    r.merge(h[key] => value)
  end
  
end

#to_html(domain: '') ⇒ Object



324
325
326
327
328
329
330
# File 'lib/dynarex.rb', line 324

def to_html(domain: '')

  h = {username: @username, password: @password}
  xsl_buffer = RXFReader.read(domain + @xslt, h).first
  Rexslt.new(xsl_buffer, self.to_doc).to_s

end

#to_json(pretty: true) ⇒ Object

to_json: pretty is set to true because although the file size is larger, the file can be load slightly quicker



336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
# File 'lib/dynarex.rb', line 336

def to_json(pretty: true)
  
  records = self.to_a
  summary = self.summary.to_h
          
  root_name = schema()[/^\w+/]
  record_name = schema()[/(?<=\/)[^\(]+/]
  
  h = {
    root_name.to_sym =>
    {
      summary: @summary,
      records: @records.map {|_, h| {record_name.to_sym => h} }        
    }
  }
  
  pretty ? JSON.pretty_generate(h) : h.to_json
     
end

#to_rss(opt = {}, xslt = nil) {|doc| ... } ⇒ Object

Yields:



735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
# File 'lib/dynarex.rb', line 735

def to_rss(opt={}, xslt=nil)
  
  puts 'inside to_rss'.info if @debug
  puts 'xslt: ' + xslt.inspect if @debug

  unless xslt then
          
    h = {limit: 11}.merge(opt)
    puts 'self.to_xslt:' + self.to_xslt.inspect if @debug
    doc = Rexle.new(self.to_xslt)
    puts 'doc: ' + doc.xml if @debug
    puts 'after xslt doc' if @debug
    e = doc.element('//xsl:apply-templates[2]')

    e2 = doc.root.element('xsl:template[3]')
    puts 'e2:' + e2.inspect if @debug
    item = e2.element('item')
    puts 'after item' if @debug
    new_item = item.deep_clone
    item.delete
    
    pubdate = @xslt_schema[/pubDate:/]

    xslif = Rexle.new("<xsl:if test='position() &lt; #{h[:limit]}'/>").root
    
    
    if pubdate.nil? then
      pubdate2 = Rexle.new("<pubDate><xsl:value-of select='pubDate'></xsl:value-of></pubDate>").root
      new_item.add pubdate2      
    end

    xslif.add new_item      
    e2.add xslif
    xslt = doc.xml      

    xslt
  end
  
  puts 'before self.to_xml' if @debug
  doc = Rexle.new(self.to_xml)
  
  puts ('pubdate: ' + pubdate.inspect).debug if @debug
  
  if pubdate.nil? then
    doc.root.xpath('records/*').each do |x|
      raw_dt = DateTime.parse x.attributes[:created]
      dt = raw_dt.strftime("%a, %d %b %Y %H:%M:%S %z")
      x.add Rexle::Element.new('pubDate').add_text dt.to_s 
    end
  end

  #puts ('doc: ' + doc.root.xml) if @debug
  #File.write '/tmp/blog.xml', doc.root.xml
  #puts ('xslt:'  + xslt.inspect) if @debug
  #File.write '/tmp/blog.xslt', xslt
  
  puts 'before Rexslt' if @debug
  out = Rexslt.new(xslt, doc).to_s(declaration: false)
  puts 'after Rexslt' if @debug

  #Rexle.new("<rss version='2.0'>%s</rss>" % xml).xml(pretty: true)

  doc = Rexle.new("<rss version='2.0'>%s</rss>" % out.to_s)
  yield( doc ) if block_given?
  puts 'before doc.xml' if @debug
  xml = doc.xml(pretty: true)
  xml
end

#to_s(header: true, delimiter: @delimiter) ⇒ Object



356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
# File 'lib/dynarex.rb', line 356

def to_s(header: true, delimiter: @delimiter)

xsl_buffer =<<EOF
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
<xsl:output encoding="UTF-8"
          method="text"
          indent="no"
          omit-xml-declaration="yes"/>

<xsl:template match="*">
  <xsl:for-each select="records/*">[!regex_values]<xsl:text>
</xsl:text>
  </xsl:for-each>
</xsl:template>
</xsl:stylesheet>
EOF


  raw_summary_fields = self.summary[:schema][/^\w+\[([^\]]+)\]/,1]
  sumry = ''
  
  if raw_summary_fields then
    summary_fields = raw_summary_fields.split(',') # .map(&:to_sym) 
    sumry = summary_fields.map {|x| x.strip!; x + ': ' + \
                             self.summary[x.to_sym].to_s}.join("\n") + "\n\n"
  end
  
  if @raw_header then
    declaration = @raw_header
  else

    smry_fields = %i(schema)              
    smry_fields << :order if self.summary[:order] == 'descending'
    
    if delimiter.length > 0 then
      smry_fields << :delimiter 
    else
      smry_fields << :format_mask unless self.summary[:rawdoc_type] == 'rowx'
    end
    s = smry_fields.map {|x| "%s=\"%s\"" % \
      [x, self.send(x).gsub('"', '\"') ]}.join ' '

    declaration = %Q(<?dynarex %s?>\n) % s
  end

  docheader = declaration + sumry

  if self.summary[:rawdoc_type] == 'rowx' then
    a = self.fields.map do |field|
"<xsl:if test=\"%s != ''\">
<xsl:text>\n</xsl:text>%s:<xsl:text> </xsl:text><xsl:value-of select='%s'/>
</xsl:if>" % ([field]*3)
    end
    
    puts ('a: ' + a.inspect).debug if @debug

    xslt_format = a.join      

    xsl_buffer.sub!(/\[!regex_values\]/, xslt_format)
    
    if @debug then
      File.write '/tmp/foo.xsl', xsl_buffer
      File.write '/tmp/foo.xml', @doc.xml
      puts 'xsl_buffer: ' + xsl_buffer.inspect
    end
    
    out = Rexslt.new(xsl_buffer, @doc).to_s
    
    docheader + "\n--+\n" + out
  elsif self.summary[:rawdoc_type] == 'sectionx' then  
    
    a = (self.fields - [:uid, 'uid']).map do |field|
"<xsl:if test=\"%s != ''\">
<xsl:text>\n</xsl:text><xsl:value-of select='%s'/>
</xsl:if>" % ([field]*2)
    end

    xslt_format = a.join      

    xsl_buffer.sub!(/\[!regex_values\]/, xslt_format)
    puts 'xsl_buffer: ' + xsl_buffer.inspect if @debug

    out = Rexslt.new(xsl_buffer, @doc).to_s
    
    header ? docheader + "--#\n" + out : out
    
  elsif self.delimiter.length > 0 then
    puts 'dinddd'
    tfo = TableFormatter.new border: false, wrap: false, \
                                                divider: self.delimiter
    tfo.source = self.to_a.map{|x| x.values}      
    docheader + tfo.display.strip

  else

    format_mask = self.format_mask
    format_mask.gsub!(/\[[^!\]]+\]/) {|x| x[1] }

    s1, s2 = '<xsl:text>', '</xsl:text>'
    xslt_format = s1 + format_mask\
        .gsub(/(?:\[!(\w+)\])/, s2 + '<xsl:value-of select="\1"/>' + s1) + s2
      
    xsl_buffer.sub!(/\[!regex_values\]/, xslt_format)

    puts 'xsl_buffer: ' + xsl_buffer if @debug
    out = Rexslt.new(xsl_buffer, @doc).to_s

    header ? docheader + "\n" + out : out
  end

end

#to_table(fields: nil, markdown: false, innermarkdown: false, heading: true) ⇒ Object



468
469
470
471
472
473
474
475
476
477
478
479
480
481
# File 'lib/dynarex.rb', line 468

def to_table(fields: nil, markdown: false, innermarkdown: false, heading: true)
  
  tfo = TableFormatter.new markdown: markdown, innermarkdown: innermarkdown
  tfo.source = self.to_a.map {|h| fields ? fields.map {|x| h[x]} : h.values }

  if heading then
    raw_headings = self.summary[:headings]
    fields = raw_headings.split(self.delimiter) if raw_headings and fields.nil?
    tfo.labels = (fields ? fields : self.fields.map{|x| x.to_s.capitalize })
  end
  
  tfo
  
end

#to_xml(opt = {}) ⇒ Object



483
484
485
486
# File 'lib/dynarex.rb', line 483

def to_xml(opt={}) 
  opt = {pretty: true} if opt == :pretty
  display_xml(opt)
end

#to_xslt(opt = {}) ⇒ Object



723
724
725
726
727
728
729
730
731
732
733
# File 'lib/dynarex.rb', line 723

def to_xslt(opt={})    

  h = {limit: -1}.merge(opt)
  @xslt_schema = @xslt_schema || self.summary[:xslt_schema]
  raise 'to_xslt(): xslt_schema nil' unless @xslt_schema
  puts 'to_xslt: ' + [@schema, @xslt_schema].inspect if @debug

  xslt = DynarexXSLT.new(schema: @schema, xslt_schema: @xslt_schema ).to_xslt

  return xslt
end

#update(id, obj) ⇒ Object

Updates a record from an id and a hash containing field name and field value.

dynarex.update 4, name: Jeff, age: 38


603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
# File 'lib/dynarex.rb', line 603

def update(id, obj)

  params = if obj.is_a? Hash then
    obj
  elsif obj.is_a? RecordX
    obj.to_h
  end
  
  fields = capture_fields(params)

  # for each field update each record field
  record = @doc.root.element("records/#{@record_name}[@id='#{id.to_s}']")    

  fields.each do |k,v|
    puts "updating ... %s = '%s'" % [k,v] if @debug
    record.element(k.to_s).text = v if v
  end
  
  record.add_attribute(last_modified: Time.now.to_s)

  @dirty_flag = true

  save() if @autosave
  
  self

end

#xpath(x) ⇒ Object



810
811
812
# File 'lib/dynarex.rb', line 810

def xpath(x)
  @doc.root.xpath x
end