Class: HacktiveRecord::Base
- Inherits:
-
Object
- Object
- HacktiveRecord::Base
- Defined in:
- lib/jails/hacktive_record.rb
Constant Summary collapse
- DB =
extend(Support)
SQLite3::Database.new("db/development.sqlite3")
Class Method Summary collapse
-
.all ⇒ Object
Return array of all rows in queried from the database table, converted to objects of the resource.
-
.columns ⇒ Object
Return array of DB column names converted to symbols.
-
.count ⇒ Object
Return number of rows by executing a count query on the database for the resource.
-
.find(id) ⇒ Object
Return an object by querying the database for the requested row searching by id.
-
.table ⇒ Object
Return table name string by transforming the model class’s name to lower case plural.
Instance Method Summary collapse
-
#destroy ⇒ Object
Delete row from database.
-
#save ⇒ Object
Save object as a new row to the database table, returning the object with the new attribute’s id value.
-
#update(attributes = {}) ⇒ Object
Modify row in database.
Class Method Details
.all ⇒ Object
Return array of all rows in queried from the database table, converted to objects of the resource.
31 32 33 34 35 36 37 38 39 40 |
# File 'lib/jails/hacktive_record.rb', line 31 def self.all rows = DB.execute("SELECT * FROM #{table}") # puts(blue("\s #{self} SQL Statement: SELECT * FROM #{table}")) puts("\s #{self} SQL Statement: ".cyan.bold + "SELECT * FROM #{table}".blue.bold) objects = rows.map do |row| attributes = Hash[columns.zip(row)] self.new(attributes) end return objects end |
.columns ⇒ Object
Return array of DB column names converted to symbols.
18 19 20 21 |
# File 'lib/jails/hacktive_record.rb', line 18 def self.columns columns = DB.table_info(table).map { |info| info["name"].to_sym } return columns end |
.count ⇒ Object
Return number of rows by executing a count query on the database for the resource.
24 25 26 27 28 |
# File 'lib/jails/hacktive_record.rb', line 24 def self.count rows_count = DB.execute("SELECT COUNT(*) FROM #{table}")[0][0] puts("\s #{self} SQL Statement: ".cyan.bold + "SELECT COUNT(*) FROM #{table}".blue.bold) return rows_count end |
.find(id) ⇒ Object
Return an object by querying the database for the requested row searching by id.
43 44 45 46 47 48 49 |
# File 'lib/jails/hacktive_record.rb', line 43 def self.find(id) row = DB.execute("SELECT * FROM #{table} WHERE id = ? LIMIT 1", id).first puts("\s #{self} SQL Statement: ".cyan.bold + "SELECT * FROM #{table} WHERE id = #{id} LIMIT 1".blue.bold) attributes = Hash[columns.zip(row)] object = self.new(attributes) return object end |
.table ⇒ Object
Return table name string by transforming the model class’s name to lower case plural.
12 13 14 15 |
# File 'lib/jails/hacktive_record.rb', line 12 def self.table table_name = self.name.downcase + "s" return table_name end |
Instance Method Details
#destroy ⇒ Object
Delete row from database.
76 77 78 79 |
# File 'lib/jails/hacktive_record.rb', line 76 def destroy DB.execute("DELETE FROM #{self.class.table} WHERE id = ?", id) puts("\s #{self.class} SQL Statement: ".cyan.bold + "DELETE FROM #{self.class.table} WHERE id = #{id}".blue.bold) end |
#save ⇒ Object
Save object as a new row to the database table, returning the object with the new attribute’s id value.
52 53 54 55 56 57 58 59 60 61 62 63 |
# File 'lib/jails/hacktive_record.rb', line 52 def save new_object = self columns = new_object.class.columns columns.delete(:id) placeholders = (["?"] * (columns.size)).join(",") values = columns.map { |key| self.send(key) } columns = columns.join(",") DB.execute("INSERT INTO #{self.class.table} (#{columns}) VALUES (#{placeholders})", values) puts("\s #{self.class} SQL Statement: ".cyan.bold + "INSERT INTO #{self.class.table} (#{columns}) VALUES (#{placeholders})".blue.bold + values.to_s) new_object.id = DB.execute("SELECT last_insert_rowid()")[0][0] return new_object end |
#update(attributes = {}) ⇒ Object
Modify row in database.
66 67 68 69 70 71 72 73 |
# File 'lib/jails/hacktive_record.rb', line 66 def update(attributes={}) columns = attributes.keys columns = columns.map { |column| "#{column}=?" }.join(",") values = attributes.values values << id DB.execute("UPDATE #{self.class.table} SET #{columns} WHERE id = ?", values) puts("\s #{self.class} SQL Statement: ".cyan.bold + "UPDATE #{self.class.table} SET #{columns} WHERE id = ?".blue.bold + values.to_s) end |