Module: SAXMachine::ClassMethods

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

Instance Method Summary collapse

Instance Method Details

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



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

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



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

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



70
71
72
# File 'lib/sax-machine/sax_document.rb', line 70

def column(sym)
  columns.select { |c| c.column == sym }[0]
end

#column_namesObject



82
83
84
# File 'lib/sax-machine/sax_document.rb', line 82

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

#columnsObject



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

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.



118
119
120
121
# File 'lib/sax-machine/sax_document.rb', line 118

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



74
75
76
# File 'lib/sax-machine/sax_document.rb', line 74

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

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



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

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



86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
# File 'lib/sax-machine/sax_document.rb', line 86

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



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

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

#parse(*args) ⇒ Object



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

def parse(*args)
  new.parse(*args)
end

#required?(sym) ⇒ Boolean

Returns:

  • (Boolean)


78
79
80
# File 'lib/sax-machine/sax_document.rb', line 78

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

#sax_configObject



111
112
113
# File 'lib/sax-machine/sax_document.rb', line 111

def sax_config
  @sax_config ||= SAXConfig.new
end

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



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

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