Module: SAXMachine::ClassMethods
- Defined in:
- lib/sax-machine/sax_document.rb
Instance Method Summary collapse
- #ancestor(name, options = {}) ⇒ Object
- #attribute(name, options = {}) ⇒ Object
- #column(sym) ⇒ Object
- #column_names ⇒ Object
- #columns ⇒ Object
-
#create_attr(real_name) ⇒ Object
we only want to insert the getter and setter if they haven’t defined it from elsewhere.
- #data_class(sym) ⇒ Object
- #element(name, options = {}) ⇒ Object
- #elements(name, options = {}) ⇒ Object
- #inherited(subclass) ⇒ Object
- #parse(xml_text, on_error = nil, on_warning = nil) ⇒ Object
- #required?(sym) ⇒ Boolean
- #sax_config ⇒ Object
- #value(name, options = {}) ⇒ Object
Instance Method Details
#ancestor(name, options = {}) ⇒ Object
44 45 46 47 48 |
# File 'lib/sax-machine/sax_document.rb', line 44 def ancestor(name, = {}) real_name = ([:as] ||= name).to_s sax_config.add_ancestor(name, ) 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, = {}) real_name = ([:as] ||= name).to_s sax_config.add_top_level_attribute(self.class.to_s, .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_names ⇒ Object
66 67 68 |
# File 'lib/sax-machine/sax_document.rb', line 66 def column_names columns.map { |e| e.column } end |
#columns ⇒ Object
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, = {}) real_name = ([:as] ||= name).to_s sax_config.add_top_level_element(name, ) 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, = {}) [:as] ||= name if [:class] sax_config.add_collection_element(name, ) else class_eval <<-SRC def add_#{[:as]}(value) #{[:as]} << value end SRC sax_config.add_top_level_element(name, .merge(:collection => true)) end if !method_defined?([:as].to_s) class_eval <<-SRC def #{[:as]} @#{[:as]} ||= [] end SRC end attr_writer [:as] unless method_defined?("#{[: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
62 63 64 |
# File 'lib/sax-machine/sax_document.rb', line 62 def required?(sym) column(sym).required? end |
#sax_config ⇒ Object
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, = {}) real_name = ([:as] ||= name).to_s sax_config.add_top_level_element_value(self.class.to_s, .merge(:name => name)) create_attr real_name end |