Class: Makanai::Model

Inherits:
Object
  • Object
show all
Defined in:
lib/makanai/model.rb

Defined Under Namespace

Classes: NotFound

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

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_attributesObject (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 = <<~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

.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 = <<~SQL
    SELECT *
    FROM #{self::TABLE_NAME}
    ORDER BY #{self::PRYMARY_KEY} ASC
    LIMIT 1;
  SQL
  new(execute_sql(sql, db).pop)
end

.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 = <<~SQL
    SELECT *
    FROM #{self::TABLE_NAME}
    ORDER BY #{self::PRYMARY_KEY} DESC
    LIMIT 1;
  SQL
  new(execute_sql(sql, db).pop)
end

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

#attributesObject



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 = <<~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



100
101
102
103
104
105
106
107
# File 'lib/makanai/model.rb', line 100

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



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 = <<~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