polyrex 0.1.2 → 0.2.0
Sign up to get free protection for your applications and to get access to all the features.
- data/lib/polyrex.rb +97 -0
- metadata +2 -2
data/lib/polyrex.rb
CHANGED
@@ -48,6 +48,7 @@ end
|
|
48
48
|
|
49
49
|
end
|
50
50
|
|
51
|
+
# -- start of crud methods --
|
51
52
|
def valid_creation?()
|
52
53
|
|
53
54
|
xpath = BacktrackXPath.new(@parent_node).to_s.gsub('//','/')
|
@@ -85,6 +86,102 @@ end
|
|
85
86
|
@parent_node = XPath.first(@doc.root, "//[@id='#{id}']")
|
86
87
|
self
|
87
88
|
end
|
89
|
+
# -- end of crud methods --
|
90
|
+
|
91
|
+
# -- start of full text edit methods
|
92
|
+
def format_masks
|
93
|
+
@format_masks
|
94
|
+
end
|
95
|
+
|
96
|
+
def parse(lines)
|
97
|
+
a = lines.split("\n")
|
98
|
+
|
99
|
+
a.map! do |x|
|
100
|
+
x.match(/(\s+)?(.*)/).captures
|
101
|
+
end
|
102
|
+
|
103
|
+
new_a = []
|
104
|
+
history = []
|
105
|
+
history << new_a
|
106
|
+
|
107
|
+
root_format_mask = @format_masks.shift
|
108
|
+
field_names = root_format_mask.to_s.scan(/\[!(\w+)\]/).flatten.map(&:to_sym)
|
109
|
+
root_name = @recordx.shift
|
110
|
+
|
111
|
+
build_tree(a, new_a, 0, history)
|
112
|
+
out = scan_link(new_a)
|
113
|
+
|
114
|
+
summary = field_names.map {|x| "<%s/>" % x}.join
|
115
|
+
@doc = Document.new("<%s><summary>%s</summary></%s>" % [root_name, summary, root_name])
|
116
|
+
records = Document.new("<records>%s</records>" % out)
|
117
|
+
@doc.root.add records
|
118
|
+
end
|
119
|
+
|
120
|
+
def build_tree(a, new_a, prev_indent, history=[])
|
121
|
+
if a.length > 0 then
|
122
|
+
x = a.shift
|
123
|
+
n = x.shift
|
124
|
+
cur_indent = n ? n.length : 0
|
125
|
+
indent, xr = build_branch(a, new_a, cur_indent, prev_indent, x, history )
|
126
|
+
|
127
|
+
if xr then
|
128
|
+
history.pop
|
129
|
+
cur_indent, xr = build_branch(a, history[-1] || new_a, indent, prev_indent, xr, history )
|
130
|
+
return [cur_indent, xr] if xr
|
131
|
+
end
|
132
|
+
end
|
133
|
+
end
|
134
|
+
|
135
|
+
def build_branch(a, new_a, cur_indent, prev_indent, x, history=[])
|
136
|
+
if cur_indent > prev_indent then
|
137
|
+
new_a = history[-1]
|
138
|
+
new_inner_a = [x]
|
139
|
+
new_a << new_inner_a
|
140
|
+
history << new_inner_a
|
141
|
+
build_tree(a, history[-2], cur_indent, history)
|
142
|
+
elsif cur_indent == prev_indent then
|
143
|
+
new_inner_a = [x]
|
144
|
+
new_a << new_inner_a
|
145
|
+
history << new_inner_a
|
146
|
+
build_tree(a, new_inner_a, cur_indent, history)
|
147
|
+
else
|
148
|
+
# revert to the earlier new_a
|
149
|
+
return [cur_indent, x]
|
150
|
+
end
|
151
|
+
end
|
152
|
+
|
153
|
+
def scan_link(a,indent='')
|
154
|
+
out = []
|
155
|
+
i2 = indent.clone
|
156
|
+
a.each do |x|
|
157
|
+
i = indent.length / 2
|
158
|
+
tag_name = @recordx[i].to_s
|
159
|
+
|
160
|
+
line= x[0].reverse.map(&:strip).join
|
161
|
+
@field_names = @format_masks[i].to_s.scan(/\[!(\w+)\]/).flatten.map(&:to_sym)
|
162
|
+
|
163
|
+
t = @format_masks[i].to_s.gsub(/\[!(\w+)\]/, '(.*)').sub(/\[/,'\[').sub(/\]/,'\]')
|
164
|
+
field_values = line.match(/#{t}/).captures
|
165
|
+
|
166
|
+
fields = @field_names.zip(field_values).map{|name, value| "<%s>%s</%s>" % [name,value,name]}.join
|
167
|
+
|
168
|
+
out << indent + "<#{tag_name}><summary>#{fields}</summary>"
|
169
|
+
if x.length > 1 and x[0].is_a? Array then
|
170
|
+
#i2 += ' '
|
171
|
+
out << indent + "<records>\n"
|
172
|
+
x.shift
|
173
|
+
out << scan_link(x,i2 + ' ')
|
174
|
+
out << indent + "</records>\n"
|
175
|
+
else
|
176
|
+
out << indent + "<records/>\n"
|
177
|
+
|
178
|
+
end
|
179
|
+
out << indent + "</#{tag_name}>\n"
|
180
|
+
end
|
181
|
+
out.join
|
182
|
+
end
|
183
|
+
|
184
|
+
# -- end of full text edit methods
|
88
185
|
|
89
186
|
def to_xml()
|
90
187
|
@doc.to_s
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: polyrex
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.2.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-
|
12
|
+
date: 2010-05-22 00:00:00 +01:00
|
13
13
|
default_executable:
|
14
14
|
dependencies:
|
15
15
|
- !ruby/object:Gem::Dependency
|