class Peictt::BaseModel

Attributes

table[RW]

Public Class Methods

all() click to toggle source
# File lib/peictt/orm/base_model.rb, line 41
def self.all
  result = DatabaseMapper.get_all self
  all = []
  result.each do |row|
    all << convert_to_object(row)
  end
  all
end
create(attributes) click to toggle source
# File lib/peictt/orm/base_model.rb, line 58
def self.create(attributes)
  model = new(attributes)
  model.save
  find_by attributes
end
destroy_all() click to toggle source
# File lib/peictt/orm/base_model.rb, line 37
def self.destroy_all
  DatabaseMapper.destroy_all(table)
end
find_by(attributes) click to toggle source
# File lib/peictt/orm/base_model.rb, line 50
def self.find_by(attributes)
  self.table = to_s.downcase.pluralize
  result = DatabaseMapper.find_by(self, attributes)
  return result if result.nil?

  convert_to_object result
end
new(attributes = {}) click to toggle source
# File lib/peictt/orm/base_model.rb, line 7
def initialize(attributes = {})
  self.class.table = self.class.to_s.downcase.pluralize
  self.class.set_methods
  attributes.each do |key, value|
    send("#{key}=", value)
  end unless attributes.empty?
  self
end

Private Class Methods

convert_to_object(result) click to toggle source
# File lib/peictt/orm/base_model.rb, line 95
def self.convert_to_object(result)
  attributes = get_columns_from_table.zip(result).to_h
  item = new attributes
  parse_string_to_time item
  item
end
get_columns_from_table() click to toggle source
# File lib/peictt/orm/base_model.rb, line 84
def self.get_columns_from_table
  Database.connect.table_info(table).
    map { |column| column["name"] }
end
make_methods(columns) click to toggle source
# File lib/peictt/orm/base_model.rb, line 89
def self.make_methods(columns)
  columns.each do |column|
    attr_accessor column.to_sym
  end
end
parse_string_to_time(model) click to toggle source
# File lib/peictt/orm/base_model.rb, line 79
def self.parse_string_to_time(model)
  model.created_at = model.created_at.to_time unless model.created_at.nil?
  model.updated_at = model.updated_at.to_time unless model.updated_at.nil?
end
parse_time_to_string(model) click to toggle source
# File lib/peictt/orm/base_model.rb, line 74
def self.parse_time_to_string(model)
  model.updated_at = Time.now.to_s if model.respond_to? :updated_at
  model.created_at = model.created_at.to_s if model.respond_to? :created_at
end
set_methods() click to toggle source
# File lib/peictt/orm/base_model.rb, line 70
def self.set_methods
  make_methods get_columns_from_table
end

Public Instance Methods

destroy() click to toggle source
# File lib/peictt/orm/base_model.rb, line 33
def destroy
  DatabaseMapper.destroy self
end
save() click to toggle source
# File lib/peictt/orm/base_model.rb, line 16
def save
  if @id
    self.class.parse_time_to_string self
    return DatabaseMapper.new(self, :update).save
  else
    return DatabaseMapper.new(self).save
  end
end
update(attributes) click to toggle source
# File lib/peictt/orm/base_model.rb, line 25
def update(attributes)
  attributes.each do |key, value|
    send("#{key}=", value)
  end
  save
  self
end