Class: ModelBase

Inherits:
Object
  • Object
show all
Extended by:
Associatable, Searchable
Defined in:
lib/reloj/orm/model_base.rb,
lib/reloj/orm/searchable.rb,
lib/reloj/orm/associatable.rb

Direct Known Subclasses

Cat

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Searchable

where

Methods included from Associatable

assoc_options, belongs_to, has_many, has_one_through

Constructor Details

#initialize(params = {}) ⇒ ModelBase

Returns a new instance of ModelBase.



77
78
79
80
81
82
83
84
# File 'lib/reloj/orm/model_base.rb', line 77

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

Class Method Details

.allObject



44
45
46
47
48
49
50
51
52
53
# File 'lib/reloj/orm/model_base.rb', line 44

def self.all
  results_array = Database.execute("  SELECT\n    *\n  FROM\n    \#{table_name};\n  SQL\n\n  self.parse_all(results_array)\nend\n")

.columnsObject

rough idea on how to replace self.finalize!

need to run at end of class definiton, not beginning
def self.inherited(subclass)
  attr_writer(*subclass.columns)
end


12
13
14
15
16
17
18
19
20
21
22
23
# File 'lib/reloj/orm/model_base.rb', line 12

def self.columns
  result = Database.execute("    SELECT\n      *\n    FROM\n      \#{self.table_name}\n    LIMIT\n      0;\n  SQL\n\n  result.fields.map(&:to_sym)\nend\n")

.finalize!Object



25
26
27
28
29
30
31
32
33
34
# File 'lib/reloj/orm/model_base.rb', line 25

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

.find(id) ⇒ Object



64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/reloj/orm/model_base.rb', line 64

def self.find(id)
  x = Database.execute("  SELECT\n    *\n  FROM\n    \#{table_name}\n  WHERE\n    id = ?;\n  SQL\n\n  x.empty? ? nil : self.new(x.first)\nend\n", id)

.parse_all(results) ⇒ Object



55
56
57
58
59
60
61
62
# File 'lib/reloj/orm/model_base.rb', line 55

def self.parse_all(results)
  # TODO: rewrite with map
  answer = []
  results.each do |result|
    answer << self.new(result)
  end
  answer
end

.table_nameObject



40
41
42
# File 'lib/reloj/orm/model_base.rb', line 40

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

.table_name=(table_name) ⇒ Object



36
37
38
# File 'lib/reloj/orm/model_base.rb', line 36

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

Instance Method Details

#attribute_valuesObject



90
91
92
93
94
# File 'lib/reloj/orm/model_base.rb', line 90

def attribute_values
  self.class.columns.map do |column|
    self.send(column)
  end
end

#attributesObject



86
87
88
# File 'lib/reloj/orm/model_base.rb', line 86

def attributes
  @attributes ||= {}
end

#insertObject



96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
# File 'lib/reloj/orm/model_base.rb', line 96

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

  response = Database.execute("    INSERT INTO\n      \#{self.class.table_name} (\#{col_names})\n    VALUES\n      (\#{question_marks})\n    RETURNING\n      id;\n  SQL\n  self.id = response[0]['id'].to_i\nend\n", self.attribute_values)

#saveObject



128
129
130
# File 'lib/reloj/orm/model_base.rb', line 128

def save
  id.nil? ? insert : update
end

#updateObject



113
114
115
116
117
118
119
120
121
122
123
124
125
126
# File 'lib/reloj/orm/model_base.rb', line 113

def update
  columns_line = self.class.columns.map do |col_name|
    "#{col_name} = ?"
  end.join(", ")

  Database.execute("    UPDATE\n      \#{self.class.table_name}\n    SET\n      \#{columns_line}\n    WHERE\n      id = ?;\n  SQL\nend\n", self.attribute_values, self.id)