Class: ThinOutBackups::Command::Bucket

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

Overview


Constant Summary collapse

@@quota_format =
%r[(\d+|\*)(/\d+)?]

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(parent, name, quota) ⇒ Bucket

Returns a new instance of Bucket.



60
61
62
63
64
65
66
67
68
# File 'lib/thin_out_backups.rb', line 60

def initialize(parent, name, quota)
  @parent = parent
  @name = name
  (
  raise "Invalid quota '#{quota}'" unless quota.is_a?(Fixnum) || quota =~ @@quota_format
  @quota = quota
  )
  @keepers = []
end

Instance Attribute Details

#keepersObject (readonly)

Returns the value of attribute keepers.



57
58
59
# File 'lib/thin_out_backups.rb', line 57

def keepers
  @keepers
end

#nameObject (readonly)

Returns the value of attribute name.



57
58
59
# File 'lib/thin_out_backups.rb', line 57

def name
  @name
end

#parentObject (readonly)

Returns the value of attribute parent.



57
58
59
# File 'lib/thin_out_backups.rb', line 57

def parent
  @parent
end

#quotaObject (readonly)

Returns the value of attribute quota.



57
58
59
# File 'lib/thin_out_backups.rb', line 57

def quota
  @quota
end

Instance Method Details

#keep(keeper) ⇒ Object



108
109
110
# File 'lib/thin_out_backups.rb', line 108

def keep(keeper)
  @keepers << keeper
end

#keep_all?Boolean

Returns:

  • (Boolean)


120
# File 'lib/thin_out_backups.rb', line 120

def keep_all?; quota =~ /\*/ end

#satisfied?Boolean

Returns:

  • (Boolean)


122
123
124
125
126
127
128
# File 'lib/thin_out_backups.rb', line 122

def satisfied?
  if keep_all?
    false # it has insatiable hunger for keepers that can never be satisfied
  else
    still_need == 0
  end
end

#start_timeObject



81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
# File 'lib/thin_out_backups.rb', line 81

def start_time
  start_time = parent.now.dup
  if parent.align_at_beginning_of_time_interval
    beginning_of_interval =
      case unit
      when :minutes
        start_time.change(:sec => 0)
      when :hours
        start_time.change(:min => 0)
      when :days
        start_time.change(:hour => 0)
      when :weeks
        start_time.beginning_of_week(:sunday)
      when :months
        start_time.change(             :day => 1, :hour => 0)
      when :years
        start_time.change(:month => 1, :day => 1, :hour => 0)
      else
        raise "unexpected unit #{unit}"
      end
    # We actually want to use the *next* interval (in the future) as our start_time because we will be using this as our max and going backwards in time...
    beginning_of_interval.shift(1, unit)
  else
    start_time
  end
end

#still_needObject



112
113
114
115
116
117
118
# File 'lib/thin_out_backups.rb', line 112

def still_need
  if keep_all?
    1 # it has insatiable hunger for keepers that can never be satisfied ... always just 1 more ...
  else
    @quota - @keepers.size
  end
end

#unitObject



70
71
72
73
74
75
76
77
78
79
# File 'lib/thin_out_backups.rb', line 70

def unit
  {
    :minutely => :minutes,
    :hourly   => :hours,
    :daily    => :days,
    :weekly   => :weeks,
    :monthly  => :months,
    :yearly   => :years,
  }[@name.to_sym]
end