Sha256: 6da32fe6632d4e38ba0b6a5539f78f1c13a18152627a9be55d9bd6e52ba06a7c

Contents?: true

Size: 879 Bytes

Versions: 6

Compression:

Stored size: 879 Bytes

Contents

require "nokogiri"

module SAXMachine
  
  def self.included(base)
    base.extend ClassMethods
  end
  
  def parse(xml_text)
    sax_handler = SAXHandler.new(self)
    parser = Nokogiri::XML::SAX::Parser.new(sax_handler)
    parser.parse(xml_text)
    self
  end
  
  module ClassMethods

    def parse(xml_text)
      new.parse(xml_text)
    end
    
    def element(name, options = {})
      options[:as] ||= name
      sax_config.add_top_level_element(name, options)
      attr_accessor options[:as]
    end
    
    def elements(name, options = {})
      options[:as] ||= name
      sax_config.add_collection_element(name, options)
      
      class_eval <<-SRC
        def #{options[:as]}
          @#{options[:as]} ||= []
        end
      SRC
      
      attr_writer options[:as]
    end
    
    def sax_config
      @sax_config ||= SAXConfig.new
    end
  end
  
end

Version data entries

6 entries across 6 versions & 2 rubygems

Version Path
ealdent-sax-machine-0.0.1.1 lib/sax-machine/sax_document.rb
ealdent-sax-machine-0.0.1 lib/sax-machine/sax_document.rb
ealdent-sax-machine-0.0.4 lib/sax-machine/sax_document.rb
pauldix-sax-machine-0.0.3 lib/sax-machine/sax_document.rb
pauldix-sax-machine-0.0.4 lib/sax-machine/sax_document.rb
pauldix-sax-machine-0.0.5 lib/sax-machine/sax_document.rb