rexle 0.3.10 → 0.4.0

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/rexle.rb +41 -15
  2. metadata +2 -2
data/lib/rexle.rb CHANGED
@@ -6,8 +6,12 @@ require 'rexleparser'
6
6
 
7
7
  class Rexle
8
8
 
9
- def initialize(s)
10
- a = RexleParser.new(s).to_a
9
+ def initialize(x)
10
+ procs = {
11
+ String: proc {|x| RexleParser.new(x).to_a},
12
+ Array: proc {|x| x}
13
+ }
14
+ a = procs[x.class.to_s.to_sym].call(x)
11
15
  @doc = scan_element(*a)
12
16
  end
13
17
 
@@ -35,8 +39,9 @@ class Rexle
35
39
 
36
40
  class Element
37
41
  attr_accessor :name, :value
42
+ attr_reader :child_lookup
38
43
 
39
- def initialize(name=nil, value='', attributes={})
44
+ def initialize(name=nil, value='', attributFes={})
40
45
  @name, @value, @attributes = name, value, attributes
41
46
  raise "Element name must not be blank" unless name
42
47
  @child_elements = []
@@ -127,24 +132,42 @@ class Rexle
127
132
  end
128
133
 
129
134
  def format_attributes(condition)
130
- raw_items = condition[1..-1].scan(/\'[^\']+\'|and|\d+|[!=]+|[\w]+/)
131
-
135
+ raw_items = condition[1..-1].scan(/\'[^\']*\'|and|\d+|[!=]+|[@\w]+/)
136
+
132
137
  if raw_items[0][/^\d+$/] then
133
138
  return raw_items[0].to_i
134
139
  else
140
+
135
141
  and_items = raw_items.map.with_index.select{|x,i| x == 'and'}.map{|x| [x.last, x.last + 1]}.flatten
136
142
  indices = [0] + and_items + [raw_items.length]
137
- cons_items = indices.each_cons(2).map{|x,y| raw_items.slice(x...y)}
138
- items = cons_items.map do |x|
139
- if x.length >= 3 then
140
- x[1] = '==' if x[1] == '='
141
- "h['%s'] %s %s" % x
142
- else
143
- x
143
+
144
+ if raw_items[0][0] == '@' then
145
+ raw_items[0].sub!(/^@/,'')
146
+ cons_items = indices.each_cons(2).map{|x,y| raw_items.slice(x...y)}
147
+ items = cons_items.map do |x|
148
+ if x.length >= 3 then
149
+ x[1] = '==' if x[1] == '='
150
+ "h['%s'] %s %s" % x
151
+ else
152
+ x
153
+ end
144
154
  end
145
- end
146
155
 
147
- return items.join(' ')
156
+ return items.join(' ')
157
+ else
158
+
159
+ cons_items = indices.each_cons(2).map{|x,y| raw_items.slice(x...y)}
160
+ items = cons_items.map do |x|
161
+ if x.length >= 3 then
162
+ x[1] = '==' if x[1] == '='
163
+ "name == '%s' and value %s %s" % x
164
+ else
165
+ x
166
+ end
167
+ end
168
+
169
+ return items.join(' ')
170
+ end
148
171
  end
149
172
  end
150
173
 
@@ -175,7 +198,10 @@ class Rexle
175
198
  if attr_search then
176
199
  if attr_search.is_a? Fixnum then
177
200
  block_given? ? blk.call(e) : e if i == attr_search
178
- elsif h and eval(attr_search)
201
+ elsif h and attr_search[/^h\[/] and eval(attr_search)
202
+ block_given? ? blk.call(e) : e
203
+ elsif attr_search[/^name ==/] and \
204
+ e.child_lookup.select{|name, attributes, value| eval(attr_search) }.length > 0
179
205
  block_given? ? blk.call(e) : e
180
206
  end
181
207
  else
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: rexle
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.3.10
4
+ version: 0.4.0
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-11 00:00:00 +00:00
12
+ date: 2010-11-14 00:00:00 +00:00
13
13
  default_executable:
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency