Module: SAXMachine::ClassMethods

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

Instance Method Summary collapse

Instance Method Details

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



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

def ancestor(name, options = {})
  real_name = (options[:as] ||= name).to_s
  sax_config.add_ancestor(name, options)
  create_attr(real_name)
end

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



32
33
34
35
36
# File 'lib/sax-machine/sax_document.rb', line 32

def attribute(name, options = {})
  real_name = (options[:as] ||= name).to_s
  sax_config.add_top_level_attribute(self.class.to_s, options.merge(:name => name))
  create_attr real_name
end

#column(sym) ⇒ Object



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

def column(sym)
  columns.select { |c| c.column == sym }[0]
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



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

def columns
  sax_config.columns
end

#create_attr(real_name) ⇒ Object

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.



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

def create_attr real_name
  attr_reader real_name unless method_defined?(real_name)
  attr_writer real_name unless method_defined?("#{real_name}=")
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



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

def element(name, options = {})
  real_name = (options[:as] ||= name).to_s
  sax_config.add_top_level_element(name, options)
  create_attr real_name
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]
    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 !method_defined?(options[:as].to_s)
    class_eval <<-SRC
      def #{options[:as]}
        @#{options[:as]} ||= []
      end
    SRC
  end

  attr_writer options[:as] unless method_defined?("#{options[:as]}=")
end

#inherited(subclass) ⇒ Object



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

def inherited(subclass)
  subclass.sax_config.send(:initialize_copy, self.sax_config)
end

#parse(xml_text, on_error = nil, on_warning = nil) ⇒ Object



22
23
24
# File 'lib/sax-machine/sax_document.rb', line 22

def parse(xml_text, on_error = nil, on_warning = nil)
  new.parse(xml_text, on_error, on_warning)
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

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



38
39
40
41
42
# File 'lib/sax-machine/sax_document.rb', line 38

def value(name, options = {})
  real_name = (options[:as] ||= name).to_s
  sax_config.add_top_level_element_value(self.class.to_s, options.merge(:name => name))
  create_attr real_name
end