Class: Rexle

Inherits:
Object
  • Object
show all
Defined in:
lib/rexle.rb

Defined Under Namespace

Classes: Element

Instance Method Summary collapse

Constructor Details

#initialize(x = nil) ⇒ Rexle

Returns a new instance of Rexle.



13
14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/rexle.rb', line 13

def initialize(x=nil)
      
  if x then
    procs = {
     String: proc {|x| parse_string(x)},
     Array: proc {|x| x},
     :"REXML::Document" =>  proc {|x| scan_doc x.root}
    }
    a = procs[x.class.to_s.to_sym].call(x)
    @doc = scan_element(*a)      
  end
  
end

Instance Method Details

#element(xpath) ⇒ Object



291
# File 'lib/rexle.rb', line 291

def element(xpath) @doc.element(xpath) end

#parse(x = nil) ⇒ Object

– end of element –



272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
# File 'lib/rexle.rb', line 272

def parse(x=nil)
  
  a = []
  
  if x then
    procs = {
     String: proc {|x| parse_string(x)},
     Array: proc {|x| x},
     :"REXML::Document" =>  proc {|x| scan_doc x.root}
    }
    a = procs[x.class.to_s.to_sym].call(x)
  else    
    a = yield
  end

  @doc = scan_element(*a)
  self
end

#rootObject



293
# File 'lib/rexle.rb', line 293

def root() @doc end

#text(xpath) ⇒ Object



292
# File 'lib/rexle.rb', line 292

def text(xpath) @doc.text(xpath) end

#xmlObject



294
295
296
297
# File 'lib/rexle.rb', line 294

def xml()
  body = scan_print(self.root.children).join
  "<%s>%s</%s>" % [self.root.name, body, self.root.name]
end

#xpath(path, &blk) ⇒ Object



27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/rexle.rb', line 27

def xpath(path, &blk)

  # is it a function
  fn_match = path.match(/^(\w+)\(([^\)]+)\)$/)

  #    Array: proc {|x| x.flatten.compact}, 
  if fn_match.nil? then      
    procs = {
      Array: proc {|x| block_given? ? x : x.flatten }, 
      String: proc {|x| x},
      :"Rexle::Element" => proc {|x| [x]}
    }
    bucket = []
    result = @doc.xpath(path, bucket, &blk)

    procs[result.class.to_s.to_sym].call(result)
    
  else
    m, xpath_value = fn_match.captures
    method(m.to_sym).call(xpath_value)
  end

end