polyrex 0.8.6 → 0.8.7
Sign up to get free protection for your applications and to get access to all the features.
- data/lib/polyrex.rb +88 -23
- metadata +2 -2
data/lib/polyrex.rb
CHANGED
@@ -12,11 +12,11 @@ require 'polyrex-xslt'
|
|
12
12
|
require 'rexle'
|
13
13
|
|
14
14
|
class Polyrex
|
15
|
-
attr_accessor :summary_fields, :xslt_schema
|
15
|
+
attr_accessor :summary_fields, :xslt_schema, :id_counter
|
16
16
|
|
17
|
-
def initialize(location)
|
17
|
+
def initialize(location, id_counter='1')
|
18
18
|
|
19
|
-
@
|
19
|
+
@id_counter = id_counter
|
20
20
|
open(location)
|
21
21
|
summary_h = Hash[*@doc.xpath("summary/*").map {|x| [x.name, x.text]}.flatten]
|
22
22
|
@summary = OpenStruct.new summary_h
|
@@ -25,9 +25,9 @@ class Polyrex
|
|
25
25
|
end
|
26
26
|
|
27
27
|
def create(id=nil)
|
28
|
-
|
29
|
-
|
30
|
-
@create.id = id || @
|
28
|
+
# @create is a PolyrexCreateObject, @parent_node is a REXML::Element pointing to the current record
|
29
|
+
|
30
|
+
@create.id = id || @id_counter
|
31
31
|
@create.record = @parent_node.name == 'records' ? @parent_node : @parent_node.element('records')
|
32
32
|
@create
|
33
33
|
end
|
@@ -56,10 +56,13 @@ class Polyrex
|
|
56
56
|
|
57
57
|
def find_by_id(id)
|
58
58
|
@parent_node = @doc.element("//[@id='#{id}']")
|
59
|
+
end
|
60
|
+
|
61
|
+
def id(id)
|
62
|
+
@parent_node = @doc.element("//[@id='#{id}']")
|
59
63
|
self
|
60
64
|
end
|
61
65
|
|
62
|
-
alias id find_by_id
|
63
66
|
# -- end of crud methods --
|
64
67
|
|
65
68
|
# -- start of full text edit methods
|
@@ -79,7 +82,7 @@ class Polyrex
|
|
79
82
|
|
80
83
|
def records
|
81
84
|
@doc.xpath("records/*").map do |record|
|
82
|
-
@objects_a[0].new(record
|
85
|
+
@objects_a[0].new(record)
|
83
86
|
end
|
84
87
|
end
|
85
88
|
|
@@ -87,6 +90,10 @@ class Polyrex
|
|
87
90
|
@summary
|
88
91
|
end
|
89
92
|
|
93
|
+
def to_a()
|
94
|
+
recordx_map @doc.root
|
95
|
+
end
|
96
|
+
|
90
97
|
def to_xslt()
|
91
98
|
@polyrex_xslt.schema = @schema
|
92
99
|
@polyrex_xslt.to_xslt
|
@@ -126,20 +133,39 @@ class Polyrex
|
|
126
133
|
#----
|
127
134
|
|
128
135
|
@schema = schema
|
129
|
-
@
|
136
|
+
@id_counter = '0'
|
130
137
|
|
131
138
|
root_name = @recordx.shift
|
132
139
|
|
133
140
|
("<%s><summary>%s</summary><records/></%s>" % [root_name, (summary || '') , root_name])
|
134
141
|
end
|
135
142
|
|
143
|
+
def recordx_map(node)
|
144
|
+
# get the summary
|
145
|
+
summary = XPath.first(node, 'summary')
|
146
|
+
|
147
|
+
# get the fields
|
148
|
+
fields = summary.elements.map do |x|
|
149
|
+
next if %w(schema format_mask recordx_type).include? x.name
|
150
|
+
r = x.text.to_s.gsub(/^[\n\s]+/,'').length > 0 ? x.text : x.cdatas.join.strip
|
151
|
+
REXML::Text::unnormalize(r)
|
152
|
+
end
|
153
|
+
|
154
|
+
# get the records
|
155
|
+
records = XPath.first(node, 'records')
|
156
|
+
a = records.elements.map {|x| recordx_map x}
|
157
|
+
|
158
|
+
[fields, a]
|
159
|
+
end
|
160
|
+
|
136
161
|
def string_parse(lines)
|
137
162
|
format_line!(@parent_node, LineTree.new(lines).to_a)
|
138
163
|
end
|
139
164
|
|
140
165
|
def load_handlers(schema)
|
141
|
-
@create = PolyrexCreateObject.new(schema, @
|
142
|
-
|
166
|
+
@create = PolyrexCreateObject.new(schema, @id_counter)
|
167
|
+
|
168
|
+
objects = PolyrexObjects.new(schema, @id_counter)
|
143
169
|
@objects = objects.to_h
|
144
170
|
@objects_a = objects.to_a
|
145
171
|
attach_create_handlers(@objects.keys)
|
@@ -149,7 +175,7 @@ class Polyrex
|
|
149
175
|
def format_line!(records, a, i=0)
|
150
176
|
|
151
177
|
a.each do |x|
|
152
|
-
|
178
|
+
|
153
179
|
unless @recordx[i] then
|
154
180
|
@recordx[i] = @recordx[-1].clone
|
155
181
|
@format_masks[i] = @format_masks[-1]
|
@@ -158,19 +184,39 @@ class Polyrex
|
|
158
184
|
tag_name = @recordx[i].to_s
|
159
185
|
line = x.shift
|
160
186
|
|
161
|
-
@field_names = @format_masks[i].to_s.scan(/\[!(\w+)\]/).flatten.map(&:to_sym)
|
162
|
-
t = @format_masks[i].to_s.gsub(/\[!(\w+)\]/, '(.*)').sub(/\[/,'\[').sub(/\]/,'\]')
|
163
|
-
a = t.reverse.split(/(?=\)\*\.\()/).reverse.map &:reverse
|
164
187
|
|
165
|
-
|
166
|
-
|
167
|
-
|
168
|
-
|
188
|
+
|
189
|
+
unless @format_masks[i][/^\(.*\)$/] then
|
190
|
+
|
191
|
+
@field_names = @format_masks[i].to_s.scan(/\[!(\w+)\]/).flatten.map(&:to_sym)
|
192
|
+
|
193
|
+
t = regexify_fmask(@format_masks[i]) #.sub(/\[/,'\[').sub(/\]/,'\]')
|
194
|
+
a = t.reverse.split(/(?=\)[^\(]+\()/).reverse.map &:reverse
|
195
|
+
patterns = tail_map(a)
|
196
|
+
|
197
|
+
pattern = patterns.detect {|x| line.match(/#{x.join}/)}.join
|
169
198
|
|
170
|
-
|
199
|
+
field_values = line.match(/#{pattern}/).captures
|
200
|
+
field_values += [''] * (@field_names.length - field_values.length)
|
201
|
+
else
|
202
|
+
|
203
|
+
format_masks = @format_masks[i][1..-2].split('|')
|
204
|
+
patterns = format_masks.map do |x|
|
205
|
+
regexify_fmask(x) #.sub(/\[/,'\[').sub(/\]/,'\]')
|
206
|
+
end
|
207
|
+
|
208
|
+
pattern = patterns.detect {|x| line.match(/#{x}/)}
|
209
|
+
i = patterns.index(pattern)
|
210
|
+
|
211
|
+
@field_names = format_masks[i].to_s.scan(/\[!(\w+)\]/).flatten.map(&:to_sym)
|
212
|
+
|
213
|
+
field_values = line.match(/#{pattern}/).captures
|
214
|
+
end
|
215
|
+
|
216
|
+
@id_counter.succ!
|
171
217
|
|
172
218
|
record = Rexle::Element.new(tag_name)
|
173
|
-
record.add_attribute(
|
219
|
+
record.add_attribute(id: @id_counter.clone)
|
174
220
|
summary = Rexle::Element.new('summary')
|
175
221
|
|
176
222
|
@field_names.zip(field_values).each do |name, value|
|
@@ -211,7 +257,7 @@ class Polyrex
|
|
211
257
|
self.instance_eval(
|
212
258
|
%Q(
|
213
259
|
def #{name.downcase}()
|
214
|
-
@objects['#{name}'].new(@parent_node)
|
260
|
+
@objects['#{name}'].new(@parent_node, @id)
|
215
261
|
end
|
216
262
|
))
|
217
263
|
end
|
@@ -242,7 +288,7 @@ class Polyrex
|
|
242
288
|
end
|
243
289
|
|
244
290
|
id = @doc.xpath('max(//@id)')
|
245
|
-
@
|
291
|
+
@id_counter = id.to_s.succ if id
|
246
292
|
|
247
293
|
load_handlers(schema)
|
248
294
|
load_find_by(schema)
|
@@ -251,6 +297,25 @@ class Polyrex
|
|
251
297
|
|
252
298
|
end
|
253
299
|
|
300
|
+
def regexify_fmask(f)
|
301
|
+
|
302
|
+
a = f.split(/(?=\[!\w+\])/).map do |x|
|
303
|
+
|
304
|
+
aa = x.split(/(?=[^\]]+$)/)
|
305
|
+
|
306
|
+
if aa.length == 2 and aa.first[/\[!\w+\]/] then
|
307
|
+
field, delimiter = *aa
|
308
|
+
delimiter ||= '$'
|
309
|
+
d = delimiter[0]
|
310
|
+
"([^%s]+)%s" % ([d] * 2)
|
311
|
+
else
|
312
|
+
x.sub(/\[!\w+\]/,'(.*)')
|
313
|
+
end
|
314
|
+
end
|
315
|
+
|
316
|
+
a.join
|
317
|
+
end
|
318
|
+
|
254
319
|
def tail_map(a)
|
255
320
|
[a] + (a.length > 1 ? tail_map(a[0..-2]) : [])
|
256
321
|
end
|
metadata
CHANGED
@@ -2,7 +2,7 @@
|
|
2
2
|
name: polyrex
|
3
3
|
version: !ruby/object:Gem::Version
|
4
4
|
prerelease:
|
5
|
-
version: 0.8.
|
5
|
+
version: 0.8.7
|
6
6
|
platform: ruby
|
7
7
|
authors: []
|
8
8
|
|
@@ -10,7 +10,7 @@ autorequire:
|
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
12
|
|
13
|
-
date: 2011-03-
|
13
|
+
date: 2011-03-23 00:00:00 +00:00
|
14
14
|
default_executable:
|
15
15
|
dependencies:
|
16
16
|
- !ruby/object:Gem::Dependency
|