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.



71
72
73
74
75
76
77
78
# File 'lib/bezelrecord_base/bezelrecord_base.rb', line 71

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



41
42
43
44
45
46
47
48
49
# File 'lib/bezelrecord_base/bezelrecord_base.rb', line 41

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
# File 'lib/bezelrecord_base/bezelrecord_base.rb', line 11

def self.columns
  unless @columns
    @columns = DBConnection.columns(table_name)
  end
  @columns
end

.finalize!Object



18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/bezelrecord_base/bezelrecord_base.rb', line 18

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



57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/bezelrecord_base/bezelrecord_base.rb', line 57

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



51
52
53
54
55
# File 'lib/bezelrecord_base/bezelrecord_base.rb', line 51

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

.table_nameObject



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

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

.table_name=(table_name) ⇒ Object



33
34
35
# File 'lib/bezelrecord_base/bezelrecord_base.rb', line 33

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

Instance Method Details

#attribute_valuesObject



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

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

#attributesObject



80
81
82
# File 'lib/bezelrecord_base/bezelrecord_base.rb', line 80

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

#insertObject



89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
# File 'lib/bezelrecord_base/bezelrecord_base.rb', line 89

def insert
  columns = self.class.columns
  columns = columns.reject { |col| col == :id }

  attr_vals = attribute_values
  attr_vals = attr_vals[1..-1]

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

#saveObject



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

def save
  id.nil? ? insert : update
end

#updateObject



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

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

end