Module: SAXMachine::ClassMethods

Defined in:
lib/sax-machine/sax_document.rb

Instance Method Summary collapse

Instance Method Details

#element(name, options = {}) ⇒ Object



22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/sax-machine/sax_document.rb', line 22

def element(name, options = {})
  options[:as] ||= name
  sax_config.add_top_level_element(name, options)
  
  # we only want to insert the setter if they haven't defined it from elsewhere.
  # this is how we allow custom parsing behavior. So you could define the setter
  # and have it parse the string into a date or whatever.
  if instance_methods.include?("#{options[:as]}=")
    attr_reader options[:as]
  else
    attr_accessor options[:as]
  end
end

#elements(name, options = {}) ⇒ Object



36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/sax-machine/sax_document.rb', line 36

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

#parse(xml_text) ⇒ Object



18
19
20
# File 'lib/sax-machine/sax_document.rb', line 18

def parse(xml_text)
  new.parse(xml_text)
end

#sax_configObject



49
50
51
# File 'lib/sax-machine/sax_document.rb', line 49

def sax_config
  @sax_config ||= SAXConfig.new
end