Sha256: a3cdd2547ec1bbbe9c6dff8cab9d9cab3eddfefb52ef4a65144cb1e7ceb9c48d

Contents?: true

Size: 1.13 KB

Versions: 9

Compression:

Stored size: 1.13 KB

Contents

#encoding: utf-8

require 'fileutils'
require 'active_record'

module Recorder

  module DB

    def self.prepare
      database_path = File.join(ENV['HOME'], '.recorder','recorder.sqlite3')

      connect_database database_path
      create_table_if_not_exists database_path
    end

    def self.connect_database(path)
      spec = {adapter: 'sqlite3', database: path}
      ActiveRecord::Base.establish_connection spec
    end

    def self.create_table_if_not_exists(path)
      create_database_path path

      connection = ActiveRecord::Base.connection

      return if connection.table_exists?(:data)

      connection.create_table :data do |d|
        d.column :weight,  :real, null: false
        d.column :bodyfat, :real, null: true
        d.column :date, :text, null: true
        d.column :memo, :text, null: true
        d.timestamps
      end
      connection.add_index :data, :created_at
      connection.add_index :data, :date

    end

    def self.create_database_path(path)
      FileUtils.mkdir_p File.dirname(path)
    end

    private_class_method :connect_database, :create_table_if_not_exists, :create_database_path

  end
end

Version data entries

9 entries across 6 versions & 1 rubygems

Version Path
weight-recorder-0.1.10 lib/recorder/#db.rb#
weight-recorder-0.1.10 lib/recorder/db.rb
weight-recorder-0.1.9 lib/recorder/db.rb
weight-recorder-0.1.9 lib/recorder/#db.rb#
weight-recorder-0.1.8 lib/recorder/#db.rb#
weight-recorder-0.1.8 lib/recorder/db.rb
weight-recorder-0.1.7 lib/recorder/db.rb
weight-recorder-0.1.6 lib/recorder/db.rb
weight-recorder-0.1.5 lib/recorder/db.rb