Module: ActiveReaper

Defined in:
lib/active_reaper/railtie.rb,
lib/active_reaper/version.rb,
lib/active_reaper/active_reaper.rb

Defined Under Namespace

Modules: ClassMethods Classes: Railtie

Constant Summary collapse

VERSION =
"0.0.2"
REAPED_CLASSES =
{}

Class Method Summary collapse

Class Method Details

.included(base) ⇒ Object



7
8
9
# File 'lib/active_reaper/active_reaper.rb', line 7

def self.included(base)
  base.extend(ClassMethods)  
end

.reap!Object



11
12
13
14
15
16
17
18
19
20
21
22
23
# File 'lib/active_reaper/active_reaper.rb', line 11

def self.reap!
  REAPED_CLASSES.each_pair do |klass, settings|
    if settings[:determined_by_type] == :column && settings[:condition].nil?
      ActiveReaper.reap_simply(klass, settings)
    elsif settings[:determined_by_type] == :column && !settings[:condition].nil? && settings[:condition_type] == :column
      ActiveReaper.reap_quickly(klass, settings)
    elsif settings[:determined_by_type] == :column && !settings[:condition].nil? && settings[:condition_type] == :method
      ActiveReaper.reap_conditionally(klass, settings)
    elsif settings[:determined_by_type] == :method
      ActiveReaper.reap_meticulously(klass, settings)
    end
  end
end

.reap_conditionally(klass, settings) ⇒ Object

objects can be selected quickly, but have to be individually evaluated for the extra condition



87
88
89
90
91
92
93
94
95
96
97
# File 'lib/active_reaper/active_reaper.rb', line 87

def self.reap_conditionally(klass, settings)
  klass.where("#{settings[:determined_by]} < ?", settings[:after].ago.to_s(:db)).each do |object|
    if object.send(settings[:condition]) == settings[:truth_value]
      if settings[:using] == :delete
        klass.delete(object.id)
      else
        klass.destroy(object.id)
      end
    end
  end
end

.reap_meticulously(klass, settings) ⇒ Object

All objects must be iterated over to see which have expired, and then may or may not require an extra condition



100
101
102
103
104
105
106
107
108
109
110
# File 'lib/active_reaper/active_reaper.rb', line 100

def self.reap_meticulously(klass, settings)
  klass.all.each do |object|
    if (object.send(settings[:determined_by]) < settings[:after].ago) && (settings[:condition].nil? || object.send(settings[:condition]) == settings[:truth_value])
      if settings[:using] == :delete
        klass.delete(object.id)
      else
        klass.destroy(object.id)
      end
    end
  end
end

.reap_quickly(klass, settings) ⇒ Object

run a delete_all or destroy_all with the value of the column given as the condition made part of the query



78
79
80
81
82
83
84
# File 'lib/active_reaper/active_reaper.rb', line 78

def self.reap_quickly(klass, settings)
  if settings[:using] == :delete
    klass.where("#{settings[:determined_by]} < ?", settings[:after].ago.to_s(:db)).where(settings[:condition] => settings[:truth_value]).delete_all
  else
    klass.where("#{settings[:determined_by]} < ?", settings[:after].ago.to_s(:db)).where(settings[:condition] => settings[:truth_value]).destroy_all
  end
end

.reap_simply(klass, settings) ⇒ Object

run a simple delete_all or destroy_all; no conditions or method-based expiration



69
70
71
72
73
74
75
# File 'lib/active_reaper/active_reaper.rb', line 69

def self.reap_simply(klass, settings)
  if settings[:using] == :delete
    klass.where("#{settings[:determined_by]} < ?", settings[:after].ago.to_s(:db)).delete_all
  else
    klass.where("#{settings[:determined_by]} < ?", settings[:after].ago.to_s(:db)).destroy_all
  end
end