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
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 64 65 |
# File 'lib/makanai/model.rb', line 57 def self.buid_sql_text(value) # rubocop:disable Lint/DuplicateBranch case value when String then "'#{value.gsub(/'/, "''")}'" when Numeric, Integer then value else value end # rubocop:enable Lint/DuplicateBranch 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 = " SELECT *\n FROM \#{self::TABLE_NAME}\n WHERE \#{self::PRYMARY_KEY} = \#{buid_sql_text(key)}\n LIMIT 1;\n SQL\n results = execute_sql(sql, db)\n raise Makanai::Model::NotFound if results.empty?\n new(results.pop)\nend\n" |
.first(db = Makanai::Database.new) ⇒ Object
37 38 39 40 41 42 43 44 45 |
# File 'lib/makanai/model.rb', line 37 def self.first(db = Makanai::Database.new) sql = " SELECT *\n FROM \#{self::TABLE_NAME}\n ORDER BY \#{self::PRYMARY_KEY} ASC\n LIMIT 1;\n SQL\n new(execute_sql(sql, db).pop)\nend\n" |
.last(db = Makanai::Database.new) ⇒ Object
47 48 49 50 51 52 53 54 55 |
# File 'lib/makanai/model.rb', line 47 def self.last(db = Makanai::Database.new) sql = " SELECT *\n FROM \#{self::TABLE_NAME}\n ORDER BY \#{self::PRYMARY_KEY} DESC\n LIMIT 1;\n SQL\n new(execute_sql(sql, db).pop)\nend\n" |
Instance Method Details
#assign_attributes(attributes) ⇒ Object
67 68 69 70 |
# File 'lib/makanai/model.rb', line 67 def assign_attributes(attributes) attributes.each { |key, val| send("#{key}=", val) } self end |
#attributes ⇒ Object
72 73 74 |
# File 'lib/makanai/model.rb', line 72 def attributes origin_attributes.to_h { |key, _val| [key, send(key)] } end |
#create(db = Makanai::Database.new) ⇒ Object
76 77 78 79 80 81 82 83 84 85 86 |
# File 'lib/makanai/model.rb', line 76 def create(db = Makanai::Database.new) sql = " INSERT\n INTO \#{self.class::TABLE_NAME}(\#{clumns.join(',')})\n VALUES (\#{insert_values.join(',')});\n SQL\n self.class.execute_sql(sql, db)\n @origin_attributes = attributes\n difine_attribute_methods\n self\nend\n" |
#delete(db = Makanai::Database.new) ⇒ Object
100 101 102 103 104 105 106 107 |
# File 'lib/makanai/model.rb', line 100 def delete(db = Makanai::Database.new) sql = " DELETE FROM \#{self.class::TABLE_NAME}\n WHERE \#{self.class::PRYMARY_KEY} = \#{self.class.buid_sql_text(send(self.class::PRYMARY_KEY))};\n SQL\n self.class.execute_sql(sql, db)\n nil\nend\n" |
#update(db = Makanai::Database.new) ⇒ Object
88 89 90 91 92 93 94 95 96 97 98 |
# File 'lib/makanai/model.rb', line 88 def update(db = Makanai::Database.new) sql = " UPDATE \#{self.class::TABLE_NAME}\n SET \#{update_values.join(',')}\n WHERE \#{self.class::PRYMARY_KEY} = \#{self.class.buid_sql_text(send(self.class::PRYMARY_KEY))};\n SQL\n self.class.execute_sql(sql, db)\n @origin_attributes = attributes\n difine_attribute_methods\n self\nend\n" |