Class: Hbtrack::Database::SequelStore
- Inherits:
-
Object
- Object
- Hbtrack::Database::SequelStore
- Defined in:
- lib/hbtrack/database/sequel_store.rb
Instance Attribute Summary collapse
-
#db ⇒ Object
readonly
Returns the value of attribute db.
Instance Method Summary collapse
-
#add_entry_of(habit_id, entry) ⇒ Object
Add a entry of a habit.
-
#add_habit(habit) ⇒ Object
Add a habit.
-
#day_range_for(time) ⇒ Object
Create a range from the start of a day to the end of a day according to the DateTime object given.
-
#delete_habit(title) ⇒ Object
Delete a habit.
-
#get_all_habits ⇒ Object
Get all habits.
-
#get_entries_count_of(habit_id) ⇒ Object
Get entries count of a habit.
-
#get_entries_of(habit_id) ⇒ Object
Get all entries of a habit.
-
#get_entries_of_month(habit_id, month, year) ⇒ Object
Get entries of a habit in a period of month according to month and year given.
-
#get_habit(id) ⇒ Object
Get habit by id.
-
#get_habit_by_title(title) ⇒ Object
Get habit by title.
-
#get_habit_id_for(title) ⇒ Object
Get ID of a habit by title.
-
#get_habits_count ⇒ Object
Get count of habits.
- #get_latest_entry_of(habit_id) ⇒ Object
-
#initialize(name: 'hbtrack.db') ⇒ SequelStore
constructor
A new instance of SequelStore.
-
#month_range(month, year) ⇒ Object
Create a range of date from the first day to the last day of a month.
- #update_entry_of(habit_id, time, type) ⇒ Object
Constructor Details
#initialize(name: 'hbtrack.db') ⇒ SequelStore
Returns a new instance of SequelStore.
9 10 11 12 |
# File 'lib/hbtrack/database/sequel_store.rb', line 9 def initialize(name: 'hbtrack.db') @db = Sequel.sqlite(name) create_table? end |
Instance Attribute Details
#db ⇒ Object (readonly)
Returns the value of attribute db.
8 9 10 |
# File 'lib/hbtrack/database/sequel_store.rb', line 8 def db @db end |
Instance Method Details
#add_entry_of(habit_id, entry) ⇒ Object
Add a entry of a habit
56 57 58 59 60 61 62 |
# File 'lib/hbtrack/database/sequel_store.rb', line 56 def add_entry_of(habit_id, entry) entries.insert( timestamp: entry., type: entry.type, habit_id: habit_id ) end |
#add_habit(habit) ⇒ Object
Add a habit
15 16 17 18 19 20 |
# 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) ⇒ Object
Create a range from the start of a day to the end of a day according to the DateTime object given.
104 105 106 107 108 109 110 111 112 113 114 |
# 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) ⇒ Object
Delete a habit
23 24 25 |
# File 'lib/hbtrack/database/sequel_store.rb', line 23 def delete_habit(title) habits.where(title: title).delete end |
#get_all_habits ⇒ Object
Get all habits
43 44 45 |
# File 'lib/hbtrack/database/sequel_store.rb', line 43 def get_all_habits habits.all end |
#get_entries_count_of(habit_id) ⇒ Object
Get entries count of a habit
81 82 83 |
# 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) ⇒ Object
Get all entries of a habit
76 77 78 |
# 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) ⇒ Object
Get entries of a habit in a period of month according to month and year given.
87 88 89 90 91 |
# 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) ⇒ Object
Get habit by id
28 29 30 |
# File 'lib/hbtrack/database/sequel_store.rb', line 28 def get_habit(id) habits.filter(id: id).first end |
#get_habit_by_title(title) ⇒ Object
Get habit by title
38 39 40 |
# 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) ⇒ Object
Get ID of a habit by title
33 34 35 |
# 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 ⇒ Object
Get count of habits
48 49 50 |
# File 'lib/hbtrack/database/sequel_store.rb', line 48 def get_habits_count habits.count end |
#get_latest_entry_of(habit_id) ⇒ Object
70 71 72 73 |
# 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) ⇒ Object
Create a range of date from the first day to the last day of a month
95 96 97 98 99 |
# 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) ⇒ Object
64 65 66 67 68 |
# 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 |