class Makanai::Model

Attributes

origin_attributes[R]

Public Class Methods

all(db = Makanai::Database.new) click to toggle source
# 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) click to toggle source
# 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) click to toggle source
# 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) click to toggle source
# 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) click to toggle source
# 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) click to toggle source
# 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
new(attributes) click to toggle source
# File lib/makanai/model.rb, line 9
def initialize(attributes)
  @origin_attributes = attributes
  difine_attribute_methods
end

Public Instance Methods

assign_attributes(attributes) click to toggle source
# File lib/makanai/model.rb, line 67
def assign_attributes(attributes)
  attributes.each { |key, val| send("#{key}=", val) }
  self
end
attributes() click to toggle source
# File lib/makanai/model.rb, line 72
def attributes
  origin_attributes.map { |key, _val| [key, send(key)] }.to_h
end
create(db = Makanai::Database.new) click to toggle source
# 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) click to toggle source
# 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) click to toggle source
# 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

Private Instance Methods

clumns() click to toggle source
# File lib/makanai/model.rb, line 111
def clumns
  attributes.keys
end
difine_attribute_methods() click to toggle source
# File lib/makanai/model.rb, line 123
def difine_attribute_methods
  origin_attributes.each do |key, val|
    instance_variable_set("@#{key}".to_sym, val)
    self.class.class_eval { attr_accessor key.to_sym }
  end
end
insert_values() click to toggle source
# File lib/makanai/model.rb, line 115
def insert_values
  attributes.values.map { |attribute| self.class.buid_sql_text(attribute).to_s }
end
update_values() click to toggle source
# File lib/makanai/model.rb, line 119
def update_values
  attributes.map { |key, val| "#{key}=#{self.class.buid_sql_text(val)}" }
end