Sha256: af50fc67faa5798716937337d99f35a8484f3a452238f50db513c002fbba455b

Contents?: true

Size: 1.38 KB

Versions: 15

Compression:

Stored size: 1.38 KB

Contents

module Generator extend Trait
  
  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

15 entries across 15 versions & 1 rubygems

Version Path
gosu_extensions-0.2.0 lib/traits/generator.rb
gosu_extensions-0.1.28 lib/traits/generator.rb
gosu_extensions-0.1.27 lib/traits/generator.rb
gosu_extensions-0.1.26 lib/traits/generator.rb
gosu_extensions-0.1.25 lib/traits/generator.rb
gosu_extensions-0.1.24 lib/traits/generator.rb
gosu_extensions-0.1.23 lib/traits/generator.rb
gosu_extensions-0.1.22 lib/traits/generator.rb
gosu_extensions-0.1.21 lib/traits/generator.rb
gosu_extensions-0.1.20 lib/traits/generator.rb
gosu_extensions-0.1.19 lib/traits/generator.rb
gosu_extensions-0.1.18 lib/traits/generator.rb
gosu_extensions-0.1.17 lib/traits/generator.rb
gosu_extensions-0.1.16 lib/traits/generator.rb
gosu_extensions-0.1.15 lib/traits/generator.rb