Class: ASeriesOfTubes::TubeRecord::SQLObject
- Inherits:
-
Object
- Object
- ASeriesOfTubes::TubeRecord::SQLObject
- Defined in:
- lib/a_series_of_tubes/tube_record/sql_object.rb
Class Method Summary collapse
- .all ⇒ Object
- .columns ⇒ Object
- .finalize! ⇒ Object
- .find(id) ⇒ Object
- .parse_all(results) ⇒ Object
- .table_name ⇒ Object
- .table_name=(table_name) ⇒ Object
Instance Method Summary collapse
- #attribute_values ⇒ Object
- #attributes ⇒ Object
- #destroy ⇒ Object
-
#initialize(params = {}) ⇒ SQLObject
constructor
A new instance of SQLObject.
- #insert ⇒ Object
- #save ⇒ Object
- #update ⇒ Object
Constructor Details
#initialize(params = {}) ⇒ SQLObject
Returns a new instance of SQLObject.
56 57 58 59 60 61 62 63 64 |
# File 'lib/a_series_of_tubes/tube_record/sql_object.rb', line 56 def initialize(params = {}) params.each do |attr_name, value| if self.class.columns.include?(attr_name.to_sym) self.send("#{attr_name}=", value) else raise "unknown attribute '#{attr_name}'" end end end |
Class Method Details
.all ⇒ Object
13 14 15 16 17 18 19 20 |
# File 'lib/a_series_of_tubes/tube_record/sql_object.rb', line 13 def self.all self.parse_all(DBConnection.execute(<<-SQL)) SELECT * FROM #{self.table_name} SQL end |
.columns ⇒ Object
4 5 6 7 8 9 10 11 |
# File 'lib/a_series_of_tubes/tube_record/sql_object.rb', line 4 def self.columns @columns ||= DBConnection.execute2(<<-SQL).first.map(&:to_sym) SELECT * FROM #{self.table_name} SQL end |
.finalize! ⇒ Object
41 42 43 44 45 46 |
# File 'lib/a_series_of_tubes/tube_record/sql_object.rb', line 41 def self.finalize! self.columns.each do |column| define_method(column) { self.attributes[column] } define_method("#{column}=") { |value| self.attributes[column] = value } end end |
.find(id) ⇒ Object
26 27 28 29 30 31 32 33 34 35 36 37 38 39 |
# File 'lib/a_series_of_tubes/tube_record/sql_object.rb', line 26 def self.find(id) result = DBConnection.execute(<<-SQL, id: id).first SELECT * FROM #{self.table_name} WHERE #{self.table_name}.id = :id LIMIT 1 SQL self.new(result) if result end |
.parse_all(results) ⇒ Object
22 23 24 |
# File 'lib/a_series_of_tubes/tube_record/sql_object.rb', line 22 def self.parse_all(results) results.map { |params| self.new(params) } end |
.table_name ⇒ Object
48 49 50 |
# File 'lib/a_series_of_tubes/tube_record/sql_object.rb', line 48 def self.table_name @table_name ||= self.to_s.tableize end |
.table_name=(table_name) ⇒ Object
52 53 54 |
# File 'lib/a_series_of_tubes/tube_record/sql_object.rb', line 52 def self.table_name=(table_name) @table_name = table_name end |
Instance Method Details
#attribute_values ⇒ Object
70 71 72 |
# File 'lib/a_series_of_tubes/tube_record/sql_object.rb', line 70 def attribute_values @attributes.keys.map { |k| @attributes[k] } end |
#attributes ⇒ Object
66 67 68 |
# File 'lib/a_series_of_tubes/tube_record/sql_object.rb', line 66 def attributes @attributes ||= {} end |
#destroy ⇒ Object
106 107 108 109 110 111 112 113 114 115 |
# File 'lib/a_series_of_tubes/tube_record/sql_object.rb', line 106 def destroy return unless self.id ASeriesOfTubes::TubeRecord::DBConnection.execute(<<-SQL) DELETE FROM #{self.class.table_name} WHERE id = #{self.id} SQL end |
#insert ⇒ Object
74 75 76 77 78 79 80 81 82 83 84 85 86 |
# File 'lib/a_series_of_tubes/tube_record/sql_object.rb', line 74 def insert columns = self.class.columns.drop(1) question_marks = (['?'] * columns.length) ASeriesOfTubes::TubeRecord::DBConnection.execute(<<-SQL, *attribute_values) INSERT INTO #{self.class.table_name} (#{columns.join(',')}) VALUES (#{question_marks.join(',')}) SQL self.id = ASeriesOfTubes::TubeRecord::DBConnection.last_insert_row_id end |
#save ⇒ Object
102 103 104 |
# File 'lib/a_series_of_tubes/tube_record/sql_object.rb', line 102 def save self.id ? self.update : self.insert end |
#update ⇒ Object
88 89 90 91 92 93 94 95 96 97 98 99 100 |
# File 'lib/a_series_of_tubes/tube_record/sql_object.rb', line 88 def update columns = self.class.columns set_values = columns.map { |attr_name| "#{attr_name} = ?" } ASeriesOfTubes::TubeRecord::DBConnection.execute(<<-SQL, *attribute_values) UPDATE #{self.class.table_name} SET #{set_values.join(',')} WHERE id = #{self.id} SQL end |