Module: SAXMachine::ClassMethods

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

Instance Method Summary collapse

Instance Method Details

#attr_writer_once(attr) ⇒ Object



98
99
100
101
102
103
104
# File 'lib/sax-machine/sax_document.rb', line 98

def attr_writer_once(attr)
  class_eval <<-SRC
      def #{attr}=(val)
        @#{attr} ||= val
      end
    SRC
end

#column(sym) ⇒ Object



54
55
56
# File 'lib/sax-machine/sax_document.rb', line 54

def column(sym)
  (sax_config.top_level_elements[sym.to_s] || []).first
end

#column_namesObject



66
67
68
# File 'lib/sax-machine/sax_document.rb', line 66

def column_names
  columns.map{|e| e.column}
end

#columnsObject



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

def columns
  r = []
  sax_config.top_level_elements.each do |name, ecs|
    r += ecs
  end
  r
end

#data_class(sym) ⇒ Object



58
59
60
# File 'lib/sax-machine/sax_document.rb', line 58

def data_class(sym)
  column(sym).data_class
end

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



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

def element(name, options = {})
  options[:as] ||= name
  sax_config.add_top_level_element(name, options)
  
  # we only want to insert the getter and 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.
  attr_reader options[:as] unless instance_methods.include?(options[:as].to_s)
  attr_writer_once options[:as] unless instance_methods.include?("#{options[:as]}=")
end

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



70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
# File 'lib/sax-machine/sax_document.rb', line 70

def elements(name, options = {})
  options[:as] ||= name
  if options[:class] || options[:events]
    sax_config.add_collection_element(name, options)
  else
    class_eval <<-SRC
      def add_#{options[:as]}(value)
        #{options[:as]} << value
      end
    SRC
    sax_config.add_top_level_element(name, options.merge(:collection => true))
  end
  
  if !instance_methods.include?(options[:as].to_s)
  class_eval <<-SRC
      def #{options[:as]}
        @#{options[:as]} ||= []
      end
    SRC
  end
  
  attr_writer options[:as] unless instance_methods.include?("#{options[:as]}=")
end

#parse(xml_text) ⇒ Object



28
29
30
31
32
33
# File 'lib/sax-machine/sax_document.rb', line 28

def parse(xml_text)
  # It might be cleaner to aditionally call parse_finish here, but
  # then Nokogiri/libxml2 barfs on incomplete documents. Desired
  # behaviour?
  new.parse(xml_text)
end

#required?(sym) ⇒ Boolean

Returns:

  • (Boolean)


62
63
64
# File 'lib/sax-machine/sax_document.rb', line 62

def required?(sym)
  column(sym).required?
end

#sax_configObject



94
95
96
# File 'lib/sax-machine/sax_document.rb', line 94

def sax_config
  @sax_config ||= SAXConfig.new
end