class Hbtrack::Database::SequelStore
Attributes
db[R]
Public Class Methods
new(name: 'hbtrack.db')
click to toggle source
# File lib/hbtrack/database/sequel_store.rb, line 9 def initialize(name: 'hbtrack.db') @db = Sequel.sqlite(name) create_table? end
Public Instance Methods
add_entry_of(habit_id, entry)
click to toggle source
Add a entry of a habit
# File lib/hbtrack/database/sequel_store.rb, line 56 def add_entry_of(habit_id, entry) entries.insert( timestamp: entry.timestamp, type: entry.type, habit_id: habit_id ) end
add_habit(habit)
click to toggle source
Add a habit
# File lib/hbtrack/database/sequel_store.rb, line 15 def add_habit(habit) habits.insert( title: habit.title, display_order: habit.display_order ) end
day_range_for(time)
click to toggle source
Create a range from the start of a day to the end of a day according to the DateTime object given.
# File lib/hbtrack/database/sequel_store.rb, line 104 def day_range_for(time) year = time.year month = time.month day = time.day timezone = Time.new.zone Range.new( DateTime.new(year, month, day, 0, 0, 0, timezone), DateTime.new(year, month, day, 23, 59, 59, timezone) ) end
delete_habit(title)
click to toggle source
Delete a habit
# File lib/hbtrack/database/sequel_store.rb, line 23 def delete_habit(title) habits.where(title: title).delete end
get_all_habits()
click to toggle source
Get all habits
# File lib/hbtrack/database/sequel_store.rb, line 43 def get_all_habits habits.all end
get_entries_count_of(habit_id)
click to toggle source
Get entries count of a habit
# File lib/hbtrack/database/sequel_store.rb, line 81 def get_entries_count_of(habit_id) get_entries_of(habit_id).count end
get_entries_of(habit_id)
click to toggle source
Get all entries of a habit
# File lib/hbtrack/database/sequel_store.rb, line 76 def get_entries_of(habit_id) entries.where(habit_id: habit_id) end
get_entries_of_month(habit_id, month, year)
click to toggle source
Get entries of a habit in a period of month according to month and year given.
# File lib/hbtrack/database/sequel_store.rb, line 87 def get_entries_of_month(habit_id, month, year) get_entries_of(habit_id) .where(timestamp: month_range(month, year)) .all end
get_habit(id)
click to toggle source
Get habit by id
# File lib/hbtrack/database/sequel_store.rb, line 28 def get_habit(id) habits.filter(id: id).first end
get_habit_by_title(title)
click to toggle source
Get habit by title
# File lib/hbtrack/database/sequel_store.rb, line 38 def get_habit_by_title(title) habits.filter(title: title).first end
get_habit_id_for(title)
click to toggle source
Get ID of a habit by title
# File lib/hbtrack/database/sequel_store.rb, line 33 def get_habit_id_for(title) get_habit_by_title(title)&.fetch(:id) end
get_habits_count()
click to toggle source
Get count of habits
# File lib/hbtrack/database/sequel_store.rb, line 48 def get_habits_count habits.count end
get_latest_entry_of(habit_id)
click to toggle source
# File lib/hbtrack/database/sequel_store.rb, line 70 def get_latest_entry_of(habit_id) entries.where(habit_id: habit_id) .order(Sequel.desc(:timestamp)).first end
month_range(month, year)
click to toggle source
Create a range of date from the first day to the last day of a month
# File lib/hbtrack/database/sequel_store.rb, line 95 def month_range(month, year) next_month = month == 12 ? 1 : month + 1 next_year = month == 12 ? year + 1 : year Date.new(year, month, 1)..Date.new(next_year, next_month, 1) end
update_entry_of(habit_id, time, type)
click to toggle source
# File lib/hbtrack/database/sequel_store.rb, line 64 def update_entry_of(habit_id, time, type) entries.where(habit_id: habit_id) .where(timestamp: day_range_for(time)) .update(type: type, timestamp: time) end
Private Instance Methods
create_table?()
click to toggle source
Create Habits and Entries table if doesn't exist
# File lib/hbtrack/database/sequel_store.rb, line 118 def create_table? db.create_table? :habits do primary_key :id String :title Integer :display_order end db.create_table? :entries do primary_key :id String :type DateTime :timestamp foreign_key :habit_id, :habits, on_delete: :cascade end end
entries()
click to toggle source
Get Entries dataset
# File lib/hbtrack/database/sequel_store.rb, line 140 def entries return @entries if @entries @entries = db[:entries] end
habits()
click to toggle source
Get Habits dataset
# File lib/hbtrack/database/sequel_store.rb, line 134 def habits return @habits if @habit @habits = db[:habits] end