Class: SQLObject

Inherits:
Object show all
Defined in:
lib/activeleopard/sql_object.rb

Direct Known Subclasses

ActiveLeopard::Base

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(params = {}) ⇒ SQLObject

Returns a new instance of SQLObject.



126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
# File 'lib/activeleopard/sql_object.rb', line 126

def initialize(params = {})
  self.class.columns.each do |attr_name|
    params_val = params[attr_name] || params[attr_name.to_s]
    params_val.strip! if params_val
    send("#{attr_name}=", params_val)
  end

  params.each do |attr_name, val|
    attr_method = "#{attr_name}="
    next if self.class.columns.include?(attr_name.to_sym)
    next unless self.respond_to?(attr_method)

    val.strip! if val
    send(attr_method, val)
  end

  if self.class.callbacks[:after_initialize]
    send(self.class.callbacks[:after_initialize])
  end
end

Class Method Details

.after_initialize(method_name) ⇒ Object



2
3
4
# File 'lib/activeleopard/sql_object.rb', line 2

def self.after_initialize(method_name)
  callbacks[:after_initialize] = method_name
end

.allObject



6
7
8
# File 'lib/activeleopard/sql_object.rb', line 6

def self.all
  Relation.new({}, self)
end

.callbacksObject



10
11
12
# File 'lib/activeleopard/sql_object.rb', line 10

def self.callbacks
  @callbacks ||= {}
end

.columnsObject



14
15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/activeleopard/sql_object.rb', line 14

def self.columns
  return @columns if @columns

  cols = DBConnection.execute(<<-SQL, [self.table_name])
    SELECT
      column_name
    FROM
      information_schema.columns
    WHERE
      table_name = $1
  SQL

  @columns = cols.map { |c| c['column_name'].to_sym }
end

.destroy_allObject



29
30
31
32
33
# File 'lib/activeleopard/sql_object.rb', line 29

def self.destroy_all
  DBConnection.execute(<<-SQL)
    DELETE FROM #{self.table_name}
  SQL
end

.finalize!Object



35
36
37
38
39
40
41
42
43
44
45
# File 'lib/activeleopard/sql_object.rb', line 35

def self.finalize!
  self.columns.each do |col|
    define_method(col) do #setter method
      attributes[col]
    end

    define_method("#{col}=") do |val| #getter method
      attributes[col] = val
    end
  end
end

.firstObject



47
48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/activeleopard/sql_object.rb', line 47

def self.first
  first_data = DBConnection.get_first_row(<<-SQL)
  SELECT
    *
  FROM
    #{self.table_name}
  ORDER BY
    id
  LIMIT
    1
  SQL

  self.new(first_data)
end

.lastObject



62
63
64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/activeleopard/sql_object.rb', line 62

def self.last
  last_data = DBConnection.get_first_row(<<-SQL)
  SELECT
    *
  FROM
    #{self.table_name}
  ORDER BY
    id DESC
  LIMIT
    1
  SQL

  self.new(last_data)
end

.parse_all(all_options) ⇒ Object



77
78
79
# File 'lib/activeleopard/sql_object.rb', line 77

def self.parse_all(all_options)
  all_options.map { |options| self.new(options) }
end

.table_nameObject



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

def self.table_name
  @table_name ||= self.to_s.tableize.gsub('humen') { 'humans' }
end

.table_name=(table_name) ⇒ Object



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

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

.validationsObject



89
90
91
# File 'lib/activeleopard/sql_object.rb', line 89

def self.validations
  @validations ||= []
end

Instance Method Details

#attr_countObject



97
98
99
# File 'lib/activeleopard/sql_object.rb', line 97

def attr_count
  @attributes.count
end

#attr_values_to_updateObject



105
106
107
# File 'lib/activeleopard/sql_object.rb', line 105

def attr_values_to_update
  attributes.reject { |attr_name, _| attr_name == :id }.values
end

#attribute_valuesObject



101
102
103
# File 'lib/activeleopard/sql_object.rb', line 101

def attribute_values
  attributes.values
end

#attributesObject



93
94
95
# File 'lib/activeleopard/sql_object.rb', line 93

def attributes
  @attributes ||= {}
end

#col_namesObject



109
110
111
112
113
# File 'lib/activeleopard/sql_object.rb', line 109

def col_names
  self.class.columns
  .map(&:to_s)
  .drop(1).join(', ')
end

#destroyObject



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

def destroy
  DBConnection.execute(<<-SQL, [self.id])
    DELETE FROM #{self.class.table_name}
    WHERE id = $1
  SQL
end

#errorsObject



122
123
124
# File 'lib/activeleopard/sql_object.rb', line 122

def errors
  @errors ||= Hash.new { |h, k| h[k] = [] }
end

#insertObject



147
148
149
150
151
152
153
154
155
156
157
158
# File 'lib/activeleopard/sql_object.rb', line 147

def insert
  result = DBConnection.execute(<<-SQL, attr_values_to_update)
  INSERT INTO
    #{self.class.table_name} (#{col_names})
  VALUES
    (#{question_marks})
  RETURNING
    id
  SQL

  self.id = result.getvalue(0,0)
end

#question_marksObject



160
161
162
# File 'lib/activeleopard/sql_object.rb', line 160

def question_marks
  (1...attributes.count).map { |n| "$#{n}"}.join(', ')
end

#saveObject



164
165
166
167
168
169
170
171
172
173
# File 'lib/activeleopard/sql_object.rb', line 164

def save
  validate!
  if valid?
    id ? update : insert
    true
  else
    show_errors
    false
  end
end

#show_errorsObject



175
176
177
178
179
# File 'lib/activeleopard/sql_object.rb', line 175

def show_errors
  errors.each do |key, messages|
    messages.each { |m| puts "#{key} #{m}" }
  end
end

#to_sObject



181
182
183
# File 'lib/activeleopard/sql_object.rb', line 181

def to_s
  "#{self.class}:#{self.object_id}"
end

#updateObject



189
190
191
192
193
194
195
196
197
198
199
# File 'lib/activeleopard/sql_object.rb', line 189

def update
  vals = attr_values_to_update << id
  DBConnection.execute(<<-SQL, vals)
    UPDATE
      #{self.class.table_name}
    SET
      #{update_set_line}
    WHERE
      id = $#{vals.length}
  SQL
end

#update_set_lineObject



185
186
187
# File 'lib/activeleopard/sql_object.rb', line 185

def update_set_line
  col_names.split(', ').map.with_index { |c, i| "#{c} = $#{i + 1}" }.join(', ')
end

#valid?Boolean

Returns:

  • (Boolean)


209
210
211
212
# File 'lib/activeleopard/sql_object.rb', line 209

def valid?
  validate!
  errors.all? { |_, v| v.empty? }
end

#validate!Object



201
202
203
204
205
206
207
# File 'lib/activeleopard/sql_object.rb', line 201

def validate!
  @errors = Hash.new { |h, k| h[k] = [] }

  self.class.validations.each do |validation|
    self.send(validation)
  end
end