Module: SAXMachine::ClassMethods
- Defined in:
- lib/sax-machine/sax_document.rb
Instance Method Summary collapse
- #ancestor(name, options = {}, &block) ⇒ Object
- #attribute(name, options = {}, &block) ⇒ Object
- #column(sym) ⇒ Object
- #column_names ⇒ Object
- #columns ⇒ Object
-
#create_attr(real_name, &block) ⇒ 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 = {}, &block) ⇒ Object
- #elements(name, options = {}, &block) ⇒ Object
- #inherited(subclass) ⇒ Object
- #parse(*args) ⇒ Object
- #required?(sym) ⇒ Boolean
- #sax_config ⇒ Object
- #value(name, options = {}, &block) ⇒ Object
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, = {}, &block) real_name = ([:as] ||= name).to_s sax_config.add_ancestor(name, ) 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, = {}, &block) real_name = ([:as] ||= name).to_s sax_config.add_top_level_attribute(self.class.to_s, .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_names ⇒ Object
112 113 114 |
# File 'lib/sax-machine/sax_document.rb', line 112 def column_names columns.map { |e| e.column } end |
#columns ⇒ Object
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, = {}, &block) real_name = ([:as] ||= name).to_s sax_config.add_top_level_element(name, ) 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, = {}, &block) real_name = ([:as] ||= name).to_s if [:class] sax_config.add_collection_element(name, ) 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, .merge(collection: true)) end if !method_defined?(real_name) class_eval <<-SRC def #{real_name} @#{real_name} ||= [] end SRC end attr_writer([:as]) unless method_defined?("#{[: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
108 109 110 |
# File 'lib/sax-machine/sax_document.rb', line 108 def required?(sym) column(sym).required? end |
#sax_config ⇒ Object
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, = {}, &block) real_name = ([:as] ||= name).to_s sax_config.add_top_level_element_value(self.class.to_s, .merge(name: name)) create_attr(real_name, &block) end |