Sha256: d66c9eaf9ec2bba24d7eb097e090d23a1c6d0024b644ab87699d3d45b868833d

Contents?: true

Size: 1.42 KB

Versions: 1

Compression:

Stored size: 1.42 KB

Contents

require File.dirname(__FILE__) + '/pool'

# module for inclusion
module RuPol
  def self.included(base_class)
    base_class.class_eval do
      include InstanceMethods
      extend ClassMethods
      
      attr_accessor :_recycled
    end
  end
  
  module ClassMethods
    def _pool
      @pool ||= Pool.new(1000, self)
    end
    
    def max_pool_size(max_size)
      _pool.max_size = max_size
    end
    
    def empty_pool!
      _pool.empty!
    end
    
    def rehydrate(*init_opts)
      instance = _pool.get
      if instance
        instance.instance_eval { init_opts.empty? ? initialize : initialize(*init_opts) }
      end
      instance
    end
  end
  
  module InstanceMethods
    def recycle
      _pool << self
      self
    end
    
    def _pool
      self.class._pool
    end
  end
  
  module Swimsuit
    def self.included(base_class)
      base_class.class_eval do
        include ::RuPol
        include InstanceMethods
        extend ClassMethods
      end
    end
    
    module InstanceMethods
      def clear
        super rescue nil
        instance_variables.each do |var|
          instance_variable_set(var, nil) unless var == "@_recycled"
        end
        self
      end
      
      def destroy
        super rescue nil
        recycle
      end
    end
    
    module ClassMethods
      def new(*init_opts)
        rehydrate || ( init_opts.empty? ? super() : super(*init_opts) )
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
ru_pol-0.1.0 lib/ru_pol.rb