Sha256: 8c4d82057e375f8503b81a09501c517d73966ba68917b93ede0b801fc4b4e0cf

Contents?: true

Size: 1.09 KB

Versions: 4

Compression:

Stored size: 1.09 KB

Contents

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

      # @return [Numric]
      def self.count
        db.execute("select count(*) from #{table_name}").first.first
      end

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

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

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

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

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

      def table_name
        self.class.table_name
      end

      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
    end
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
procon_bypass_man-web-0.1.3 lib/procon_bypass_man/web/models/base_model.rb
procon_bypass_man-web-0.1.2 lib/procon_bypass_man/web/models/base_model.rb
procon_bypass_man-web-0.1.1 lib/procon_bypass_man/web/models/base_model.rb
procon_bypass_man-web-0.1.0 lib/procon_bypass_man/web/models/base_model.rb