Sha256: 949b70549ebed4e1421fc89379f79e67dddc6c5cba0e7fc91a4cfadbf587dac9

Contents?: true

Size: 1.37 KB

Versions: 7

Compression:

Stored size: 1.37 KB

Contents

module Generator
  
  def self.included base
    base.extend ClassMethods
  end
  
  module ClassMethods
    
    def generates klass, options = {}
      self.send :include, InstanceMethods
      
      rate   = options[:every]
      til    = options[:until]
      offset = options[:starting_at]
      
      class_eval <<-GENERATION
def start_generating klass = #{klass.name}, every_rate = #{rate || 10}, til = #{til || false}, offset = #{offset || rate || 10}
  return if til && til <= 0
  threaded offset, &generation(klass, every_rate, til)
end
      GENERATION
      
      # If an offset is given, start generating after the offset, else after start_generating is called.
      #
      if offset
        InitializerHooks.register self do
          start_generating
        end
      end
      
    end
    
  end
  
  module InstanceMethods
    
    def destroy!
      stop_generating!
      super
    end
    
    def stop_generating!
      @stop_generating = true
    end
    
    def generation klass, every_rate, til
      return lambda {} if @stop_generating
      lambda do
        self.generate klass
        til = til - every_rate if til
        self.start_generating klass, every_rate, til, every_rate
      end
    end
    
    def generate klass
      generated = klass.new self.window
      generated.warp self.position
      self.window.register generated
    end
    
  end
  
end

Version data entries

7 entries across 7 versions & 1 rubygems

Version Path
gosu_extensions-0.1.14 lib/traits/generator.rb
gosu_extensions-0.1.13 lib/traits/generator.rb
gosu_extensions-0.1.12 lib/traits/generator.rb
gosu_extensions-0.1.11 lib/traits/generator.rb
gosu_extensions-0.1.10 lib/traits/generator.rb
gosu_extensions-0.1.9 lib/traits/generator.rb
gosu_extensions-0.1.8 lib/traits/generator.rb