Class: Makanai::Model
- Inherits:
-
Object
- Object
- Makanai::Model
- Defined in:
- lib/makanai/model.rb
Defined Under Namespace
Classes: NotFound
Instance Attribute Summary collapse
-
#origin_attributes ⇒ Object
readonly
Returns the value of attribute origin_attributes.
Class Method Summary collapse
- .all(db = Makanai::Database.new) ⇒ Object
- .buid_sql_text(value) ⇒ Object
- .execute_sql(sql, db = Makanai::Database.new) ⇒ Object
- .find(key, db = Makanai::Database.new) ⇒ Object
- .first(db = Makanai::Database.new) ⇒ Object
- .last(db = Makanai::Database.new) ⇒ Object
Instance Method Summary collapse
- #assign_attributes(attributes) ⇒ Object
- #attributes ⇒ Object
- #create(db = Makanai::Database.new) ⇒ Object
- #delete(db = Makanai::Database.new) ⇒ Object
-
#initialize(attributes) ⇒ Model
constructor
A new instance of Model.
- #update(db = Makanai::Database.new) ⇒ Object
Constructor Details
#initialize(attributes) ⇒ Model
Returns a new instance of Model.
9 10 11 12 |
# File 'lib/makanai/model.rb', line 9 def initialize(attributes) @origin_attributes = attributes difine_attribute_methods end |
Instance Attribute Details
#origin_attributes ⇒ Object (readonly)
Returns the value of attribute origin_attributes.
14 15 16 |
# File 'lib/makanai/model.rb', line 14 def origin_attributes @origin_attributes end |
Class Method Details
.all(db = Makanai::Database.new) ⇒ Object
20 21 22 23 |
# File 'lib/makanai/model.rb', line 20 def self.all(db = Makanai::Database.new) results = execute_sql("SELECT * FROM #{self::TABLE_NAME};", db) results.map { |result| new(result) } end |
.buid_sql_text(value) ⇒ Object
57 58 59 60 61 62 63 |
# File 'lib/makanai/model.rb', line 57 def self.buid_sql_text(value) case value when String then "'#{value.gsub(/'/, "''")}'" when Numeric, Integer then value else value end end |
.execute_sql(sql, db = Makanai::Database.new) ⇒ Object
16 17 18 |
# File 'lib/makanai/model.rb', line 16 def self.execute_sql(sql, db = Makanai::Database.new) db.execute_sql(sql) end |
.find(key, db = Makanai::Database.new) ⇒ Object
25 26 27 28 29 30 31 32 33 34 35 |
# File 'lib/makanai/model.rb', line 25 def self.find(key, db = Makanai::Database.new) sql = <<~SQL SELECT * FROM #{self::TABLE_NAME} WHERE #{self::PRYMARY_KEY} = #{buid_sql_text(key)} LIMIT 1; SQL results = execute_sql(sql, db) raise Makanai::Model::NotFound if results.empty? new(results.pop) end |
Instance Method Details
#assign_attributes(attributes) ⇒ Object
65 66 67 68 |
# File 'lib/makanai/model.rb', line 65 def assign_attributes(attributes) attributes.each { |key, val| send("#{key}=", val) } self end |
#attributes ⇒ Object
70 71 72 |
# File 'lib/makanai/model.rb', line 70 def attributes origin_attributes.map { |key, _val| [key, send(key)] }.to_h end |
#create(db = Makanai::Database.new) ⇒ Object
74 75 76 77 78 79 80 81 82 83 84 |
# File 'lib/makanai/model.rb', line 74 def create(db = Makanai::Database.new) sql = <<~SQL INSERT INTO #{self.class::TABLE_NAME}(#{clumns.join(',')}) VALUES (#{insert_values.join(',')}); SQL self.class.execute_sql(sql, db) @origin_attributes = attributes difine_attribute_methods self end |
#delete(db = Makanai::Database.new) ⇒ Object
98 99 100 101 102 103 104 105 |
# File 'lib/makanai/model.rb', line 98 def delete(db = Makanai::Database.new) sql = <<~SQL DELETE FROM #{self.class::TABLE_NAME} WHERE #{self.class::PRYMARY_KEY} = #{self.class.buid_sql_text(send(self.class::PRYMARY_KEY))}; SQL self.class.execute_sql(sql, db) nil end |
#update(db = Makanai::Database.new) ⇒ Object
86 87 88 89 90 91 92 93 94 95 96 |
# File 'lib/makanai/model.rb', line 86 def update(db = Makanai::Database.new) sql = <<~SQL UPDATE #{self.class::TABLE_NAME} SET #{update_values.join(',')} WHERE #{self.class::PRYMARY_KEY} = #{self.class.buid_sql_text(send(self.class::PRYMARY_KEY))}; SQL self.class.execute_sql(sql, db) @origin_attributes = attributes difine_attribute_methods self end |