Sha256: 969dafcb164c20bfa2dd8cea6fb57868df3563723a6d085d8db04aeca8676506

Contents?: true

Size: 847 Bytes

Versions: 3

Compression:

Stored size: 847 Bytes

Contents

module ShortLived
  
  class LifetimeMissingError < RuntimeError
    def initialize
      super <<-MESSAGE
        A ShortLived thing must define method
          lifetime lifetime = nil, &block
        with either params
          lifetime 74 # some value
        or
          lifetime { 50 + rand(50) } # some block
        to define how long the thing should live until it is destroyed.
      MESSAGE
    end
  end
  
  def self.included klass
    klass.extend ClassMethods
  end
  
  def initialize window
    raise LifetimeMissingError.new unless self.respond_to? :lifetime
    super window
    threaded self.lifetime do
      self.destroy!
    end
  end
  
  module ClassMethods
    
    def lifetime lifetime = nil, &block
      block = lambda { lifetime } unless block_given?
      define_method :lifetime, &block
    end
    
  end
  
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
gosu_extensions-0.1.14 lib/traits/short_lived.rb
gosu_extensions-0.1.13 lib/traits/short_lived.rb
gosu_extensions-0.1.12 lib/traits/short_lived.rb