Class: BezelrecordBase

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

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Associatable

assoc_options, belongs_to, has_many, has_many_through, has_one_through

Methods included from Searchable

where

Constructor Details

#initialize(params = {}) ⇒ BezelrecordBase

Returns a new instance of BezelrecordBase.



76
77
78
79
80
81
82
83
# File 'lib/bezelrecord_base/bezelrecord_base.rb', line 76

def initialize(params = {})
  table_columns = self.class.columns

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

Class Method Details

.allObject



46
47
48
49
50
51
52
53
54
# File 'lib/bezelrecord_base/bezelrecord_base.rb', line 46

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

.columnsObject



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

def self.columns
  unless @columns
    temp_columns = DBConnection.execute2("      SELECT\n        *\n      FROM\n        \#{table_name}\n    SQL\n    @columns = temp_columns.first.map { |column| column.to_sym }\n  end\n  @columns\nend\n")

.finalize!Object



23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/bezelrecord_base/bezelrecord_base.rb', line 23

def self.finalize!
  columns.each do |column|

    define_method("#{column}") do
      @attributes = self.attributes
      @attributes[column]
    end

    define_method("#{column}=") do |value|
      @attributes = self.attributes
      @attributes[column] = value
    end
  end
end

.find(id) ⇒ Object



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

def self.find(id)
  object = DBConnection.execute("    SELECT\n      *\n    FROM\n      \#{table_name}\n    WHERE\n      id = ?\n    LIMIT\n      1\n  SQL\n  nil || object.map {|obj| self.new(obj)}.first\nend\n", id)

.parse_all(results) ⇒ Object



56
57
58
59
60
# File 'lib/bezelrecord_base/bezelrecord_base.rb', line 56

def self.parse_all(results)
  objects = results.map do |object|
    self.new(object)
  end
end

.table_nameObject



42
43
44
# File 'lib/bezelrecord_base/bezelrecord_base.rb', line 42

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

.table_name=(table_name) ⇒ Object



38
39
40
# File 'lib/bezelrecord_base/bezelrecord_base.rb', line 38

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

Instance Method Details

#attribute_valuesObject



89
90
91
92
93
# File 'lib/bezelrecord_base/bezelrecord_base.rb', line 89

def attribute_values
  table_cols = self.class.columns
  table_cols
  table_cols.map{ |col| self.send("#{col}")}
end

#attributesObject



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

def attributes
@attributes = @attributes || {}
end

#insertObject



95
96
97
98
99
100
101
102
103
104
105
# File 'lib/bezelrecord_base/bezelrecord_base.rb', line 95

def insert
  columns = self.class.columns

  DBConnection.execute("    INSERT INTO\n      \#{self.class.table_name} (\#{columns.join(\", \")})\n    VALUES\n      (\#{Array.new(columns.length,\"?\").join(\", \")})\n    SQL\n    self.send(:id=,DBConnection.last_insert_row_id)\nend\n", *attribute_values)

#saveObject



119
120
121
# File 'lib/bezelrecord_base/bezelrecord_base.rb', line 119

def save
  id.nil? ? insert : update
end

#updateObject



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

def update
  DBConnection.execute("    UPDATE\n      \#{self.class.table_name}\n    SET\n      \#{self.class.columns[1..-1].map{|col| \"\#{col} = ?\"}.join(\", \")}\n    WHERE\n      id = ?\n    SQL\n\nend\n",*attribute_values[1..-1], attribute_values.first)