dynarex 0.7.2 → 0.7.3

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 +24 -23
  2. metadata +2 -2
@@ -86,16 +86,14 @@ EOF
86
86
 
87
87
  def parse(buffer)
88
88
  i = XPath.match(@doc.root, 'records/*/attribute::id').max_by(&:value).to_s.to_i
89
- format_mask = XPath.first(@doc.root, 'summary/format_mask/text()').to_s
90
- t = format_mask.to_s.gsub(/\[!(\w+)\]/, '(.*)').sub(/\[/,'\[').sub(/\]/,'\]')
89
+ t = @format_mask.to_s.gsub(/\[!(\w+)\]/, '(.*)').sub(/\[/,'\[').sub(/\]/,'\]')
91
90
  lines = buffer.split(/\r?\n|\r(?!\n)/).map {|x|x.match(/#{t}/).captures}
92
- fields = format_mask.scan(/\[!(\w+)\]/).flatten.map(&:to_sym)
93
91
 
94
92
  a = lines.map do|x|
95
93
  created = Time.now.to_s
96
94
 
97
- h = Hash[fields.zip(x)]
98
- [h[@default_key], {id: '', created: created, last_modified: '', body: h}]
95
+ h = Hash[@fields.zip(x)]
96
+ [h[@default_key.to_s], {id: '', created: created, last_modified: '', body: h}]
99
97
  end
100
98
 
101
99
  h2 = Hash[a]
@@ -140,13 +138,11 @@ EOF
140
138
  # dynarex.create_from_line 'Tracy 37 15-Jun-1972'
141
139
 
142
140
  def create_from_line(line)
143
- format_mask = XPath.first(@doc.root, 'summary/format_mask/text()').to_s
144
- t = format_mask.to_s.gsub(/\[!(\w+)\]/, '(.*)').sub(/\[/,'\[').sub(/\]/,'\]')
141
+ t = @format_mask.to_s.gsub(/\[!(\w+)\]/, '(.*)').sub(/\[/,'\[').sub(/\]/,'\]')
145
142
  line.match(/#{t}/).captures
146
143
 
147
144
  a = line.match(/#{t}/).captures
148
- fields = format_mask.scan(/\[!(\w+)\]/).flatten.map(&:to_sym)
149
- h = Hash[fields.zip(a)]
145
+ h = Hash[@fields.zip(a)]
150
146
  create h
151
147
  self
152
148
  end
@@ -159,7 +155,7 @@ EOF
159
155
 
160
156
  # for each field update each record field
161
157
  record = XPath.first(@doc.root, "records/#{@record_name}[@id=#{id.to_s}]")
162
- @fields.each {|k,v| record.elements[k.to_s].text = v if v}
158
+ fields.each {|k,v| record.elements[k.to_s].text = v if v}
163
159
  record.add_attribute('last_modified', Time.now.to_s)
164
160
 
165
161
  load_records
@@ -247,7 +243,6 @@ EOF
247
243
  xml.records
248
244
  end
249
245
 
250
- @default_key = fields[0]
251
246
  @records = {}
252
247
  @flat_records = {}
253
248
 
@@ -275,27 +270,33 @@ end))
275
270
  end
276
271
 
277
272
  @doc = Document.new buffer
278
- @schema = @doc.root.text('summary/schema').to_s
273
+ @schema = @doc.root.text('summary/schema')
279
274
  @root_name = @doc.root.name
280
275
  @summary = summary_to_h
281
276
  @default_key = XPath.first(@doc.root, 'summary/default_key/text()')
282
-
283
- @record_name, raw_fields = @schema.match(/(\w+)\(([^\)]+)/).captures
284
- @fields = Hash[raw_fields.split(',').map{|x| [x.strip.to_sym, nil]}]
277
+ @format_mask = XPath.first(@doc.root, 'summary/format_mask/text()')
278
+
279
+ @fields = @format_mask.to_s.scan(/\[!(\w+)\]/).flatten.map(&:to_sym) if @format_mask
285
280
 
286
-
287
- # load the record query handler methods
288
- attach_record_methods
281
+ if @schema then
282
+ @record_name, raw_fields = @schema.match(/(\w+)\(([^\)]+)/).captures
283
+ @fields = Hash[raw_fields.split(',').map{|x| [x.strip.to_sym, nil]}] unless @fields
284
+ end
285
+
286
+ if @fields then
287
+ @default_key = @fields[0] unless @default_key
288
+ # load the record query handler methods
289
+ attach_record_methods
290
+ end
289
291
 
290
292
  if XPath.match(@doc.root, 'records/*').length > 0 then
293
+ @record_name = XPath.first(@doc.root, 'records/*[1]').name
294
+ load_records
295
+ end
291
296
 
292
- load_records
293
- end
294
297
  end
295
298
 
296
299
  def load_records
297
- @default_key = (XPath.first(@doc.root, 'records/*[1]/*[1]').name).to_s.to_sym unless @default_key
298
- @record_name = XPath.first(@doc.root, 'records/*[1]').name
299
300
  @records = records_to_h
300
301
  @flat_records = flat_records_to_h
301
302
  end
@@ -334,7 +335,7 @@ end))
334
335
  r[node.name.to_s.to_sym] = node.text.to_s
335
336
  r
336
337
  end
337
- [body[@default_key],{id: id, created: created, last_modified: \
338
+ [body[@default_key.to_s],{id: id, created: created, last_modified: \
338
339
  last_modified, body: body}]
339
340
  end
340
341
  Hash[*ah.flatten]
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.7.2
4
+ version: 0.7.3
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-16 00:00:00 +01:00
12
+ date: 2010-05-17 00:00:00 +01:00
13
13
  default_executable:
14
14
  dependencies: []
15
15