module SAXMachine::ClassMethods

Public Instance Methods

ancestor(name, options = {}, &block) click to toggle source
# File lib/sax-machine/sax_document.rb, line 60
def ancestor(name, options = {}, &block)
  real_name = (options[:as] ||= name).to_s
  sax_config.add_ancestor(name, options)
  create_attr(real_name, &block)
end
attribute(name, options = {}, &block) click to toggle source
# File lib/sax-machine/sax_document.rb, line 48
def attribute(name, options = {}, &block)
  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, &block)
end
column(sym) click to toggle source
# File lib/sax-machine/sax_document.rb, line 100
def column(sym)
  columns.select { |c| c.column == sym }[0]
end
column_names() click to toggle source
# File lib/sax-machine/sax_document.rb, line 112
def column_names
  columns.map { |e| e.column }
end
columns() click to toggle source
# File lib/sax-machine/sax_document.rb, line 96
def columns
  sax_config.columns
end
create_attr(real_name, &block) click to toggle source

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.

# File lib/sax-machine/sax_document.rb, line 123
def create_attr(real_name, &block)
  attr_reader(real_name) unless method_defined?(real_name)

  if !method_defined?("#{real_name}=")
    if block_given?
      define_method("#{real_name}=") do |value|
        instance_variable_set("@#{real_name}", instance_exec(value, &block))
      end
    else
      attr_writer(real_name)
    end
  end
end
data_class(sym) click to toggle source
# File lib/sax-machine/sax_document.rb, line 104
def data_class(sym)
  column(sym).data_class
end
element(name, options = {}, &block) click to toggle source
# File lib/sax-machine/sax_document.rb, line 42
def element(name, options = {}, &block)
  real_name = (options[:as] ||= name).to_s
  sax_config.add_top_level_element(name, options)
  create_attr(real_name, &block)
end
elements(name, options = {}, &block) click to toggle source
# File lib/sax-machine/sax_document.rb, line 66
    def elements(name, options = {}, &block)
      real_name = (options[:as] ||= name).to_s

      if options[:class]
        sax_config.add_collection_element(name, options)
      else
        if block_given?
          define_method("add_#{real_name}") do |value|
            send(real_name).send(:<<, instance_exec(value, &block))
          end
        else
          define_method("add_#{real_name}") do |value|
            send(real_name).send(:<<, value)
          end
        end

        sax_config.add_top_level_element(name, options.merge(collection: true))
      end

      if !method_defined?(real_name)
        class_eval <<-SRC
          def #{real_name}
            @#{real_name} ||= []
          end
        SRC
      end

      attr_writer(options[:as]) unless method_defined?("#{options[:as]}=")
    end
inherited(subclass) click to toggle source
# File lib/sax-machine/sax_document.rb, line 34
def inherited(subclass)
  subclass.sax_config.send(:initialize_copy, self.sax_config)
end
parse(*args) click to toggle source
# File lib/sax-machine/sax_document.rb, line 38
def parse(*args)
  new.parse(*args)
end
required?(sym) click to toggle source
# File lib/sax-machine/sax_document.rb, line 108
def required?(sym)
  column(sym).required?
end
sax_config() click to toggle source
# File lib/sax-machine/sax_document.rb, line 116
def sax_config
  @sax_config ||= SAXConfig.new
end
value(name, options = {}, &block) click to toggle source
# File lib/sax-machine/sax_document.rb, line 54
def value(name, options = {}, &block)
  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, &block)
end