Sha256: fe13dae5365eb20409b243a3bcf2e9472ecac8f31a5a6856a308d15e1118b48f

Contents?: true

Size: 1.6 KB

Versions: 4

Compression:

Stored size: 1.6 KB

Contents

module Generator extend Trait
  
  def self.start klass, *args
    [:start_generating, klass, *args]
  end
  Start = :start_generating
  
  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 destroyed!
      stop_generating!
    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
    
  end
  
  # Returns the generated thing.
  #
  # TODO generate klass, times = 1, &after_generation ?
  #
  def generate klass
    generated = klass.new self.window
    generated.warp self.position
    self.window.register generated
    generated
  end
  
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
gosu_extensions-0.2.9 lib/traits/generator.rb
gosu_extensions-0.2.8 lib/traits/generator.rb
gosu_extensions-0.2.7 lib/traits/generator.rb
gosu_extensions-0.2.6 lib/traits/generator.rb