dynarex 0.4.3 → 0.5.0

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 (3) hide show
  1. data/lib/dynarex.rb +52 -3
  2. metadata +2 -3
  3. data/lib/dynarex2.rb +0 -163
@@ -1,11 +1,13 @@
1
1
  #!/usr/bin/ruby
2
2
 
3
+ # file: dynarex.rb
4
+
3
5
  require 'rexml/document'
4
6
  require 'nokogiri'
5
7
  require 'open-uri'
6
8
  require 'builder'
7
9
 
8
- class Dynarex
10
+ class Dynarexx
9
11
  include REXML
10
12
 
11
13
  def initialize(location)
@@ -96,7 +98,48 @@ EOF
96
98
  self
97
99
  end
98
100
 
101
+ def create(params={})
102
+
103
+ record_name, fields = capture_fields(params)
104
+ record = Element.new record_name
105
+ fields.each{|k,v| record.add Element.new(k.to_s).add_text(v) if v}
106
+
107
+ ids = XPath.match(@doc.root,'records/*/attribute::id').map &:value
108
+ id = ids.empty? ? 1 : ids.max.succ
109
+
110
+ attributes = {id: id, created: Time.now.to_s, last_modified: nil}
111
+ attributes.each {|k,v| record.add_attribute(k.to_s, v)}
112
+ @doc.root.elements['records'].add record
113
+
114
+ load_records
115
+ end
116
+
117
+ def update(id, params={})
118
+ record_name, fields = capture_fields(params)
119
+
120
+ # for each field update each record field
121
+ record = XPath.first(@doc.root, "records/#{record_name}[@id=#{id.to_s}]")
122
+ fields.each {|k,v| record.elements[k.to_s].text = v if v}
123
+ load_records
124
+ end
125
+
126
+ def delete(id)
127
+ node = XPath.first(@doc.root, "records/*[@id='#{id}']")
128
+ node.parent.delete node
129
+ load_records
130
+ end
131
+
99
132
  private
133
+
134
+ def capture_fields(params)
135
+
136
+ record_name, raw_fields = @schema.match(/(\w+)\(([^\)]+)/).captures
137
+ fields = Hash[raw_fields.split(',').map{|x| [x.strip.to_sym, nil]}]
138
+
139
+ fields.keys.each {|key| fields[key] = params[key] if params.has_key? key}
140
+ [record_name, fields]
141
+ end
142
+
100
143
 
101
144
  def display_xml
102
145
 
@@ -134,12 +177,17 @@ EOF
134
177
  XPath.first(@doc.root, 'records/*[1]/*[1]').name).to_s.to_sym
135
178
 
136
179
  @summary = summary_to_h
137
- @records = records_to_h
138
- @flat_records = flat_records_to_h
180
+ @schema = @doc.root.text('summary/schema').to_s
181
+ load_records
139
182
  @root_name = @doc.root.name
140
183
  @item_name = XPath.first(@doc.root, 'records/*[1]').name
141
184
  end
142
185
 
186
+ def load_records
187
+ @records = records_to_h
188
+ @flat_records = flat_records_to_h
189
+ end
190
+
143
191
  def display()
144
192
  puts @doc.to_s
145
193
  end
@@ -186,3 +234,4 @@ EOF
186
234
  end
187
235
 
188
236
  end
237
+
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.4.3
4
+ version: 0.5.0
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-05-09 00:00:00 +01:00
12
+ date: 2010-05-10 00:00:00 +01:00
13
13
  default_executable:
14
14
  dependencies: []
15
15
 
@@ -23,7 +23,6 @@ extra_rdoc_files: []
23
23
 
24
24
  files:
25
25
  - lib/dynarex.rb
26
- - lib/dynarex2.rb
27
26
  has_rdoc: true
28
27
  homepage:
29
28
  licenses: []
