Class: TGauge::TRecordBase

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

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Associatable

assoc_options, belongs_to, has_many, has_one, through

Methods included from Searchable

where

Constructor Details

#initialize(params = {}) ⇒ TRecordBase

Returns a new instance of TRecordBase.



99
100
101
102
103
104
105
106
# File 'lib/app/models/trecord_base.rb', line 99

def initialize(params = {})
  # ...
  params.each do |att_name, val|
    att_name = att_name.to_sym
    raise "unknown attribute '#{att_name}'" unless columns.include?(att_name.to_s)
    self.send(att_name.to_s + "=", val)
  end
end

Class Method Details

.allObject



70
71
72
73
74
75
76
77
78
79
# File 'lib/app/models/trecord_base.rb', line 70

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

.columnsObject



31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/app/models/trecord_base.rb', line 31

def self.columns
  # ...
  return @columns if @columns

  arr = DBConnection.execute("  SELECT\n  *\n  FROM\n  \#{self.table_name}\n  SQL\n\n  @columns = []\n  arr.nfields.times do |i|\n    @columns << arr.fname(i)\n  end\n\n  @columns\nend\n")

.finalize!Object



50
51
52
53
54
55
56
57
58
59
60
# File 'lib/app/models/trecord_base.rb', line 50

def self.finalize!
  columns.each do |column|
    # inst_var = "@" + column.to_s
    define_method(column) do
      attributes[column]
    end
    define_method(column.to_s + "=") do |arg|
      attributes[column] = arg
    end
  end
end

.find(id) ⇒ Object



86
87
88
89
90
91
92
93
94
95
96
97
# File 'lib/app/models/trecord_base.rb', line 86

def self.find(id)
  obj = DBConnection.execute("  SELECT\n  \#{table_name}.*\n  FROM\n  \#{table_name}\n  WHERE\n  \#{table_name}.id = ?\n  SQL\n\n  parse_all(obj).first\nend\n", id)

.my_attr_accessor(*names) ⇒ Object



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

def self.my_attr_accessor(*names)
  names.each do |name|
    define_method(name) do
      instance_variable_get("@" + name.to_s)
    end

    define_method(name.to_s + "=") do |arg|
      instance_variable_set("@" + name.to_s, arg)
    end
  end
end

.my_attr_reader(*names) ⇒ Object



23
24
25
26
27
28
29
# File 'lib/app/models/trecord_base.rb', line 23

def self.my_attr_reader(*names)
  names.each do |name|
    define_method(name) do
      instance_variable_get("@" + name.to_s)
    end
  end
end

.parse_all(results) ⇒ Object



81
82
83
84
# File 'lib/app/models/trecord_base.rb', line 81

def self.parse_all(results)
  # ...
  results.map { |obj_hash| self.new(obj_hash) }
end

.table_nameObject



66
67
68
# File 'lib/app/models/trecord_base.rb', line 66

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

.table_name=(table_name) ⇒ Object



62
63
64
# File 'lib/app/models/trecord_base.rb', line 62

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

Instance Method Details

#attribute_valuesObject



112
113
114
# File 'lib/app/models/trecord_base.rb', line 112

def attribute_values
  attributes.values
end

#attributesObject



108
109
110
# File 'lib/app/models/trecord_base.rb', line 108

def attributes
  @attributes ||= {}
end

#insertObject



116
117
118
119
120
121
122
123
124
125
126
127
128
# File 'lib/app/models/trecord_base.rb', line 116

def insert
  cols = columns.reject { |col| col == "id" }
  attr_count = cols.count
  column_str = cols.join(", ")
  quest_str = Array.new(attr_count) {"?"}.join(", ")
  debugger
  DBConnection.execute("  INSERT INTO\n  \#{table_name} (\#{column_str})\n  VALUES\n  (\#{quest_str})\n  SQL\nend\n", attribute_values)

#saveObject



144
145
146
147
148
149
150
# File 'lib/app/models/trecord_base.rb', line 144

def save
  if attributes[:id]
    update
  else
    insert
  end
end

#updateObject



130
131
132
133
134
135
136
137
138
139
140
141
142
# File 'lib/app/models/trecord_base.rb', line 130

def update
  # attr_count = columns.count - 1
  column_str = columns[1..-1].map { |col| "#{col} = ?" }.join(", ")

  DBConnection.execute("  UPDATE\n  \#{table_name}\n  SET\n  \#{column_str}\n  WHERE\n  id = ?\n  SQL\nend\n", attribute_values)