Class: WORMY::Base

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

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Searchable

where

Methods included from Associatable

association_options, belongs_to, has_many, has_many_through, has_one_through

Constructor Details

#initialize(params = {}) ⇒ Base

Returns a new instance of Base.



99
100
101
102
103
104
# File 'lib/wormy.rb', line 99

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

Class Method Details

.allObject



11
12
13
14
15
16
17
18
19
20
# File 'lib/wormy.rb', line 11

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

  parse_all(results)
end

.columnsObject



22
23
24
25
26
27
28
29
30
# File 'lib/wormy.rb', line 22

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

.count(params) ⇒ Object



32
33
34
# File 'lib/wormy.rb', line 32

def self.count(params)
  where(params).count
end

.destroy_all(params) ⇒ Object



36
37
38
# File 'lib/wormy.rb', line 36

def self.destroy_all(params)
  self.where(params).each(&:destroy)
end

.finalize!Object



40
41
42
43
44
45
46
# File 'lib/wormy.rb', line 40

def self.finalize!
  # make sure to call finalize! at the end of any class that inherits from WORMY::Base
  columns.each do |col|
    define_method(col) { attributes[col] }
    define_method("#{col}=") { |new_attr| attributes[col] = new_attr }
  end
end

.find(id) ⇒ Object



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

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

  parse_all(result).first
end

.firstObject



61
62
63
# File 'lib/wormy.rb', line 61

def self.first
  all.first
end

.lastObject



65
66
67
# File 'lib/wormy.rb', line 65

def self.last
  all.last
end

.parse_all(results) ⇒ Object



69
70
71
72
# File 'lib/wormy.rb', line 69

def self.parse_all(results)
  # create new instances of self from the query results
  results.map { |options| self.new(options) }
end

.table_nameObject



74
75
76
# File 'lib/wormy.rb', line 74

def self.table_name
  @table_name ||= self.to_s.tableize
end

.table_name=(table_name) ⇒ Object



78
79
80
# File 'lib/wormy.rb', line 78

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

.validates(*methods) ⇒ Object



82
83
84
# File 'lib/wormy.rb', line 82

def self.validates(*methods)
  @validations = methods
end

.validationsObject



86
87
88
# File 'lib/wormy.rb', line 86

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

Instance Method Details

#attribute_valuesObject



94
95
96
97
# File 'lib/wormy.rb', line 94

def attribute_values
  # look at all the columns and get the values for this instance
  self.class.columns.map { |col| self.send(col) }
end

#attributesObject



90
91
92
# File 'lib/wormy.rb', line 90

def attributes
  @attributes ||= {}
end

#destroyObject



106
107
108
109
110
111
112
113
114
115
116
117
118
# File 'lib/wormy.rb', line 106

def destroy
  if self.class.find(id)
    DBConnection.execute(<<-SQL)
      DELETE
      FROM
        #{self.class.table_name}
      WHERE
        id = #{id}
    SQL

    self
  end
end

#insertObject



120
121
122
123
124
125
126
127
128
129
130
131
132
# File 'lib/wormy.rb', line 120

def insert
  col_names = self.class.columns.join(", ")
  question_marks = ["?"] * self.class.columns.count

  DBConnection.execute(<<-SQL, *attribute_values)
    INSERT INTO
      #{self.class.table_name} (#{col_names})
    VALUES
      (#{question_marks.join(',')})
  SQL

  self.id = DBConnection.last_insert_row_id
end

#saveObject



134
135
136
137
138
139
140
141
# File 'lib/wormy.rb', line 134

def save
  self.class.validations.each do |method|
    raise "Validation error" unless self.send(method)
  end

  # if it alredy exists then update it, otherwise insert it
  id ? update : insert
end

#updateObject



143
144
145
146
147
148
149
150
151
152
153
154
# File 'lib/wormy.rb', line 143

def update
  set = self.class.columns.map { |col_name| "#{col_name} = ?" }.drop(1).join(", ")

  DBConnection.execute(<<-SQL, *attribute_values.rotate)
    UPDATE
      #{self.class.table_name}
    SET
      #{set}
    WHERE
      id = ?
  SQL
end

#valid?Boolean

Returns:

  • (Boolean)


156
157
158
159
160
161
162
# File 'lib/wormy.rb', line 156

def valid?
  self.class.validations.each do |method|
    return false unless self.send(method)
  end

  true
end