Class: SQLObject
Direct Known Subclasses
Class Method Summary collapse
- .after_initialize(method_name) ⇒ Object
- .all ⇒ Object
- .callbacks ⇒ Object
- .columns ⇒ Object
- .destroy_all ⇒ Object
- .finalize! ⇒ Object
- .first ⇒ Object
- .last ⇒ Object
- .parse_all(all_options) ⇒ Object
- .table_name ⇒ Object
- .table_name=(table_name) ⇒ Object
- .validations ⇒ Object
Instance Method Summary collapse
- #attr_count ⇒ Object
- #attr_values_to_update ⇒ Object
- #attribute_values ⇒ Object
- #attributes ⇒ Object
- #col_names ⇒ Object
- #destroy ⇒ Object
- #errors ⇒ Object
-
#initialize(params = {}) ⇒ SQLObject
constructor
A new instance of SQLObject.
- #insert ⇒ Object
- #question_marks ⇒ Object
- #save ⇒ Object
- #show_errors ⇒ Object
- #to_s ⇒ Object
- #update ⇒ Object
- #update_set_line ⇒ Object
- #valid? ⇒ Boolean
- #validate! ⇒ Object
Constructor Details
#initialize(params = {}) ⇒ SQLObject
Returns a new instance of SQLObject.
126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 |
# File 'lib/activeleopard/sql_object.rb', line 126 def initialize(params = {}) self.class.columns.each do |attr_name| params_val = params[attr_name] || params[attr_name.to_s] params_val.strip! if params_val send("#{attr_name}=", params_val) end params.each do |attr_name, val| attr_method = "#{attr_name}=" next if self.class.columns.include?(attr_name.to_sym) next unless self.respond_to?(attr_method) val.strip! if val send(attr_method, val) end if self.class.callbacks[:after_initialize] send(self.class.callbacks[:after_initialize]) end end |
Class Method Details
.after_initialize(method_name) ⇒ Object
2 3 4 |
# File 'lib/activeleopard/sql_object.rb', line 2 def self.after_initialize(method_name) callbacks[:after_initialize] = method_name end |
.all ⇒ Object
6 7 8 |
# File 'lib/activeleopard/sql_object.rb', line 6 def self.all Relation.new({}, self) end |
.callbacks ⇒ Object
10 11 12 |
# File 'lib/activeleopard/sql_object.rb', line 10 def self.callbacks @callbacks ||= {} end |
.columns ⇒ Object
14 15 16 17 18 19 20 21 22 23 24 25 26 27 |
# File 'lib/activeleopard/sql_object.rb', line 14 def self.columns return @columns if @columns cols = DBConnection.execute(<<-SQL, [self.table_name]) SELECT column_name FROM information_schema.columns WHERE table_name = $1 SQL @columns = cols.map { |c| c['column_name'].to_sym } end |
.destroy_all ⇒ Object
29 30 31 32 33 |
# File 'lib/activeleopard/sql_object.rb', line 29 def self.destroy_all DBConnection.execute(<<-SQL) DELETE FROM #{self.table_name} SQL end |
.finalize! ⇒ Object
35 36 37 38 39 40 41 42 43 44 45 |
# File 'lib/activeleopard/sql_object.rb', line 35 def self.finalize! self.columns.each do |col| define_method(col) do #setter method attributes[col] end define_method("#{col}=") do |val| #getter method attributes[col] = val end end end |
.first ⇒ Object
47 48 49 50 51 52 53 54 55 56 57 58 59 60 |
# File 'lib/activeleopard/sql_object.rb', line 47 def self.first first_data = DBConnection.get_first_row(<<-SQL) SELECT * FROM #{self.table_name} ORDER BY id LIMIT 1 SQL self.new(first_data) end |
.last ⇒ Object
62 63 64 65 66 67 68 69 70 71 72 73 74 75 |
# File 'lib/activeleopard/sql_object.rb', line 62 def self.last last_data = DBConnection.get_first_row(<<-SQL) SELECT * FROM #{self.table_name} ORDER BY id DESC LIMIT 1 SQL self.new(last_data) end |
.parse_all(all_options) ⇒ Object
77 78 79 |
# File 'lib/activeleopard/sql_object.rb', line 77 def self.parse_all() .map { || self.new() } end |
.table_name ⇒ Object
85 86 87 |
# File 'lib/activeleopard/sql_object.rb', line 85 def self.table_name @table_name ||= self.to_s.tableize.gsub('humen') { 'humans' } end |
.table_name=(table_name) ⇒ Object
81 82 83 |
# File 'lib/activeleopard/sql_object.rb', line 81 def self.table_name=(table_name) @table_name = table_name end |
.validations ⇒ Object
89 90 91 |
# File 'lib/activeleopard/sql_object.rb', line 89 def self.validations @validations ||= [] end |
Instance Method Details
#attr_count ⇒ Object
97 98 99 |
# File 'lib/activeleopard/sql_object.rb', line 97 def attr_count @attributes.count end |
#attr_values_to_update ⇒ Object
105 106 107 |
# File 'lib/activeleopard/sql_object.rb', line 105 def attr_values_to_update attributes.reject { |attr_name, _| attr_name == :id }.values end |
#attribute_values ⇒ Object
101 102 103 |
# File 'lib/activeleopard/sql_object.rb', line 101 def attribute_values attributes.values end |
#attributes ⇒ Object
93 94 95 |
# File 'lib/activeleopard/sql_object.rb', line 93 def attributes @attributes ||= {} end |
#col_names ⇒ Object
109 110 111 112 113 |
# File 'lib/activeleopard/sql_object.rb', line 109 def col_names self.class.columns .map(&:to_s) .drop(1).join(', ') end |
#destroy ⇒ Object
115 116 117 118 119 120 |
# File 'lib/activeleopard/sql_object.rb', line 115 def destroy DBConnection.execute(<<-SQL, [self.id]) DELETE FROM #{self.class.table_name} WHERE id = $1 SQL end |
#errors ⇒ Object
122 123 124 |
# File 'lib/activeleopard/sql_object.rb', line 122 def errors @errors ||= Hash.new { |h, k| h[k] = [] } end |
#insert ⇒ Object
147 148 149 150 151 152 153 154 155 156 157 158 |
# File 'lib/activeleopard/sql_object.rb', line 147 def insert result = DBConnection.execute(<<-SQL, attr_values_to_update) INSERT INTO #{self.class.table_name} (#{col_names}) VALUES (#{question_marks}) RETURNING id SQL self.id = result.getvalue(0,0) end |
#question_marks ⇒ Object
160 161 162 |
# File 'lib/activeleopard/sql_object.rb', line 160 def question_marks (1...attributes.count).map { |n| "$#{n}"}.join(', ') end |
#save ⇒ Object
164 165 166 167 168 169 170 171 172 173 |
# File 'lib/activeleopard/sql_object.rb', line 164 def save validate! if valid? id ? update : insert true else show_errors false end end |
#show_errors ⇒ Object
175 176 177 178 179 |
# File 'lib/activeleopard/sql_object.rb', line 175 def show_errors errors.each do |key, | .each { |m| puts "#{key} #{m}" } end end |
#to_s ⇒ Object
181 182 183 |
# File 'lib/activeleopard/sql_object.rb', line 181 def to_s "#{self.class}:#{self.object_id}" end |
#update ⇒ Object
189 190 191 192 193 194 195 196 197 198 199 |
# File 'lib/activeleopard/sql_object.rb', line 189 def update vals = attr_values_to_update << id DBConnection.execute(<<-SQL, vals) UPDATE #{self.class.table_name} SET #{update_set_line} WHERE id = $#{vals.length} SQL end |
#update_set_line ⇒ Object
185 186 187 |
# File 'lib/activeleopard/sql_object.rb', line 185 def update_set_line col_names.split(', ').map.with_index { |c, i| "#{c} = $#{i + 1}" }.join(', ') end |
#valid? ⇒ Boolean
209 210 211 212 |
# File 'lib/activeleopard/sql_object.rb', line 209 def valid? validate! errors.all? { |_, v| v.empty? } end |
#validate! ⇒ Object
201 202 203 204 205 206 207 |
# File 'lib/activeleopard/sql_object.rb', line 201 def validate! @errors = Hash.new { |h, k| h[k] = [] } self.class.validations.each do |validation| self.send(validation) end end |