class Gus::Importer::HashParser

Public Class Methods

new(callback) click to toggle source
# File lib/gus/importer/hash_parser.rb, line 6
def initialize(callback)
  @callback = callback
  @hash     = {}
end
parse(file_path, &block) click to toggle source
# 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

Public Instance Methods

on_cdata_block(cdata) click to toggle source
# 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) click to toggle source
# 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) click to toggle source
# 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) click to toggle source
# 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