class TGauge::TRecordBase

Public Class Methods

all() click to toggle source
# File lib/app/models/trecord_base.rb, line 73
    def self.all
      objs_arr = TGauge::DBConnection.execute(<<-SQL)
        SELECT
          #{table_name}.*
        FROM
          #{table_name}
      SQL

      parse_all(objs_arr)
    end
columns() click to toggle source
# File lib/app/models/trecord_base.rb, line 35
    def self.columns
      # ...
      return @columns if @columns

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

      @columns
    end
destroy_all() click to toggle source
# File lib/app/models/trecord_base.rb, line 31
def self.destroy_all
  all.each(&:destroy!)
end
finalize!() click to toggle source
# File lib/app/models/trecord_base.rb, line 53
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) click to toggle source
# File lib/app/models/trecord_base.rb, line 89
    def self.find(id)
      obj = TGauge::DBConnection.execute(<<-SQL, id)
        SELECT
          #{table_name}.*
        FROM
          #{table_name}
        WHERE
          #{table_name}.id = ?
      SQL

      parse_all(obj).first
    end
my_attr_accessor(*names) click to toggle source
# 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) click to toggle source
# 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
new(params = {}) click to toggle source
# File lib/app/models/trecord_base.rb, line 102
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
parse_all(results) click to toggle source
# File lib/app/models/trecord_base.rb, line 84
def self.parse_all(results)
  # ...
  results.map { |obj_hash| self.new(obj_hash) }
end
table_name() click to toggle source
# File lib/app/models/trecord_base.rb, line 69
def self.table_name
  @table_name = @table_name || self.to_s.tableize
end
table_name=(table_name) click to toggle source
# File lib/app/models/trecord_base.rb, line 65
def self.table_name=(table_name)
  @table_name = table_name
end

Public Instance Methods

attribute_values() click to toggle source
# File lib/app/models/trecord_base.rb, line 115
def attribute_values
  attributes.values
end
attributes() click to toggle source
# File lib/app/models/trecord_base.rb, line 111
def attributes
  @attributes ||= {}
end
destroy!() click to toggle source
# File lib/app/models/trecord_base.rb, line 133
    def destroy!
      if self.class.find(id)
        TGauge::DBConnection.execute(<<-SQL)
          DELETE
          FROM
            #{self.class.table_name}
          WHERE
            id = #{id}
        SQL
        return self
      end
    end
insert() click to toggle source
# File lib/app/models/trecord_base.rb, line 119
    def insert
      cols = columns.reject { |col| col == "id" }
      attr_count = cols.count
      column_str = cols.join(", ")
      quest_str = Array.new(attr_count) {"?"}.join(", ")

      TGauge::DBConnection.execute(<<-SQL, attribute_values)
        INSERT INTO
          #{table_name} (#{column_str})
        VALUES
          (#{quest_str})
      SQL
    end
save() click to toggle source
# File lib/app/models/trecord_base.rb, line 160
def save
  if attributes[:id]
    update
  else
    insert
  end
end
update() click to toggle source
# File lib/app/models/trecord_base.rb, line 146
    def update
      attr_count = columns.count - 1
      column_str = columns[1..-1].map { |col| "#{col} = ?" }.join(", ")

      TGauge::DBConnection.execute(<<-SQL, attribute_values)
        UPDATE
          #{table_name}
        SET
          #{column_str}
        WHERE
          id = ?
      SQL
    end

Private Instance Methods

columns() click to toggle source
# File lib/app/models/trecord_base.rb, line 170
def columns
  self.class.columns
end
table_name() click to toggle source
# File lib/app/models/trecord_base.rb, line 174
def table_name
  self.class.table_name
end