polyrex 0.7.4 → 0.7.5

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/polyrex.rb +23 -25
  2. metadata +2 -2
@@ -6,12 +6,11 @@ require 'polyrex-schema'
6
6
  require 'line-tree'
7
7
  require 'polyrex-objects'
8
8
  require 'polyrex-createobject'
9
- require 'rexml/document'
10
9
  require 'ostruct'
11
10
  require 'polyrex-object-methods'
11
+ require 'rexle'
12
12
 
13
13
  class Polyrex
14
- include REXML
15
14
 
16
15
  def initialize(location)
17
16
  @id = '0'
@@ -26,8 +25,7 @@ class Polyrex
26
25
  end
27
26
 
28
27
  def delete(id=nil)
29
- self.find_by_id(id) if id
30
- @parent_node.parent.delete @parent_node
28
+ @doc.delete("//[@id='#{id}'")
31
29
  end
32
30
 
33
31
  def record()
@@ -45,7 +43,7 @@ class Polyrex
45
43
  # -- start of crud methods --
46
44
 
47
45
  def find_by_id(id)
48
- @parent_node = XPath.first(@doc.root, "//[@id='#{id}']")
46
+ @parent_node = @doc.element("//[@id='#{id}']")
49
47
  self
50
48
  end
51
49
 
@@ -63,25 +61,25 @@ class Polyrex
63
61
  end
64
62
 
65
63
  def xpath(s)
66
- XPath.first(@doc.root, s)
64
+ @doc.element s
67
65
  end
68
66
 
69
67
  def records
70
- XPath.match(@doc.root, "records/*").map do |record|
68
+ @doc.xpath("records/*").map do |record|
71
69
  @objects_a[0].new(record, @id)
72
70
  end
73
71
  end
74
72
 
75
73
  def summary
76
- OpenStruct.new Hash[*XPath.match(@doc.root, "summary/*").map {|x| [x.name, x.text]}.flatten]
74
+ OpenStruct.new Hash[*@doc.xpath("summary/*").map {|x| [x.name, x.text]}.flatten]
77
75
  end
78
76
 
79
77
  private
80
78
 
81
79
  def polyrex_new(schema)
82
80
  # -- required for the parsing feature
83
- doc = Document.new(PolyrexSchema.new(schema).to_s)
84
- @format_masks = XPath.match(doc.root, '//format_mask/text()').map &:to_s
81
+ doc = Rexle.new(PolyrexSchema.new(schema).to_s)
82
+ @format_masks = doc.xpath('//format_mask/text()')
85
83
  schema_rpath = schema.gsub(/\[[^\]]+\]/,'')
86
84
  @recordx = schema_rpath.split('/')
87
85
 
@@ -127,21 +125,22 @@ class Polyrex
127
125
  pattern = patterns.detect {|x| line.match(/#{x.join}/)}.join
128
126
  field_values = line.match(/#{pattern}/).captures
129
127
  field_values += [''] * (@field_names.length - field_values.length)
130
- #field_values = line.match(/#{t}/).captures
131
128
 
132
129
  @id.succ!
133
- record = Element.new(tag_name)
134
- record.add_attribute('id', @id)
135
- summary = Element.new('summary')
130
+
131
+ record = Rexle::Element.new(tag_name)
132
+ record.add_attribute('id' => @id.clone)
133
+ summary = Rexle::Element.new('summary')
136
134
 
137
135
  @field_names.zip(field_values).each do |name, value|
138
- field = Element.new(name.to_s)
136
+ field = Rexle::Element.new(name.to_s)
139
137
  field.text = value
140
138
  summary.add field
141
139
  end
142
- summary.add Element.new('format_mask').add_text(@format_masks[i])
140
+
141
+ summary.add Rexle::Element.new('format_mask').add_text(@format_masks[i])
143
142
 
144
- new_records = Element.new('records')
143
+ new_records = Rexle::Element.new('records')
145
144
 
146
145
  record.add summary
147
146
  record.add new_records
@@ -189,23 +188,22 @@ class Polyrex
189
188
  buffer = File.open(s,'r').read
190
189
  end
191
190
 
192
- @doc = Document.new buffer
191
+ @doc = Rexle.new buffer
193
192
  schema = @doc.root.text('summary/schema')
194
193
 
195
194
  unless @format_masks
196
- @format_masks = XPath.match(@doc.root, '//format_mask/text()').map &:to_s
197
195
  schema_rpath = schema.gsub(/\[[^\]]+\]/,'')
198
196
  @recordx = schema_rpath.split('/')
199
197
  @recordx.shift
200
198
  end
201
199
 
202
- ids = XPath.match(@doc.root, '//@id')
203
- @id = ids.map{|x| x.value.to_i}.max.to_s.succ unless ids.empty?
200
+ id = @doc.xpath('max(//@id)')
201
+ @id = id.to_s.succ if id
204
202
 
205
- #puts 'schema : ' + schema
206
203
  load_handlers(schema)
207
204
  load_find_by(schema)
208
- @parent_node = XPath.first(@doc.root,'records')
205
+
206
+ @parent_node = @doc.element('records')
209
207
 
210
208
  end
211
209
 
@@ -219,7 +217,7 @@ class Polyrex
219
217
  methodx = a.map do |class_name, methods|
220
218
  class_name.downcase!
221
219
  methods.map do |method_name|
222
- xpath = %Q(XPath.first(@doc.root, "//%s[summary/%s='\#\{val\}']")) % [class_name, method_name]
220
+ xpath = %Q(@doc.xpath("//%s[summary/%s='\#\{val\}']")) % [class_name, method_name]
223
221
  "def find_by_%s_%s(val) @parent_node = %s; self.%s end" % [class_name, method_name, xpath, class_name]
224
222
  end
225
223
  end
@@ -227,4 +225,4 @@ class Polyrex
227
225
  self.instance_eval methodx.join("\n")
228
226
  end
229
227
 
230
- end
228
+ end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: polyrex
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.7.4
4
+ version: 0.7.5
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-11-25 00:00:00 +00:00
12
+ date: 2010-12-01 00:00:00 +00:00
13
13
  default_executable:
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency