Sha256: a39607fdbfe0017f58eab05ab322b8be5e0804e3fd4dac8e12ee32d55b6ffa57

Contents?: true

Size: 1.83 KB

Versions: 29

Compression:

Stored size: 1.83 KB

Contents

module Timetrap
  class Entry < Sequel::Model
    plugin :schema

    class << self
    # a class level instance variable that controls whether or not all entries
    # should respond to #start and #end with times rounded to 15 minute
    # increments.
      attr_accessor :round
    end

    def round?
      !!self.class.round
    end

    def start= time
      self[:start]= Timer.process_time(time)
    end

    def end= time
      self[:end]= Timer.process_time(time)
    end

    def start
      round? ? rounded_start : self[:start]
    end

    def end
      round? ? rounded_end : self[:end]
    end

    def sheet
      self[:sheet].to_s
    end

    def duration
      @duration ||= self.end_or_now.to_i - self.start.to_i
    end
    def duration=( nd )
      @duration = nd.to_i
    end

    def end_or_now
      self.end || (round? ? round(Time.now) : Time.now)
    end

    def rounded_start
      round(self[:start])
    end

    def rounded_end
      round(self[:end])
    end

    def round time
      return nil unless time
      Time.at(
        if (r = time.to_i % Timetrap::Config['round_in_seconds']) < 450
          time.to_i - r
        else
          time.to_i + (Timetrap::Config['round_in_seconds'] - r)
        end
      )
    end

    def self.sheets
      map{|e|e.sheet}.uniq.sort
    end

    private
    # do a quick pseudo migration.  This should only get executed on the first run
    set_schema do
      primary_key :id
      column :note, String
      column :start, DateTime
      column :end, DateTime
      column :sheet, String
    end
    create_table unless table_exists?
  end

  class Meta < Sequel::Model(:meta)
    plugin :schema

    set_schema do
      primary_key :id
      column :key, String
      column :value, String
    end
    create_table unless table_exists?

    def value
      self[:value].to_s
    end
  end
end

Version data entries

29 entries across 29 versions & 1 rubygems

Version Path
timetrap-1.15.1 lib/timetrap/models.rb
timetrap-1.14.3 lib/timetrap/models.rb
timetrap-1.15.0 lib/timetrap/models.rb
timetrap-1.14.2 lib/timetrap/models.rb
timetrap-1.14.1 lib/timetrap/models.rb
timetrap-1.14.0 lib/timetrap/models.rb
timetrap-1.13.0 lib/timetrap/models.rb
timetrap-1.12.0 lib/timetrap/models.rb
timetrap-1.11.0 lib/timetrap/models.rb
timetrap-1.10.0 lib/timetrap/models.rb
timetrap-1.9.0 lib/timetrap/models.rb
timetrap-1.8.14 lib/timetrap/models.rb
timetrap-1.8.13 lib/timetrap/models.rb
timetrap-1.8.12 lib/timetrap/models.rb
timetrap-1.8.11 lib/timetrap/models.rb
timetrap-1.8.10 lib/timetrap/models.rb
timetrap-1.8.9 lib/timetrap/models.rb
timetrap-1.8.8 lib/timetrap/models.rb
timetrap-1.8.7 lib/timetrap/models.rb
timetrap-1.8.6 lib/timetrap/models.rb