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(count = 1) ⇒ Object
- #pick(count = 1) ⇒ Object
- #reset! ⇒ Object
- #sample(count = 1) ⇒ Object
Constructor Details
#initialize(klass) ⇒ Samplooper
Returns a new instance of Samplooper.
44 45 46 47 |
# File 'lib/active_record_samplooper.rb', line 44 def initialize(klass) self.klass = klass init! end |
Instance Attribute Details
#id_store ⇒ Object
Returns the value of attribute id_store.
41 42 43 |
# File 'lib/active_record_samplooper.rb', line 41 def id_store @id_store end |
#klass ⇒ Object
Returns the value of attribute klass.
41 42 43 |
# File 'lib/active_record_samplooper.rb', line 41 def klass @klass end |
#rest ⇒ Object
Returns the value of attribute rest.
41 42 43 |
# File 'lib/active_record_samplooper.rb', line 41 def rest @rest end |
Instance Method Details
#find(id) ⇒ Object
50 51 52 53 54 |
# File 'lib/active_record_samplooper.rb', line 50 def find(id) klass.find(id) rescue ActiveRecord::RecordNotFound => e raise Gone, id end |
#init! ⇒ Object
72 73 74 75 |
# File 'lib/active_record_samplooper.rb', line 72 def init! self.id_store = klass.pluck(:id).shuffle! reset! end |
#loop(count = 1) ⇒ Object
67 68 69 |
# File 'lib/active_record_samplooper.rb', line 67 def loop(count = 1) count > 1 ? count.times.map { do_looping } : do_looping end |
#pick(count = 1) ⇒ Object
62 63 64 |
# File 'lib/active_record_samplooper.rb', line 62 def pick(count = 1) count > 1 ? count.times.map { do_picking } : do_picking end |
#reset! ⇒ Object
78 79 80 |
# File 'lib/active_record_samplooper.rb', line 78 def reset! self.rest = id_store.dup.shuffle end |
#sample(count = 1) ⇒ Object
57 58 59 |
# File 'lib/active_record_samplooper.rb', line 57 def sample(count = 1) count > 1 ? count.times.map { do_sampling } : do_sampling end |