Class: MySQLXML::MyXListener

Inherits:
Object
  • Object
show all
Defined in:
lib/mysql-xml.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(block) ⇒ MyXListener

Returns a new instance of MyXListener.



26
27
28
29
30
31
32
# File 'lib/mysql-xml.rb', line 26

def initialize(block)
  #Track where we are in the tree.
  @context        = []
  @cur_record     = nil
  @cur_field_name = nil
  @block = block
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(*opts) ⇒ Object

Silence method missing exceptions since we only need to handle some events



61
# File 'lib/mysql-xml.rb', line 61

def method_missing(*opts); end

Instance Attribute Details

#recordsObject (readonly)

Returns the value of attribute records.



24
25
26
# File 'lib/mysql-xml.rb', line 24

def records
  @records
end

Instance Method Details

#tag_end(name) ⇒ Object



49
50
51
52
53
54
55
56
57
# File 'lib/mysql-xml.rb', line 49

def tag_end(name)
  @context.pop
  if name == 'field'
    @cur_field_name = nil
  elsif name == 'row'
    @block.call(@cur_record)
    @cur_record     = nil
  end
end

#tag_start(name, attrs) ⇒ Object



34
35
36
37
38
39
40
41
# File 'lib/mysql-xml.rb', line 34

def tag_start(name, attrs)
  @context << name
  if name == 'row'
    @cur_record = {}
  elsif name == 'field'
    @cur_field_name = attrs['name']
  end
end

#text(text) ⇒ Object



43
44
45
46
47
# File 'lib/mysql-xml.rb', line 43

def text(text)
  if @context[-1] == 'field'
    @cur_record[@cur_field_name] = text
  end
end