Class: Expire::KeepAdjectiveRuleBase

Inherits:
RuleBase
  • Object
show all
Defined in:
lib/expire/keep_adjective_rule_base.rb

Overview

Base class for rules with an adjective in their name

Constant Summary collapse

NOUN_FOR =
{
  "hourly" => "hour",
  "daily" => "day",
  "weekly" => "week",
  "monthly" => "month",
  "yearly" => "year"
}.freeze
PRIMARY_RANK =
20
SECONDARY_RANK_FOR =
{
  "hourly" => 1,
  "daily" => 2,
  "weekly" => 3,
  "monthly" => 4,
  "yearly" => 5
}.freeze

Instance Attribute Summary

Attributes inherited from RuleBase

#amount

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from RuleBase

<=>, #<=>, camelized_name, #initialize, name, #name, #numerus_backup, #option_name, option_name

Constructor Details

This class inherits a constructor from Expire::RuleBase

Class Method Details

.from_value(value) ⇒ Object

Raises:

  • (ArgumentError)


25
26
27
28
29
30
31
32
33
# File 'lib/expire/keep_adjective_rule_base.rb', line 25

def self.from_value(value)
  value = -1 if value.all?
  value = 0 if value.none?

  integer_value = Integer(value)
  raise ArgumentError, "must be at least -1" if integer_value < -1

  new(amount: integer_value)
end

.primary_rankObject



35
36
37
# File 'lib/expire/keep_adjective_rule_base.rb', line 35

def self.primary_rank
  PRIMARY_RANK
end

.rankObject



39
40
41
# File 'lib/expire/keep_adjective_rule_base.rb', line 39

def self.rank
  primary_rank + secondary_rank
end

.secondary_rankObject



43
44
45
46
47
48
# File 'lib/expire/keep_adjective_rule_base.rb', line 43

def self.secondary_rank
  match = name.downcase.match(/(hourly|daily|weekly|monthly|yearly)/)
  return unless match

  SECONDARY_RANK_FOR[match[1]]
end

Instance Method Details

#adjectiveObject



50
51
52
# File 'lib/expire/keep_adjective_rule_base.rb', line 50

def adjective
  @adjective ||= infer_adjective
end

#apply(backups, _) ⇒ Object



54
55
56
57
58
# File 'lib/expire/keep_adjective_rule_base.rb', line 54

def apply(backups, _)
  per_spacing = backups.one_per(spacing)
  kept = (amount == -1) ? per_spacing : per_spacing.most_recent(amount)
  kept.each { |backup| backup.add_reason_to_keep(reason_to_keep) }
end

#primary_rankObject



64
65
66
# File 'lib/expire/keep_adjective_rule_base.rb', line 64

def primary_rank
  self.class.primary_rank
end

#rankObject



60
61
62
# File 'lib/expire/keep_adjective_rule_base.rb', line 60

def rank
  @rank ||= primary_rank + secondary_rank
end

#secondary_rankObject



68
69
70
# File 'lib/expire/keep_adjective_rule_base.rb', line 68

def secondary_rank
  self.class.secondary_rank
end

#spacingObject



72
73
74
# File 'lib/expire/keep_adjective_rule_base.rb', line 72

def spacing
  NOUN_FOR[adjective]
end