Class: Panda::Record::Base

Inherits:
Object show all
Includes:
BaseHelper
Defined in:
lib/panda/record/base.rb

Class Method Summary collapse

Instance Method Summary collapse

Methods included from BaseHelper

included

Constructor Details

#initialize(attributes = {}) ⇒ Base

Returns a new instance of Base.



9
10
11
# File 'lib/panda/record/base.rb', line 9

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

Class Method Details

.allObject



62
63
64
65
66
# File 'lib/panda/record/base.rb', line 62

def self.all
  Database.execute_query(
    "SELECT * FROM #{table} ORDER BY id ASC"
  ).map(&method(:get_model_object))
end

.countObject



94
95
96
# File 'lib/panda/record/base.rb', line 94

def self.count
  Database.execute_query("SELECT COUNT (*) FROM #{table}")[0][0]
end

.create(attributes) ⇒ Object



56
57
58
59
60
# File 'lib/panda/record/base.rb', line 56

def self.create(attributes)
  model = new(attributes)
  model.save
  true
end

.create_tableObject



48
49
50
51
52
53
54
# File 'lib/panda/record/base.rb', line 48

def self.create_table
  Database.execute_query(
    "CREATE TABLE IF NOT EXISTS #{table} "  \
    "(#{column_names_with_constraints.join(', ')})"
  )
  build_column_methods
end

.destroy(id) ⇒ Object



98
99
100
# File 'lib/panda/record/base.rb', line 98

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

.destroy_allObject



102
103
104
# File 'lib/panda/record/base.rb', line 102

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

.find(id) ⇒ Object



68
69
70
71
72
73
74
# File 'lib/panda/record/base.rb', line 68

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

.find_by(option) ⇒ Object



76
77
78
79
80
81
82
# File 'lib/panda/record/base.rb', line 76

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

.property(column_name, constraints) ⇒ Object



43
44
45
46
# File 'lib/panda/record/base.rb', line 43

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

.to_table(name) ⇒ Object



39
40
41
# File 'lib/panda/record/base.rb', line 39

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

Instance Method Details

#destroyObject



35
36
37
# File 'lib/panda/record/base.rb', line 35

def destroy
  self.class.destroy(id)
end

#saveObject Also known as: save!



13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/panda/record/base.rb', line 13

def save
  query = if id
            "UPDATE #{model_table} SET " \
            "#{update_placeholders} WHERE id = ?"
          else
            "INSERT INTO #{model_table} (#{current_table_columns})" \
            " VALUES (#{current_table_placeholders})"
          end
  values = id ? record_values << id : record_values
  Database.execute_query(query, values)
  true
end

#update(attributes) ⇒ Object



26
27
28
29
30
31
# File 'lib/panda/record/base.rb', line 26

def update(attributes)
  attributes.each do |key, value|
    send("#{key}=", value)
  end
  save
end