Class: ActiveRecordSamplooper::Samplooper
- Inherits:
-
Object
- Object
- ActiveRecordSamplooper::Samplooper
- Defined in:
- lib/active_record_samplooper.rb
Instance Attribute Summary collapse
-
#id_store ⇒ Object
Returns the value of attribute id_store.
-
#klass ⇒ Object
Returns the value of attribute klass.
-
#rest ⇒ Object
Returns the value of attribute rest.
Instance Method Summary collapse
- #find(id) ⇒ Object
- #init! ⇒ Object
-
#initialize(klass) ⇒ Samplooper
constructor
A new instance of Samplooper.
- #loop ⇒ Object
- #pick ⇒ Object
- #reset! ⇒ Object
- #sample ⇒ Object
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_store ⇒ Object
Returns the value of attribute id_store.
15 16 17 |
# File 'lib/active_record_samplooper.rb', line 15 def id_store @id_store end |
#klass ⇒ Object
Returns the value of attribute klass.
15 16 17 |
# File 'lib/active_record_samplooper.rb', line 15 def klass @klass end |
#rest ⇒ Object
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 |
#loop ⇒ Object
42 43 44 45 |
# File 'lib/active_record_samplooper.rb', line 42 def loop reset! if rest.blank? sample end |
#pick ⇒ Object
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 |
#sample ⇒ Object
31 32 33 |
# File 'lib/active_record_samplooper.rb', line 31 def sample find(id_store.sample) end |