Class: Vundabar::BaseModel

Inherits:
Object show all
Extended by:
Associations
Includes:
ModelHelper
Defined in:
lib/vundabar/orm/base_model.rb

Class Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Associations

belongs_to, has_many

Methods included from ModelHelper

included

Constructor Details

#initialize(attributes = {}) ⇒ BaseModel

Returns a new instance of BaseModel.



5
6
7
# File 'lib/vundabar/orm/base_model.rb', line 5

def initialize(attributes = {})
  attributes.each { |column, value| send("#{column}=", value) }
end

Class Attribute Details

.propertiesObject (readonly)

Returns the value of attribute properties.



10
11
12
# File 'lib/vundabar/orm/base_model.rb', line 10

def properties
  @properties
end

Class Method Details

.allObject



72
73
74
75
76
77
# File 'lib/vundabar/orm/base_model.rb', line 72

def self.all
  query = "SELECT * FROM #{table_name} "\
    "ORDER BY id DESC"
  result = Database.execute_query query
  result.map { |row| get_model_object(row) }
end

.countObject



87
88
89
90
# File 'lib/vundabar/orm/base_model.rb', line 87

def self.count
  result = Database.execute_query "SELECT COUNT(*) FROM #{table_name}"
  result.first.first
end

.create(attributes) ⇒ Object



79
80
81
82
83
84
85
# File 'lib/vundabar/orm/base_model.rb', line 79

def self.create(attributes)
  model_object = new(attributes)
  model_object.save
  id = Database.execute_query "SELECT last_insert_rowid()"
  model_object.id = id.first.first
  model_object
end

.create_tableObject



31
32
33
34
35
36
# File 'lib/vundabar/orm/base_model.rb', line 31

def self.create_table
  query = "CREATE TABLE IF NOT EXISTS #{table_name} "\
  "(#{build_table_fields(@properties).join(', ')})"
  Database.execute_query(query)
  make_methods
end

.destroy(id) ⇒ Object



115
116
117
# File 'lib/vundabar/orm/base_model.rb', line 115

def self.destroy(id)
  Database.execute_query "DELETE FROM #{table_name} WHERE id= ?", id
end

.destroy_allObject



119
120
121
# File 'lib/vundabar/orm/base_model.rb', line 119

def self.destroy_all
  Database.execute_query "DELETE FROM #{table_name}"
end

.find(id) ⇒ Object



101
102
103
104
105
106
# File 'lib/vundabar/orm/base_model.rb', line 101

def self.find(id)
  query = "SELECT * FROM #{table_name} "\
  "WHERE id= ?"
  row = Database.execute_query(query, id).first
  get_model_object(row) if row
end

.find_by(option) ⇒ Object



108
109
110
111
112
113
# File 'lib/vundabar/orm/base_model.rb', line 108

def self.find_by(option)
  query = "SELECT * FROM #{table_name} "\
  "WHERE #{option.keys.first}= ?"
  row = Database.execute_query(query, option.values.first).first
  get_model_object(row) if row
end

.property(column_name, column_properties) ⇒ Object



26
27
28
29
# File 'lib/vundabar/orm/base_model.rb', line 26

def self.property(column_name, column_properties)
  @properties ||= {}
  @properties[column_name] = column_properties
end

.to_table(name) ⇒ Object



22
23
24
# File 'lib/vundabar/orm/base_model.rb', line 22

def self.to_table(name)
  @table = name.to_s
end

.where(query_string, value) ⇒ Object



123
124
125
126
127
# File 'lib/vundabar/orm/base_model.rb', line 123

def self.where(query_string, value)
  data = Database.execute_query "SELECT * FROM "\
  "#{table_name} WHERE #{query_string}", value
  data.map { |row| get_model_object(row) }
end

Instance Method Details

#destroyObject



45
46
47
48
# File 'lib/vundabar/orm/base_model.rb', line 45

def destroy
  table = self.class.table_name
  Database.execute_query "DELETE FROM #{table} WHERE id= ?", id
end

#saveObject Also known as: save!



50
51
52
53
54
55
56
57
58
59
60
# File 'lib/vundabar/orm/base_model.rb', line 50

def save
  table = self.class.table_name
  query = if id
            "UPDATE #{table} SET #{update_placeholders} WHERE id = ?"
          else
            "INSERT INTO #{table} (#{table_columns}) VALUES "\
            "(#{record_placeholders})"
          end
  values = id ? record_values << send("id") : record_values
  Database.execute_query query, values
end

#to_hshObject



62
63
64
65
66
67
68
# File 'lib/vundabar/orm/base_model.rb', line 62

def to_hsh
  model_hsh = {}
  self.class.properties.keys.each do |key|
    model_hsh[key] = send(key)
  end
  model_hsh
end

#update(attributes) ⇒ Object



38
39
40
41
42
43
# File 'lib/vundabar/orm/base_model.rb', line 38

def update(attributes)
  table = self.class.table_name
  query = "UPDATE #{table} SET #{update_placeholders(attributes)}"\
  " WHERE id= ?"
  Database.execute_query(query, update_values(attributes))
end