Class: BiblioTech::Config::Schedules

Inherits:
Object
  • Object
show all
Defined in:
lib/bibliotech/config/schedule.rb

Direct Known Subclasses

Calendars, Periods

Constant Summary collapse

SCHEDULE_SHORTHANDS =
{
  "hourly"      => 60,
  "hourlies"    => 60,
  "daily"       => 60 * 24,
  "dailies"     => 60 * 24,
  "weekly"      => 60 * 24 * 7,
  "weeklies"    => 60 * 24 * 7,
  "monthly"     => 60 * 24 * 30,
  "monthlies"   => 60 * 24 * 30,
  "quarterly"   => 60 * 24 * 120,
  "quarterlies" => 60 * 24 * 120,
  "yearly"      => 60 * 24 * 365,
  "yearlies"    => 60 * 24 * 365,
}.freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(config) ⇒ Schedules

Returns a new instance of Schedules.



7
8
9
# File 'lib/bibliotech/config/schedule.rb', line 7

def initialize(config)
  @config = config
end

Instance Attribute Details

#configObject (readonly)

Returns the value of attribute config.



10
11
12
# File 'lib/bibliotech/config/schedule.rb', line 10

def config
  @config
end

Instance Method Details

#backup_frequencyObject



32
33
34
# File 'lib/bibliotech/config/schedule.rb', line 32

def backup_frequency
  @backup_frequency ||= regularize_frequency(config.local_get(:backup_frequency))
end

#get_config_hash(name) ⇒ Object



64
65
66
67
68
69
70
71
# File 'lib/bibliotech/config/schedule.rb', line 64

def get_config_hash(name)
  value = config.local_get(name)
  if value.to_s == "none"
    return {}
  else
    return value
  end
end

#regularize_frequency(frequency) ⇒ Object



26
27
28
29
30
# File 'lib/bibliotech/config/schedule.rb', line 26

def regularize_frequency(frequency)
  Integer( SCHEDULE_SHORTHANDS.fetch(frequency){ frequency } )
rescue ArgumentError
  raise "#{frequency.inspect} is neither a number of minutes or a shorthand. Try:\n  #{SCHEDULE_SHORTHANDS.keys.join(" ")}"
end

#regularize_schedule(freq) ⇒ Object



36
37
38
# File 'lib/bibliotech/config/schedule.rb', line 36

def regularize_schedule(freq)
  regularize_frequency(freq)
end

#schedulesObject



40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/bibliotech/config/schedule.rb', line 40

def schedules
  config_hash.map do |frequency, limit|
    next if limit == "none"
    real_frequency = regularize_schedule(frequency)
    limit =
      case limit
      when "all"
        nil
      else
        Integer(limit)
      end
    [frequency, real_frequency, limit]
  end.compact.map do |config_frequency, regularized, limit|
    build_scheduler(config_frequency, regularized, limit).tap do |sched|
      unless sched.frequency % backup_frequency == 0
        raise "Pruning frequency #{sched.frequency}:#{config_frequency} is not a multiple " +
        "of backup frequency: #{backup_frequency}:#{config.local_get(:backup_frequency)}"
      end
    end
  end.compact.sort_by do |schedule|
    schedule.frequency
  end
end