Module: SAXMachine::ClassMethods

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

Instance Method Summary collapse

Instance Method Details

#attr_writer_once(attr) ⇒ Object



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

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

#column(sym) ⇒ Object



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

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

#column_namesObject



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

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

#columnsObject



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

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

#data_class(sym) ⇒ Object



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

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

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



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

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



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

def elements(name, options = {})
  options[:as] ||= name
  if options[:class]
    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



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

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)


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

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

#sax_configObject



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

def sax_config
  @sax_config ||= SAXConfig.new
end