dynarex 0.7.0 → 0.7.1
Sign up to get free protection for your applications and to get access to all the features.
- data/lib/dynarex.rb +22 -16
- metadata +1 -1
data/lib/dynarex.rb
CHANGED
@@ -121,26 +121,15 @@ EOF
|
|
121
121
|
# dynarex = Dynarex.new 'contacts/contact(name,age,dob)'
|
122
122
|
# dynarex.create name: Bob, age: 52
|
123
123
|
|
124
|
-
def create(
|
125
|
-
|
126
|
-
|
127
|
-
record = Element.new record_name
|
128
|
-
fields.each do |k,v|
|
129
|
-
element = Element.new(k.to_s)
|
130
|
-
element.text = v if v
|
131
|
-
record.add element
|
132
|
-
end
|
133
|
-
|
134
|
-
ids = XPath.match(@doc.root,'records/*/attribute::id').map &:value
|
135
|
-
id = ids.empty? ? 1 : ids.max.succ
|
136
|
-
|
137
|
-
attributes = {id: id, created: Time.now.to_s, last_modified: nil}
|
138
|
-
attributes.each {|k,v| record.add_attribute(k.to_s, v)}
|
139
|
-
@doc.root.elements['records'].add record
|
124
|
+
def create(arg)
|
125
|
+
methods = {Hash: :hash_create, String: :create_from_line}
|
126
|
+
send (methods[arg.class.to_s.to_sym]), arg
|
140
127
|
|
141
128
|
load_records
|
142
129
|
self
|
143
130
|
end
|
131
|
+
|
132
|
+
|
144
133
|
|
145
134
|
#Create a record from a string, given the dynarex document contains a format mask.
|
146
135
|
# dynarex = Dynarex.new 'contacts/contact(name,age,dob)'
|
@@ -184,6 +173,23 @@ EOF
|
|
184
173
|
end
|
185
174
|
|
186
175
|
private
|
176
|
+
|
177
|
+
def hash_create(params={})
|
178
|
+
record_name, fields = capture_fields(params)
|
179
|
+
record = Element.new record_name
|
180
|
+
fields.each do |k,v|
|
181
|
+
element = Element.new(k.to_s)
|
182
|
+
element.text = v if v
|
183
|
+
record.add element
|
184
|
+
end
|
185
|
+
|
186
|
+
ids = XPath.match(@doc.root,'records/*/attribute::id').map &:value
|
187
|
+
id = ids.empty? ? 1 : ids.max.succ
|
188
|
+
|
189
|
+
attributes = {id: id, created: Time.now.to_s, last_modified: nil}
|
190
|
+
attributes.each {|k,v| record.add_attribute(k.to_s, v)}
|
191
|
+
@doc.root.elements['records'].add record
|
192
|
+
end
|
187
193
|
|
188
194
|
def capture_fields(params)
|
189
195
|
|