dynarex 0.3.0 → 0.3.1

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 (2) hide show
  1. data/lib/dynarex.rb +45 -11
  2. metadata +2 -2
@@ -7,10 +7,7 @@ require 'builder'
7
7
  class Dynarex
8
8
  include REXML
9
9
 
10
- def initialize(opt)
11
- o = {xml: '', default_key: ''}.merge opt
12
- location = o[:xml]
13
- @default_key = o[:default_key]
10
+ def initialize(location)
14
11
  open(location)
15
12
  end
16
13
 
@@ -30,6 +27,40 @@ class Dynarex
30
27
  xml = display_xml()
31
28
  File.open(filepath,'w'){|f| f.write xml}
32
29
  end
30
+
31
+ def parse(buffer)
32
+ format_mask = XPath.first(@doc.root, 'summary/format_mask/text()').to_s
33
+ t = format_mask.to_s.gsub(/\[!(\w+)\]/, '(.*)').sub(/\[/,'\[').sub(/\]/,'\]')
34
+ lines = buffer.split(/\r?\n|\r(?!\n)/).map {|x|x.match(/#{t}/).captures}
35
+ fields = format_mask.scan(/\[!(\w+)\]/).flatten.map(&:to_sym)
36
+
37
+ a = lines.each_with_index.map do|x,i|
38
+ created = Time.now.to_s; id = Time.now.strftime("%Y%m%I%H%M%S") + i.to_s
39
+ h = Hash[fields.zip(x)]
40
+ [h[@default_key], {id: id, created: created, last_modified: '', body: h}]
41
+ end
42
+
43
+ h2 = Hash[a]
44
+
45
+ #replace the existing records hash
46
+ h = @records
47
+ h2.each do |key,item|
48
+ if h.has_key? key then
49
+
50
+ # overwrite the previous item and change the timestamps
51
+ h[key][:last_modified] = item[:created]
52
+ item[:body].each do |k,v|
53
+ h[key][:body][k.to_sym] = v
54
+ end
55
+ else
56
+
57
+ h[key] = item.clone
58
+ end
59
+ end
60
+
61
+ h.each {|key, item| h.delete(key) if not h2.has_key? key}
62
+
63
+ end
33
64
 
34
65
  private
35
66
 
@@ -45,8 +76,8 @@ class Dynarex
45
76
  xml.records do
46
77
  @records.each do |k, item|
47
78
  xml.send(@item_name, id: item[:id], created: item[:created], \
48
- last_modified: item[:last_modified]) do
49
- item[:body].each{|name,value| xml.send name, value}
79
+ last_modified: item[:last_modified]) do
80
+ item[:body].each{|name,value| xml.send name, value}
50
81
  end
51
82
  end
52
83
  end
@@ -65,8 +96,11 @@ class Dynarex
65
96
  buffer = File.open(location,'r').read
66
97
  end
67
98
  @doc = Document.new buffer
68
- @summary = summary_to_h
69
- @records = records_to_h @default_key
99
+ @default_key = (XPath.first(@doc.root, 'summary/default_key/text()') || \
100
+ XPath.first(@doc.root, 'records/*[1]/*[1]').name).to_s.to_sym
101
+
102
+ @summary = summary_to_h
103
+ @records = records_to_h
70
104
  @root_name = @doc.root.name
71
105
  @item_name = XPath.first(@doc.root, 'records/*[1]').name
72
106
  end
@@ -75,7 +109,7 @@ class Dynarex
75
109
  puts @doc.to_s
76
110
  end
77
111
 
78
- def records_to_h(default_key)
112
+ def records_to_h()
79
113
  ah = XPath.match(@doc.root, 'records/*').each_with_index.map do |row,i|
80
114
  created = Time.now.to_s; id = Time.now.strftime("%Y%m%I%H%M%S") + i.to_s
81
115
  last_modified = ''
@@ -86,8 +120,8 @@ class Dynarex
86
120
  r[node.name.to_s.to_sym] = node.text.to_s
87
121
  r
88
122
  end
89
- [body[default_key.to_sym],{id: id, created: created, last_modified: \
90
- last_modified, body: body}]
123
+ [body[@default_key],{id: id, created: created, last_modified: \
124
+ last_modified, body: body}]
91
125
  end
92
126
  Hash[*ah.flatten]
93
127
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: dynarex
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.3.0
4
+ version: 0.3.1
5
5
  platform: ruby
6
6
  authors: []
7
7
 
@@ -9,7 +9,7 @@ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
11
 
12
- date: 2010-04-06 00:00:00 +01:00
12
+ date: 2010-04-07 00:00:00 +01:00
13
13
  default_executable:
14
14
  dependencies: []
15
15