Class: Tcelfer::Storage

Inherits:
Object
  • Object
show all
Defined in:
lib/tcelfer/storage.rb

Overview

Middleware between Sequel and everything else

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeStorage

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

#daysObject (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

Parameters:

  • month (Integer)
  • year (Integer)

Returns:

  • (Array)

Raises:



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

Parameters:

  • rating (String)
  • notes (String)
  • date (Date)


39
40
41
42
43
44
45
46
47
# File 'lib/tcelfer/storage.rb', line 39

def rec_day(rating, notes, date)
  if Tcelfer.config.update_existing
    days.update_or_create({ date: date }, rating: rating, notes: notes)
  elsif days.where(date: date).count.zero?
    days.new(date: date, rating: rating, notes: notes).save
  else
    raise Tcelfer::DuplicateDayError, "Date: `#{date}' already has a record. Try again tomorrow."
  end
end