dynarex 0.7.1 → 0.7.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (2) hide show
  1. data/lib/dynarex.rb +28 -11
  2. metadata +1 -1
@@ -21,7 +21,11 @@ class Dynarex
21
21
  def initialize(location)
22
22
  open(location)
23
23
  end
24
-
24
+
25
+ def fields
26
+ @fields
27
+ end
28
+
25
29
  # Returns the hash representation of the document summary.
26
30
 
27
31
  def summary
@@ -151,11 +155,11 @@ EOF
151
155
  # dynarex.update 4, name: Jeff, age: 38
152
156
 
153
157
  def update(id, params={})
154
- record_name, fields = capture_fields(params)
158
+ fields = capture_fields(params)
155
159
 
156
160
  # for each field update each record field
157
- record = XPath.first(@doc.root, "records/#{record_name}[@id=#{id.to_s}]")
158
- fields.each {|k,v| record.elements[k.to_s].text = v if v}
161
+ 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}
159
163
  record.add_attribute('last_modified', Time.now.to_s)
160
164
 
161
165
  load_records
@@ -175,8 +179,8 @@ EOF
175
179
  private
176
180
 
177
181
  def hash_create(params={})
178
- record_name, fields = capture_fields(params)
179
- record = Element.new record_name
182
+ fields = capture_fields(params)
183
+ record = Element.new @record_name
180
184
  fields.each do |k,v|
181
185
  element = Element.new(k.to_s)
182
186
  element.text = v if v
@@ -192,12 +196,9 @@ EOF
192
196
  end
193
197
 
194
198
  def capture_fields(params)
195
-
196
- record_name, raw_fields = @schema.match(/(\w+)\(([^\)]+)/).captures
197
- fields = Hash[raw_fields.split(',').map{|x| [x.strip.to_sym, nil]}]
198
-
199
+ fields = @fields.clone
199
200
  fields.keys.each {|key| fields[key] = params[key] if params.has_key? key}
200
- [record_name, fields]
201
+ fields
201
202
  end
202
203
 
203
204
 
@@ -253,6 +254,15 @@ EOF
253
254
  buffer
254
255
  end
255
256
 
257
+ def attach_record_methods()
258
+ @fields.keys.each do |field|
259
+ self.instance_eval(
260
+ %Q(def find_by_#{field}(s)
261
+ Hash[@fields.keys.zip(XPath.match(@doc.root, "records/*[#{field}='\#{s}']/*/text()").map &:to_s)]
262
+ end))
263
+ end
264
+ end
265
+
256
266
  def open(s)
257
267
  if s[/\(/] then # schema
258
268
  buffer = dynarex_new s
@@ -269,7 +279,14 @@ EOF
269
279
  @root_name = @doc.root.name
270
280
  @summary = summary_to_h
271
281
  @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]}]
272
285
 
286
+
287
+ # load the record query handler methods
288
+ attach_record_methods
289
+
273
290
  if XPath.match(@doc.root, 'records/*').length > 0 then
274
291
 
275
292
  load_records
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.1
4
+ version: 0.7.2
5
5
  platform: ruby
6
6
  authors: []
7
7