Module: Acts::Expireable::ClassMethods

Defined in:
lib/acts/expireable.rb

Instance Method Summary collapse

Instance Method Details

#acts_as_expireable(options = {}) ⇒ Object



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/acts/expireable.rb', line 9

def acts_as_expireable(options = {})
    config = {:respawn => false, :lifespan => 10.seconds, :delay=> 0.seconds}
    config.update options if options.is_a?(Hash)

    unless expireable? # don't let AR call this twice
      cattr_accessor :respawn, :lifespan, :delay
      self.respawn = config[:respawn]
      self.lifespan = config[:lifespan]
      self.delay = config[:delay]
      Thread.new do
        while true

            #puts "Checking Expireables."
            #self.find(:all).each do |s|
            # puts "Expires at:" + s.expires_at.to_s
            # s.destroy
            #end

            #puts Time.now.to_s

            self.find(:all, :conditions => ["spawn_at <= ? and expires_at >= ?", Time.now, Time.now]).each do |expireable|
              expireable.spawn
            end
            self.find(:all, :conditions => ["expires_at <= ?", Time.now]).each do |expireable|
              expireable.expire
            end
            sleep 0.5
        end
      end
    end
    before_create :setup_expireable
    include InstanceMethods
rescue
  puts $!
end

#expireable?Boolean

Returns:

  • (Boolean)


45
46
47
# File 'lib/acts/expireable.rb', line 45

def expireable?
  self.included_modules.include?(InstanceMethods)
end