Class: Statlysis::Clock

Inherits:
Object
  • Object
show all
Includes:
Common
Defined in:
lib/statlysis/clock.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Common

#cron

Constructor Details

#initialize(feature, default_time = nil) ⇒ Clock

feature is a string



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/statlysis/clock.rb', line 9

def initialize feature, default_time = nil
  # init table & model
  cron.stat_table_name = [Statlysis.tablename_default_pre, 'clocks'].compact.join("_")
  unless Statlysis.sequel.table_exists?(cron.stat_table_name)
    Statlysis.sequel.create_table cron.stat_table_name, DefaultTableOpts.merge(:engine => "InnoDB") do
      primary_key :id
      String      :feature
      DateTime    :t
      index       :feature, :unique => true
    end
  end
  h = Utils.setup_pattern_table_and_model cron.stat_table_name
  cron.stat_model = h[:model]

  # init default_time
  default_time ||= DateTime.now
  cron.clock = cron.stat_model.find_or_create(:feature => feature)
  cron.clock.update :t => default_time if cron.current.nil?
  cron
end

Instance Attribute Details

#clockObject

Returns the value of attribute clock.



5
6
7
# File 'lib/statlysis/clock.rb', line 5

def clock
  @clock
end

Instance Method Details

#currentObject



36
# File 'lib/statlysis/clock.rb', line 36

def current; cron.clock.t end

#update(time = DateTime.now) ⇒ Object



30
31
32
33
34
# File 'lib/statlysis/clock.rb', line 30

def update time = DateTime.now
  time = DateTime.now if time == DateTime1970
  return false if time && (time < cron.current)
  cron.clock.update :t => time
end