Class: Mgt::ActiveRecord
- Defined in:
- lib/orm/active_record.rb,
lib/orm/query_generator.rb
Class Method Summary collapse
- .all ⇒ Object
- .attribute(column_name, args) ⇒ Object
- .attributes_keys ⇒ Object
- .create_table ⇒ Object
- .destroy(id) ⇒ Object
- .destroy_all ⇒ Object
- .find(id) ⇒ Object
- .find_by(finder) ⇒ Object
- .first ⇒ Object
- .last ⇒ Object
- .make_methods ⇒ Object
- .map_object(row) ⇒ Object
- .nullable_query(value = true) ⇒ Object
- .primary_key_query(value = false) ⇒ Object
- .table(table_name) ⇒ Object
- .table_name ⇒ Object
- .type_query(value) ⇒ Object
- .where(query_pattern, value) ⇒ Object
Instance Method Summary collapse
- #destroy ⇒ Object
- #get_columns ⇒ Object
- #get_values ⇒ Object
- #new_record_placeholders ⇒ Object
- #new_record_value ⇒ Object
- #save ⇒ Object
- #update(params) ⇒ Object
- #update_records ⇒ Object
- #update_records_placeholders ⇒ Object
- #update_values(params) ⇒ Object
- #update_values_placeholders(params) ⇒ Object
Class Method Details
.all ⇒ Object
11 12 13 14 15 16 17 |
# File 'lib/orm/query_generator.rb', line 11 def self.all data = Database.execute "SELECT #{attributes_keys.join(',')} FROM #{@table} ORDER BY id DESC" data.map do |row| map_object(row) end end |
.attribute(column_name, args) ⇒ Object
13 14 15 16 |
# File 'lib/orm/active_record.rb', line 13 def self.attribute(column_name, args) @attributes ||= {} @attributes[column_name] = args end |
.attributes_keys ⇒ Object
51 52 53 |
# File 'lib/orm/active_record.rb', line 51 def self.attributes_keys @attributes.keys end |
.create_table ⇒ Object
18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 |
# File 'lib/orm/active_record.rb', line 18 def self.create_table all_attributes = [] @attributes.each do |key, value| attributes ||= [] attributes << key.to_s value.each do |name, type| attributes << send("#{name.downcase}_query", type) end all_attributes << attributes.join(" ") end query = "CREATE TABLE IF NOT EXISTS #{@table} (#{all_attributes.join(', ')})" Database.execute(query) make_methods end |
.destroy(id) ⇒ Object
45 46 47 |
# File 'lib/orm/query_generator.rb', line 45 def self.destroy(id) Database.execute "DELETE FROM #{table_name} WHERE id = ?", id end |
.destroy_all ⇒ Object
71 72 73 |
# File 'lib/orm/query_generator.rb', line 71 def self.destroy_all Database.execute "DELETE FROM #{@table}" end |
.find(id) ⇒ Object
39 40 41 42 43 |
# File 'lib/orm/query_generator.rb', line 39 def self.find(id) row = Database.execute("SELECT #{attributes_keys.join(',')} FROM #{@table} WHERE id = ?", id).first map_object(row) end |
.find_by(finder) ⇒ Object
3 4 5 6 7 8 9 |
# File 'lib/orm/query_generator.rb', line 3 def self.find_by(finder) key = finder.keys[0].to_s value = finder.values[0].to_s row = Database.execute("SELECT #{attributes_keys.join(',')} FROM #{@table} WHERE #{key} = ?", value).first map_object(row) end |
.first ⇒ Object
33 34 35 36 37 |
# File 'lib/orm/query_generator.rb', line 33 def self.first query = "SELECT * FROM #{@table} ORDER BY id LIMIT 1" row = (Database.execute query).first map_object(row) end |
.last ⇒ Object
27 28 29 30 31 |
# File 'lib/orm/query_generator.rb', line 27 def self.last query = "SELECT * FROM #{@table} ORDER BY id DESC LIMIT 1" row = (Database.execute query).first map_object(row) end |
.make_methods ⇒ Object
34 35 36 37 |
# File 'lib/orm/active_record.rb', line 34 def self.make_methods mtds = @attributes.keys.map(&:to_sym) mtds.each { |mtd| attr_accessor mtd } end |
.map_object(row) ⇒ Object
55 56 57 58 59 60 61 |
# File 'lib/orm/active_record.rb', line 55 def self.map_object(row) model_name = new @attributes.each_key.with_index do |value, index| model_name.send("#{value}=", row[index]) end model_name end |
.nullable_query(value = true) ⇒ Object
43 44 45 |
# File 'lib/orm/active_record.rb', line 43 def self.nullable_query(value = true) "NOT NULL" unless value end |
.primary_key_query(value = false) ⇒ Object
39 40 41 |
# File 'lib/orm/active_record.rb', line 39 def self.primary_key_query(value = false) "PRIMARY KEY AUTOINCREMENT" if value end |
.table(table_name) ⇒ Object
5 6 7 |
# File 'lib/orm/active_record.rb', line 5 def self.table(table_name) @table = table_name end |
.table_name ⇒ Object
9 10 11 |
# File 'lib/orm/active_record.rb', line 9 def self.table_name @table end |
.type_query(value) ⇒ Object
47 48 49 |
# File 'lib/orm/active_record.rb', line 47 def self.type_query(value) value.to_s end |
Instance Method Details
#destroy ⇒ Object
66 67 68 69 |
# File 'lib/orm/query_generator.rb', line 66 def destroy table_name = self.class.table_name Database.execute "DELETE FROM #{table_name} WHERE id = ?", id end |
#get_columns ⇒ Object
63 64 65 66 67 |
# File 'lib/orm/active_record.rb', line 63 def get_columns columns = self.class.attributes_keys columns.delete(:id) columns.join(",") end |
#get_values ⇒ Object
69 70 71 72 73 |
# File 'lib/orm/active_record.rb', line 69 def get_values attributes = self.class.attributes_keys attributes.delete(:id) attributes.map { |method| send(method) } end |
#new_record_placeholders ⇒ Object
87 88 89 90 |
# File 'lib/orm/active_record.rb', line 87 def new_record_placeholders attributes = self.class.attributes_keys (["?"] * (attributes.size - 1)).join(",") end |
#new_record_value ⇒ Object
75 76 77 |
# File 'lib/orm/active_record.rb', line 75 def new_record_value get_values end |
#save ⇒ Object
49 50 51 52 53 54 55 56 57 58 |
# File 'lib/orm/query_generator.rb', line 49 def save table_name = self.class.table_name if id Database.execute "UPDATE #{table_name} SET #{update_records_placeholders} WHERE id = ?", update_records else Database.execute "INSERT INTO #{table_name} (#{get_columns}) VALUES (#{new_record_placeholders})", new_record_value end end |
#update(params) ⇒ Object
60 61 62 63 64 |
# File 'lib/orm/query_generator.rb', line 60 def update(params) table_name = self.class.table_name Database.execute "UPDATE #{table_name} SET #{update_values_placeholders(params)} WHERE id=?", update_values(params) end |
#update_records ⇒ Object
79 80 81 |
# File 'lib/orm/active_record.rb', line 79 def update_records get_values << send(:id) end |
#update_records_placeholders ⇒ Object
98 99 100 101 102 |
# File 'lib/orm/active_record.rb', line 98 def update_records_placeholders columns = self.class.attributes_keys columns.delete(:id) columns.map { |col| "#{col}=?" }.join(",") end |
#update_values(params) ⇒ Object
83 84 85 |
# File 'lib/orm/active_record.rb', line 83 def update_values(params) params.values << id end |
#update_values_placeholders(params) ⇒ Object
92 93 94 95 96 |
# File 'lib/orm/active_record.rb', line 92 def update_values_placeholders(params) columns = params.keys columns.delete(:id) columns.map { |col| "#{col}=?" }.join(",") end |