dynarex 1.0.19 → 1.0.20

Sign up to get free protection for your applications and to get access to all the features.
Files changed (2) hide show
  1. data/lib/dynarex.rb +69 -65
  2. metadata +1 -1
@@ -133,71 +133,9 @@ EOF
133
133
 
134
134
  #Parses 1 or more lines of text to create or update existing records.
135
135
 
136
- def parse(buffer)
137
-
138
- raw_header = buffer.slice!(/<\?dynarex[^>]+>/)
139
-
140
- if raw_header then
141
- header = raw_header[/<?dynarex (.*)?>/,1]
142
- header.scan(/\w+\="[^"]+\"/).map{|x| r = x.split(/=/); [(r[0] + "=").to_sym, r[1][/^"(.*)"$/,1]] }.each {|name, value| self.method(name).call(value)}
143
- end
144
-
145
- # if records already exist find the max id
146
- i = @doc.xpath('max(records/*/attribute::id)').to_i
147
-
148
-
149
- # 'a' and 'a_split' just used for validation
150
- a = @format_mask.scan(/\[!\w+\]/)
151
- a_split = @format_mask.split(/\[!\w+\]/)
152
-
153
- if a.length == 2 and a_split[1].length == 1 then
154
- a2 = []
155
-
156
- # 1st
157
- a2 << "([^#{a_split[1]}]+)" << a_split[1]
158
-
159
- # last
160
- a2 << "(.*)"
161
- t = a2.join
162
- else
163
- # convert the format mask into a friendly reg exp string
164
- t = @format_mask.to_s.gsub(/\[!(\w+)\]/, '(.*)').sub(/\[/,'\[').sub(/\]/,'\]')
165
- end
166
-
167
- lines = buffer.strip.split(/\r?\n|\r(?!\n)/).map {|x|x.match(/#{t}/).captures}
168
-
169
- a = lines.map do|x|
170
- created = Time.now.to_s
171
-
172
- h = Hash[@fields.zip(x)]
173
- [h[@default_key], {id: '', created: created, last_modified: '', body: h}]
174
- end
175
-
176
- h2 = Hash[a]
177
-
178
- #replace the existing records hash
179
- h = @records
180
- h2.each do |key,item|
181
- if h.has_key? key then
182
-
183
- # overwrite the previous item and change the timestamps
184
- h[key][:last_modified] = item[:created]
185
- item[:body].each do |k,v|
186
- h[key][:body][k.to_sym] = v
187
- end
188
- else
189
- i += 1
190
- item[:id] = i.to_s
191
- h[key] = item.clone
192
- end
193
- end
194
-
195
- h.each {|key, item| h.delete(key) if not h2.has_key? key}
196
- #refresh_doc
197
- #load_records
198
- @flat_records = @records.values.map{|x| x[:body]}
199
- rebuild_doc
200
- self
136
+ def parse(buffer='')
137
+ buffer = yield if block_given?
138
+ string_parse buffer
201
139
  end
202
140
 
203
141
  #Create a record from a hash containing the field name, and the field value.
@@ -371,6 +309,72 @@ EOF
371
309
 
372
310
  alias refresh_doc display_xml
373
311
 
312
+ def string_parse(buffer)
313
+ raw_header = buffer.slice!(/<\?dynarex[^>]+>/)
314
+
315
+ if raw_header then
316
+ header = raw_header[/<?dynarex (.*)?>/,1]
317
+ header.scan(/\w+\="[^"]+\"/).map{|x| r = x.split(/=/); [(r[0] + "=").to_sym, r[1][/^"(.*)"$/,1]] }.each {|name, value| self.method(name).call(value)}
318
+ end
319
+
320
+ # if records already exist find the max id
321
+ i = @doc.xpath('max(records/*/attribute::id)').to_i
322
+
323
+
324
+ # 'a' and 'a_split' just used for validation
325
+ a = @format_mask.scan(/\[!\w+\]/)
326
+ a_split = @format_mask.split(/\[!\w+\]/)
327
+
328
+ if a.length == 2 and a_split[1].length == 1 then
329
+ a2 = []
330
+
331
+ # 1st
332
+ a2 << "([^#{a_split[1]}]+)" << a_split[1]
333
+
334
+ # last
335
+ a2 << "(.*)"
336
+ t = a2.join
337
+ else
338
+ # convert the format mask into a friendly reg exp string
339
+ t = @format_mask.to_s.gsub(/\[!(\w+)\]/, '(.*)').sub(/\[/,'\[').sub(/\]/,'\]')
340
+ end
341
+
342
+ lines = buffer.strip.split(/\r?\n|\r(?!\n)/).map {|x|x.match(/#{t}/).captures}
343
+
344
+ a = lines.map do|x|
345
+ created = Time.now.to_s
346
+
347
+ h = Hash[@fields.zip(x)]
348
+ [h[@default_key], {id: '', created: created, last_modified: '', body: h}]
349
+ end
350
+
351
+ h2 = Hash[a]
352
+
353
+ #replace the existing records hash
354
+ h = @records
355
+ h2.each do |key,item|
356
+ if h.has_key? key then
357
+
358
+ # overwrite the previous item and change the timestamps
359
+ h[key][:last_modified] = item[:created]
360
+ item[:body].each do |k,v|
361
+ h[key][:body][k.to_sym] = v
362
+ end
363
+ else
364
+ i += 1
365
+ item[:id] = i.to_s
366
+ h[key] = item.clone
367
+ end
368
+ end
369
+
370
+ h.each {|key, item| h.delete(key) if not h2.has_key? key}
371
+ #refresh_doc
372
+ #load_records
373
+ @flat_records = @records.values.map{|x| x[:body]}
374
+ rebuild_doc
375
+ self
376
+ end
377
+
374
378
  def dynarex_new(s)
375
379
  @schema = s
376
380
  ptrn = %r((\w+)\[?([^\]]+)?\]?\/(\w+)\(([^\)]+)\))
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: 1.0.19
4
+ version: 1.0.20
5
5
  platform: ruby
6
6
  authors: []
7
7