Class: Bezel::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.



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

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



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

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

.columnsObject



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

def self.columns
  unless @columns
    temp_columns = DBConnection.execute(<<-SQL)
      SELECT
        *
      FROM
        #{table_name}
    SQL
    @columns = temp_columns.first.map { |column| column.to_sym }
  end
  @columns
end

.finalize!Object



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

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



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

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

.parse_all(results) ⇒ Object



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

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

.table_nameObject



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

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

.table_name=(table_name) ⇒ Object



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

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

Instance Method Details

#attribute_valuesObject



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

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

#attributesObject



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

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

#insertObject



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

def insert
  columns = self.class.columns

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

#saveObject



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

def save
  id.nil? ? insert : update
end

#updateObject



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

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

end