dynarex 0.9.3 → 0.9.4
Sign up to get free protection for your applications and to get access to all the features.
- data/lib/dynarex.rb +25 -4
- metadata +1 -1
data/lib/dynarex.rb
CHANGED
@@ -11,7 +11,7 @@ require 'ostruct'
|
|
11
11
|
class Dynarex
|
12
12
|
include REXML
|
13
13
|
|
14
|
-
attr_accessor :format_mask, :delimiter
|
14
|
+
attr_accessor :format_mask, :delimiter, :schema
|
15
15
|
|
16
16
|
#Create a new dynarex document from 1 of the following options:
|
17
17
|
#* a local file path
|
@@ -21,9 +21,9 @@ class Dynarex
|
|
21
21
|
#* an XML string
|
22
22
|
# Dynarex.new '<contacts><summary><schema>contacts/contact(name,age,dob)</schema></summary><records/></contacts>'
|
23
23
|
|
24
|
-
def initialize(location)
|
24
|
+
def initialize(location=nil)
|
25
25
|
@delimiter = ' '
|
26
|
-
open(location)
|
26
|
+
open(location) if location
|
27
27
|
end
|
28
28
|
|
29
29
|
def delimiter=(separator)
|
@@ -40,6 +40,10 @@ class Dynarex
|
|
40
40
|
@summary[:format_mask] = @format_mask
|
41
41
|
end
|
42
42
|
|
43
|
+
def schema=(s)
|
44
|
+
open s
|
45
|
+
end
|
46
|
+
|
43
47
|
# Returns the hash representation of the document summary.
|
44
48
|
|
45
49
|
def summary
|
@@ -78,6 +82,13 @@ xsl_buffer =<<EOF
|
|
78
82
|
EOF
|
79
83
|
|
80
84
|
format_mask = XPath.first(@doc.root, 'summary/format_mask/text()').to_s
|
85
|
+
dynarex = Dynarex.new
|
86
|
+
|
87
|
+
s = %q(schema="companies/company(name, last_contacted, contact)" delimiter=" # ")
|
88
|
+
s.split(/(?=\w+\="[^"]+")/)
|
89
|
+
raw_a = s.scan(/\w+\="[^"]+"/)
|
90
|
+
a = raw_a.map{|x| r = x.split(/=/); [(r[0] + "=").to_sym, r[1][/^"(.*)"$/,1]] }
|
91
|
+
a.each{|name, value| dynarex.method(name).call(value)}
|
81
92
|
|
82
93
|
xslt_format = format_mask.to_s.gsub(/\s(?=\[!\w+\])/,'<xsl:text> </xsl:text>').gsub(/\[!(\w+)\]/, '<xsl:value-of select="\1"/>')
|
83
94
|
xsl_buffer.sub!(/\[!regex_values\]/, xslt_format)
|
@@ -102,9 +113,18 @@ EOF
|
|
102
113
|
#Parses 1 or more lines of text to create or update existing records.
|
103
114
|
|
104
115
|
def parse(buffer)
|
116
|
+
|
117
|
+
raw_header = buffer.slice!(/<\?dynarex[^>]+>/)
|
118
|
+
|
119
|
+
if raw_header then
|
120
|
+
header = raw_header[/<?dynarex (.*)?>/,1]
|
121
|
+
header.scan(/\w+\="[^"]+\"/).map{|x| r = x.split(/=/); [(r[0] + "=").to_sym, r[1][/^"(.*)"$/,1]] }.each {|name, value| self.method(name).call(value)}
|
122
|
+
end
|
123
|
+
|
105
124
|
i = XPath.match(@doc.root, 'records/*/attribute::id').max_by(&:value).to_s.to_i
|
106
125
|
t = @format_mask.to_s.gsub(/\[!(\w+)\]/, '(.*)').sub(/\[/,'\[').sub(/\]/,'\]')
|
107
|
-
|
126
|
+
|
127
|
+
lines = buffer.strip.split(/\r?\n|\r(?!\n)/).map {|x|x.match(/#{t}/).captures}
|
108
128
|
|
109
129
|
a = lines.map do|x|
|
110
130
|
created = Time.now.to_s
|
@@ -267,6 +287,7 @@ EOF
|
|
267
287
|
alias refresh_doc display_xml
|
268
288
|
|
269
289
|
def dynarex_new(s)
|
290
|
+
@schema = s
|
270
291
|
ptrn = %r((\w+)\[?([^\]]+)?\]?\/(\w+)\(([^\)]+)\))
|
271
292
|
root_name, raw_summary, record_name, raw_fields = s.match(ptrn).captures
|
272
293
|
summary, fields = [raw_summary || '',raw_fields].map {|x| x.split(/,/).map &:strip}
|