Class: ProconBypassMan::Web::BaseModel

Inherits:
Object
  • Object
show all
Defined in:
lib/procon_bypass_man/web/models/base_model.rb

Direct Known Subclasses

Setting

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(row) ⇒ BaseModel

Returns a new instance of BaseModel.



4
5
6
7
8
# File 'lib/procon_bypass_man/web/models/base_model.rb', line 4

def initialize(row)
  @@column_names.each.with_index(0) do |name, index|
    self.public_send("#{name}=", row[index])
  end
end

Class Method Details

.column_namesObject



23
24
25
26
# File 'lib/procon_bypass_man/web/models/base_model.rb', line 23

def self.column_names
  raise "need to define column_names" if not defined?(@@column_names)
 @@column_names
end

.column_names=(c) ⇒ Object



19
20
21
# File 'lib/procon_bypass_man/web/models/base_model.rb', line 19

def self.column_names=(c)
  @@column_names = c
end

.countNumric

Returns:

  • (Numric)


11
12
13
# File 'lib/procon_bypass_man/web/models/base_model.rb', line 11

def self.count
  db.execute("select count(*) from #{table_name}").first.first
end

.dbObject



15
16
17
# File 'lib/procon_bypass_man/web/models/base_model.rb', line 15

def self.db
  ProconBypassMan::Web::Db.db
end

.table_nameObject



28
29
30
31
# File 'lib/procon_bypass_man/web/models/base_model.rb', line 28

def self.table_name
  raise "need to define column_names" if not defined?(@@table_name)
  @@table_name
end

.table_name=(value) ⇒ Object



33
34
35
# File 'lib/procon_bypass_man/web/models/base_model.rb', line 33

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

Instance Method Details

#table_nameObject



37
38
39
# File 'lib/procon_bypass_man/web/models/base_model.rb', line 37

def table_name
  self.class.table_name
end

#update!(attributes) ⇒ Object



41
42
43
44
# File 'lib/procon_bypass_man/web/models/base_model.rb', line 41

def update!(attributes)
  c = attributes.map {|key, _value| "'#{key}' = ?"  }.join(", ")
  self.class.db.execute("update #{table_name} set #{c}", attributes.map {|_key, value| value })
end