@@ -1,163 +0,0 @@
1
- #!/usr/bin/ruby
2
-
3
- require 'rexml/document'
4
- require 'nokogiri'
5
- require 'open-uri'
6
- require 'builder'
7
-
8
- class Dynarex
9
- include REXML
10
-
11
- def initialize(location)
12
- open(location)
13
- end
14
-
15
- def summary
16
- @summary
17
- end
18
-
19
- def records
20
- @records
21
- end
22
-
23
- def to_s
24
- xsl_buffer =<<EOF
25
- <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
26
- <xsl:output encoding="UTF-8"
27
- method="text"
28
- indent="no"
29
- omit-xml-declaration="yes"/>
30
-
31
- <xsl:template match="*">
32
- <xsl:for-each select="records/*">[!regex_values]<xsl:text>
33
- </xsl:text>
34
- </xsl:for-each>
35
- </xsl:template>
36
- </xsl:stylesheet>
37
- EOF
38
-
39
- format_mask = XPath.first(@doc.root, 'summary/format_mask/text()').to_s
40
-
41
- xslt_format = format_mask.to_s.gsub(/\s(?=\[!\w+\])/,'<xsl:text> </xsl:text>').gsub(/\[!(\w+)\]/, '<xsl:value-of select="\1"/>')
42
- xsl_buffer.sub!(/\[!regex_values\]/, xslt_format)
43
-
44
- xslt = Nokogiri::XSLT(xsl_buffer)
45
- out = xslt.transform(Nokogiri::XML(@doc.to_s))
46
- out.text
47
- end
48
-
49
- def to_xml
50
- display_xml()
51
- end
52
-
53
- def save(filepath)
54
- xml = display_xml()
55
- File.open(filepath,'w'){|f| f.write xml}
56
- end
57
-
58
- def parse(buffer)
59
- format_mask = XPath.first(@doc.root, 'summary/format_mask/text()').to_s
60
- t = format_mask.to_s.gsub(/\[!(\w+)\]/, '(.*)').sub(/\[/,'\[').sub(/\]/,'\]')
61
- lines = buffer.split(/\r?\n|\r(?!\n)/).map {|x|x.match(/#{t}/).captures}
62
- fields = format_mask.scan(/\[!(\w+)\]/).flatten.map(&:to_sym)
63
-
64
- a = lines.each_with_index.map do|x,i|
65
- created = Time.now.to_s; id = Time.now.strftime("%Y%m%I%H%M%S") + i.to_s
66
- h = Hash[fields.zip(x)]
67
- [h[@default_key], {id: id, created: created, last_modified: '', body: h}]
68
- end
69
-
70
- h2 = Hash[a]
71
-
72
- #replace the existing records hash
73
- h = @records
74
- h2.each do |key,item|
75
- if h.has_key? key then
76
-
77
- # overwrite the previous item and change the timestamps
78
- h[key][:last_modified] = item[:created]
79
- item[:body].each do |k,v|
80
- h[key][:body][k.to_sym] = v
81
- end
82
- else
83
-
84
- h[key] = item.clone
85
- end
86
- end
87
-
88
- h.each {|key, item| h.delete(key) if not h2.has_key? key}
89
- self
90
- end
91
-
92
- private
93
-
94
- def display_xml
95
-
96
- xml = Builder::XmlMarkup.new( :target => buffer='', :indent => 2 )
97
- xml.instruct! :xml, :version => "1.0", :encoding => "UTF-8"
98
-
99
- xml.send @root_name do
100
- xml.summary do
101
- @summary.each{|key,value| xml.send key, value}
102
- end
103
- xml.records do
104
- @records.each do |k, item|
105
- xml.send(@item_name, id: item[:id], created: item[:created], \
106
- last_modified: item[:last_modified]) do
107
- item[:body].each{|name,value| xml.send name, value}
108
- end
109
- end
110
- end
111
- end
112
-
113
- buffer
114
-
115
- end
116
-
117
- def open(location)
118
- if location[/^https?:\/\//] then
119
- buffer = Kernel.open(location, 'UserAgent' => 'Dynarex-Reader').read
120
- elsif location[/\</]
121
- buffer = location
122
- else
123
- buffer = File.open(location,'r').read
124
- end
125
- @doc = Document.new buffer
126
- @default_key = (XPath.first(@doc.root, 'summary/default_key/text()') || \
127
- XPath.first(@doc.root, 'records/*[1]/*[1]').name).to_s.to_sym
128
-
129
- @summary = summary_to_h
130
- @records = records_to_h
131
- @root_name = @doc.root.name
132
- @item_name = XPath.first(@doc.root, 'records/*[1]').name
133
- end
134
-
135
- def display()
136
- puts @doc.to_s
137
- end
138
-
139
- def records_to_h()
140
- ah = XPath.match(@doc.root, 'records/*').each_with_index.map do |row,i|
141
- created = Time.now.to_s; id = Time.now.strftime("%Y%m%I%H%M%S") + i.to_s
142
- last_modified = ''
143
- id = row.attribute('id').value.to_s if row.attribute('id')
144
- created = row.attribute('created').value.to_s if row.attribute('created')
145
- last_modified = row.attribute('last_modified').value.to_s if row.attribute('last_modified')
146
- body = XPath.match(row, '*').inject({}) do |r,node|
147
- r[node.name.to_s.to_sym] = node.text.to_s
148
- r
149
- end
150
- [body[@default_key],{id: id, created: created, last_modified: \
151
- last_modified, body: body}]
152
- end
153
- Hash[*ah.flatten]
154
- end
155
-
156
- def summary_to_h
157
- XPath.match(@doc.root, 'summary/*').inject({}) do |r,node|
158
- r[node.name.to_s.to_sym] = node.text.to_s
159
- r
160
- end
161
- end
162
-
163
- end