Class: Gus::Importer::HashParser

Inherits:
Object
  • Object
show all
Includes:
LibXML::XML::SaxParser::Callbacks
Defined in:
lib/gus/importer/hash_parser.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(callback) ⇒ HashParser

Returns a new instance of HashParser.



6
7
8
9
# File 'lib/gus/importer/hash_parser.rb', line 6

def initialize(callback)
  @callback = callback
  @hash     = {}
end

Class Method Details

.parse(file_path, &block) ⇒ Object



45
46
47
48
49
# File 'lib/gus/importer/hash_parser.rb', line 45

def self.parse(file_path, &block)
  parser = LibXML::XML::SaxParser.file(file_path)
  parser.callbacks = HashParser.new(block)
  parser.parse
end

Instance Method Details

#on_cdata_block(cdata) ⇒ Object



21
22
23
# File 'lib/gus/importer/hash_parser.rb', line 21

def on_cdata_block(cdata)
  #puts "on on_cdata_block element: #{cdata}"
end

#on_characters(chars) ⇒ Object



25
26
27
28
29
30
# File 'lib/gus/importer/hash_parser.rb', line 25

def on_characters(chars)
  if @current_tag == :col && !@current_attr_name.nil?
    @hash[@current_attr_name] ||= ""
    @hash[@current_attr_name] += chars
  end
end

#on_end_element(element) ⇒ Object



32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/gus/importer/hash_parser.rb', line 32

def on_end_element(element)
  if element.to_sym == :row
    @current_tag = nil

    @callback.call(@hash)
    @hash = {}
  end

  if element.to_sym == :col
    @current_attr_name = nil
  end
end

#on_start_element(element, attributes) ⇒ Object



11
12
13
14
15
16
17
18
19
# File 'lib/gus/importer/hash_parser.rb', line 11

def on_start_element(element, attributes)  
  @current_tag = element.to_sym

  if @current_tag == :row
    @hash = {}
  elsif @current_tag == :col
    @current_attr_name = attributes["name"].to_sym
  end
end