Class: Tcelfer::Storage
- Inherits:
-
Object
- Object
- Tcelfer::Storage
- Defined in:
- lib/tcelfer/storage.rb
Overview
Middleware between Sequel and everything else
Instance Attribute Summary collapse
-
#days ⇒ Object
readonly
Returns the value of attribute days.
Instance Method Summary collapse
-
#by_month(month, year) ⇒ Array
Returns an array of Tcelfer::Model::Day objects for the given month/year.
-
#initialize ⇒ Storage
constructor
A new instance of Storage.
-
#rec_day(rating, notes, date) ⇒ Object
Record a new day or update an existing one.
Constructor Details
#initialize ⇒ Storage
Returns a new instance of Storage.
26 27 28 29 30 31 32 33 |
# File 'lib/tcelfer/storage.rb', line 26 def initialize db = Sequel.connect("sqlite://#{Tcelfer.config.sqlite_path}") db.loggers << Logger.new($stderr) if Tcelfer.config.debug validate_db! db Dir["#{__dir__}/models/*"].each(&Kernel.method(:require)) @days = Tcelfer::Models::Day @days.plugin :update_or_create end |
Instance Attribute Details
#days ⇒ Object (readonly)
Returns the value of attribute days.
24 25 26 |
# File 'lib/tcelfer/storage.rb', line 24 def days @days end |
Instance Method Details
#by_month(month, year) ⇒ Array
Returns an array of Tcelfer::Model::Day objects for the given month/year
53 54 55 56 57 |
# File 'lib/tcelfer/storage.rb', line 53 def by_month(month, year) raise StorageError, "Invalid Month #{month}, valid: [1-12]" unless (1..12).cover? month @days.where(date: Date.new(year, month, 1)..Date.new(year, month, -1)).to_a end |
#rec_day(rating, notes, date) ⇒ Object
Record a new day or update an existing one
39 40 41 42 43 44 45 46 47 |
# File 'lib/tcelfer/storage.rb', line 39 def rec_day(, notes, date) if Tcelfer.config.update_existing days.update_or_create({ date: date }, rating: , notes: notes) elsif days.where(date: date).count.zero? days.new(date: date, rating: , notes: notes).save else raise Tcelfer::DuplicateDayError, "Date: `#{date}' already has a record. Try again tomorrow." end end |