Class: ActiveRecordSamplooper::Samplooper

Inherits:
Object
  • Object
show all
Defined in:
lib/active_record_samplooper.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(klass) ⇒ Samplooper

Returns a new instance of Samplooper.



18
19
20
21
# File 'lib/active_record_samplooper.rb', line 18

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

Instance Attribute Details

#id_storeObject

Returns the value of attribute id_store.



15
16
17
# File 'lib/active_record_samplooper.rb', line 15

def id_store
  @id_store
end

#klassObject

Returns the value of attribute klass.



15
16
17
# File 'lib/active_record_samplooper.rb', line 15

def klass
  @klass
end

#restObject

Returns the value of attribute rest.



15
16
17
# File 'lib/active_record_samplooper.rb', line 15

def rest
  @rest
end

Instance Method Details

#find(id) ⇒ Object



24
25
26
27
28
# File 'lib/active_record_samplooper.rb', line 24

def find(id)
  klass.find(id)
rescue ActiveRecord::RecordNotFound => e
  raise Gone, id
end

#init!Object



48
49
50
51
# File 'lib/active_record_samplooper.rb', line 48

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

#loopObject



42
43
44
45
# File 'lib/active_record_samplooper.rb', line 42

def loop
  reset! if rest.blank?
  sample
end

#pickObject



36
37
38
39
# File 'lib/active_record_samplooper.rb', line 36

def pick
  return if rest.blank?
  find(rest.shift)
end

#reset!Object



54
55
56
# File 'lib/active_record_samplooper.rb', line 54

def reset!
  self.rest = id_store.dup
end

#sampleObject



31
32
33
# File 'lib/active_record_samplooper.rb', line 31

def sample
  find(id_store.sample)
end