Class: RailzLite::SQLObject

Inherits:
Object
  • Object
show all
Extended by:
Associatable, Searchable
Defined in:
lib/railz_lite/models/sql_object.rb

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Associatable

assoc_options, belongs_to, has_many

Methods included from Searchable

where

Constructor Details

#initialize(params = {}) ⇒ SQLObject

Returns a new instance of SQLObject.



73
74
75
76
77
78
79
# File 'lib/railz_lite/models/sql_object.rb', line 73

def initialize(params = {})
  params.each do |attr_name, val|
    name_sym = attr_name.to_sym
    raise Exception.new "unknown attribute '#{attr_name}'" unless self.class.columns.include?(name_sym)
    send("#{name_sym}=", val)
  end
end

Class Method Details

.allObject



40
41
42
43
44
45
46
47
48
# File 'lib/railz_lite/models/sql_object.rb', line 40

def self.all
  results = DBConnection.execute(<<-SQL)
  SELECT
    *
  FROM
    #{table_name};
SQL
  parse_all(results)
end

.columnsObject



11
12
13
14
15
16
17
18
19
# File 'lib/railz_lite/models/sql_object.rb', line 11

def self.columns
  @columns ||= DBConnection.execute2(<<-SQL)
  SELECT
    *
  FROM
    #{table_name};
SQL
  @columns.first.map(&:to_sym)
end

.columns_sans_idObject

for insert statements we don’t want to include the id field



69
70
71
# File 'lib/railz_lite/models/sql_object.rb', line 69

def self.columns_sans_id
  self.columns.reject { |col| col == :id }
end

.finalize!Object



21
22
23
24
25
26
27
28
29
30
# File 'lib/railz_lite/models/sql_object.rb', line 21

def self.finalize!
  columns.each do |name|
    define_method(name) do
      attributes[name]
    end
    define_method("#{name}=") do |val|
      attributes[name] = val
    end
  end
end

.find(id) ⇒ Object



54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/railz_lite/models/sql_object.rb', line 54

def self.find(id)
  target = DBConnection.execute(<<-SQL, id)
  SELECT
    *
  FROM
    #{table_name}
  WHERE
    ID = ?;
SQL

  return nil if target.empty?
  self.new(target.first)
end

.parse_all(results) ⇒ Object



50
51
52
# File 'lib/railz_lite/models/sql_object.rb', line 50

def self.parse_all(results)
  results.map { |attrs| self.new(attrs) }
end

.table_nameObject



36
37
38
# File 'lib/railz_lite/models/sql_object.rb', line 36

def self.table_name
  @table_name || self.name.tableize 
end

.table_name=(table_name) ⇒ Object



32
33
34
# File 'lib/railz_lite/models/sql_object.rb', line 32

def self.table_name=(table_name)
  @table_name = table_name
end

Instance Method Details

#attribute_valuesObject



85
86
87
# File 'lib/railz_lite/models/sql_object.rb', line 85

def attribute_values
  self.class.columns.map { |attr| send(attr) }
end

#attribute_values_sans_idObject

see columns_sans_id



90
91
92
# File 'lib/railz_lite/models/sql_object.rb', line 90

def attribute_values_sans_id
  self.class.columns_sans_id.map { |attr| send(attr) }
end

#attributesObject



81
82
83
# File 'lib/railz_lite/models/sql_object.rb', line 81

def attributes
  @attributes ||= {}
end

#insertObject



94
95
96
97
98
99
100
101
102
# File 'lib/railz_lite/models/sql_object.rb', line 94

def insert
  last_row_id = DBConnection.insert(<<-SQL, *attribute_values_sans_id)
  INSERT INTO
    #{self.class.table_name}(#{self.class.columns_sans_id.join(',')})
  VALUES
    (#{(["?"] * attribute_values_sans_id.length).join(',')});
SQL
  self.id = last_row_id
end

#saveObject



115
116
117
118
119
120
121
# File 'lib/railz_lite/models/sql_object.rb', line 115

def save
  if self.id.nil?
    insert
  else
    update
  end
end

#updateObject



104
105
106
107
108
109
110
111
112
113
# File 'lib/railz_lite/models/sql_object.rb', line 104

def update
  DBConnection.execute(<<-SQL, *attribute_values_sans_id, self.id)
  UPDATE
    #{self.class.table_name}
  SET
    #{self.class.columns_sans_id.map { |attr_name| "#{attr_name}=?"}.join(',')}
  WHERE
    id = ?;
SQL
end