Module: SAXMachine::ClassMethods

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

Instance Method Summary collapse

Instance Method Details

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



60
61
62
63
64
# 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) ⇒ Object



48
49
50
51
52
# 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) ⇒ Object



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

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

#column_namesObject



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

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

#columnsObject



96
97
98
# File 'lib/sax-machine/sax_document.rb', line 96

def columns
  sax_config.columns
end

#create_attr(real_name, &block) ⇒ 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.



123
124
125
126
127
128
129
130
131
132
133
134
135
# 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) ⇒ Object



104
105
106
# File 'lib/sax-machine/sax_document.rb', line 104

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

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



42
43
44
45
46
# 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) ⇒ Object



66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
# 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) ⇒ 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)


108
109
110
# File 'lib/sax-machine/sax_document.rb', line 108

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

#sax_configObject



116
117
118
# File 'lib/sax-machine/sax_document.rb', line 116

def sax_config
  @sax_config ||= SAXConfig.new
end

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



54
55
56
57
58
# 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