Module: SAXMachine::ClassMethods
- Defined in:
- lib/sax-machine/sax_document.rb
Instance Method Summary collapse
- #attr_writer_once(attr) ⇒ Object
- #column(sym) ⇒ Object
- #column_names ⇒ Object
- #columns ⇒ Object
- #data_class(sym) ⇒ Object
- #element(name, options = {}) ⇒ Object
- #elements(name, options = {}) ⇒ Object
- #parse(xml_text) ⇒ Object
- #required?(sym) ⇒ Boolean
- #sax_config ⇒ Object
Instance Method Details
#attr_writer_once(attr) ⇒ Object
98 99 100 101 102 103 104 |
# File 'lib/sax-machine/sax_document.rb', line 98 def attr_writer_once(attr) class_eval <<-SRC def #{attr}=(val) @#{attr} ||= val end SRC end |
#column(sym) ⇒ Object
54 55 56 |
# File 'lib/sax-machine/sax_document.rb', line 54 def column(sym) (sax_config.top_level_elements[sym.to_s] || []).first 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
46 47 48 49 50 51 52 |
# File 'lib/sax-machine/sax_document.rb', line 46 def columns r = [] sax_config.top_level_elements.each do |name, ecs| r += ecs end r 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
35 36 37 38 39 40 41 42 43 44 |
# File 'lib/sax-machine/sax_document.rb', line 35 def element(name, = {}) [:as] ||= name sax_config.add_top_level_element(name, ) # 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. attr_reader [:as] unless instance_methods.include?([:as].to_s) attr_writer_once [:as] unless instance_methods.include?("#{[:as]}=") 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] || [:events] 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 !instance_methods.include?([:as].to_s) class_eval <<-SRC def #{[:as]} @#{[:as]} ||= [] end SRC end attr_writer [:as] unless instance_methods.include?("#{[:as]}=") end |
#parse(xml_text) ⇒ Object
28 29 30 31 32 33 |
# File 'lib/sax-machine/sax_document.rb', line 28 def parse(xml_text) # It might be cleaner to aditionally call parse_finish here, but # then Nokogiri/libxml2 barfs on incomplete documents. Desired # behaviour? new.parse(xml_text) end |
#required?(sym) ⇒ Boolean
62 63 64 |
# File 'lib/sax-machine/sax_document.rb', line 62 def required?(sym) column(sym).required? end |