Sha256: a27ba47d27d6713b85e446f493ed2db5aeddc619326b0995fed257add500f18d

Contents?: true

Size: 907 Bytes

Versions: 1

Compression:

Stored size: 907 Bytes

Contents

module ActiveRecordSamplooper
  class << self
    def call(*args)
      ready(*args)
    end


    def ready(klass)
      Samplooper.new(klass)
    end
  end


  class Samplooper
    attr_accessor :klass, :id_store, :rest


    def initialize(klass)
      self.klass = klass
      reset!
    end


    def sample
      return if rest.blank?
      klass.find(id = rest.shift)
    rescue ActiveRecord::RecordNotFound => e
      raise Gone, id
    end


    def loop
      reset! if rest.blank?
      sample
    end


    def reset!
      self.id_store = klass.pluck(:id).shuffle!
      self.rest = id_store.dup
    end
  end

  class Gone < StandardError
    attr_accessor :id


    def initialize(id)
      self.id = id
    end
  end
end

class ::ActiveRecord::Base
  class << self
    def sample
      offset(rand(count)).first
    end


    def sampler
      ActiveRecordSamplooper.(self)
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
active_record_samplooper-0.0.1 lib/active_record_samplooper.rb