active_record_samplooper 0.0.3 → 0.0.4
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/Gemfile.lock +1 -1
- data/lib/active_record_samplooper.rb +58 -8
- data/lib/active_record_samplooper/version.rb +1 -1
- data/spec/models/sample_model_spec.rb +50 -1
- metadata +2 -2
    
        checksums.yaml
    CHANGED
    
    | @@ -1,7 +1,7 @@ | |
| 1 1 | 
             
            ---
         | 
| 2 2 | 
             
            SHA1:
         | 
| 3 | 
            -
              metadata.gz:  | 
| 4 | 
            -
              data.tar.gz:  | 
| 3 | 
            +
              metadata.gz: 93d485e74aa6069680edeb330cc7ce8c4c8a376c
         | 
| 4 | 
            +
              data.tar.gz: ab4b473a2b67acc840bbbf937af758f49e0b82b0
         | 
| 5 5 | 
             
            SHA512:
         | 
| 6 | 
            -
              metadata.gz:  | 
| 7 | 
            -
              data.tar.gz:  | 
| 6 | 
            +
              metadata.gz: 360c1d888bbf00a5704752c3e7c75a641e9f33286612c847bf59cee523d39e90ef4d5706d3571760d29c51c3c557bd088e9f228be2c46d33b84c8d7b5af557db
         | 
| 7 | 
            +
              data.tar.gz: a5621a045cf40a4cc1d1fbe504e0d68cb4a8a771cbc7e2c28d7ea548670e44126141be7f37a9db7cc7fd93588d5521dc1d76802526e015565cf89ce2dd7afd7c
         | 
    
        data/Gemfile.lock
    CHANGED
    
    
| @@ -10,6 +10,32 @@ module ActiveRecordSamplooper | |
| 10 10 | 
             
                end
         | 
| 11 11 | 
             
              end
         | 
| 12 12 |  | 
| 13 | 
            +
              class ArrayLooper
         | 
| 14 | 
            +
                attr_accessor :array
         | 
| 15 | 
            +
             | 
| 16 | 
            +
             | 
| 17 | 
            +
                class << self
         | 
| 18 | 
            +
                  def call(*args)
         | 
| 19 | 
            +
                    new(*args)
         | 
| 20 | 
            +
                  end
         | 
| 21 | 
            +
                end
         | 
| 22 | 
            +
             | 
| 23 | 
            +
             | 
| 24 | 
            +
                def initialize(array)
         | 
| 25 | 
            +
                  self.array = array.dup
         | 
| 26 | 
            +
                end
         | 
| 27 | 
            +
             | 
| 28 | 
            +
             | 
| 29 | 
            +
                def find(id)
         | 
| 30 | 
            +
                  raise ActiveRecord::RecordNotFound unless id
         | 
| 31 | 
            +
                  array[id - 1] || raise(Gone, id)
         | 
| 32 | 
            +
                end
         | 
| 33 | 
            +
             | 
| 34 | 
            +
             | 
| 35 | 
            +
                def pluck(*)
         | 
| 36 | 
            +
                  (1..array.size).to_a
         | 
| 37 | 
            +
                end
         | 
| 38 | 
            +
              end
         | 
| 13 39 |  | 
| 14 40 | 
             
              class Samplooper
         | 
| 15 41 | 
             
                attr_accessor :klass, :id_store, :rest
         | 
| @@ -28,20 +54,18 @@ module ActiveRecordSamplooper | |
| 28 54 | 
             
                end
         | 
| 29 55 |  | 
| 30 56 |  | 
| 31 | 
            -
                def sample
         | 
| 32 | 
            -
                   | 
| 57 | 
            +
                def sample(count = 1)
         | 
| 58 | 
            +
                  count > 1 ? count.times.map { do_sample } : do_sample
         | 
| 33 59 | 
             
                end
         | 
| 34 60 |  | 
| 35 61 |  | 
| 36 | 
            -
                def pick
         | 
| 37 | 
            -
                   | 
| 38 | 
            -
                  find(rest.shift)
         | 
| 62 | 
            +
                def pick(count = 1)
         | 
| 63 | 
            +
                  count > 1 ? count.times.map { do_pick } : do_pick
         | 
| 39 64 | 
             
                end
         | 
| 40 65 |  | 
| 41 66 |  | 
| 42 | 
            -
                def loop
         | 
| 43 | 
            -
                   | 
| 44 | 
            -
                  sample
         | 
| 67 | 
            +
                def loop(count = 1)
         | 
| 68 | 
            +
                  count > 1 ? count.times.map { do_loop } : do_loop
         | 
| 45 69 | 
             
                end
         | 
| 46 70 |  | 
| 47 71 |  | 
| @@ -54,6 +78,25 @@ module ActiveRecordSamplooper | |
| 54 78 | 
             
                def reset!
         | 
| 55 79 | 
             
                  self.rest = id_store.dup
         | 
| 56 80 | 
             
                end
         | 
| 81 | 
            +
             | 
| 82 | 
            +
             | 
| 83 | 
            +
                private
         | 
| 84 | 
            +
                def do_sample
         | 
| 85 | 
            +
                  find(id_store.sample)
         | 
| 86 | 
            +
                end
         | 
| 87 | 
            +
             | 
| 88 | 
            +
             | 
| 89 | 
            +
                def do_pick
         | 
| 90 | 
            +
                  return if rest.blank?
         | 
| 91 | 
            +
                  find(rest.shift)
         | 
| 92 | 
            +
                end
         | 
| 93 | 
            +
             | 
| 94 | 
            +
             | 
| 95 | 
            +
                def do_loop
         | 
| 96 | 
            +
                  reset! if rest.blank?
         | 
| 97 | 
            +
                  pick
         | 
| 98 | 
            +
                end
         | 
| 99 | 
            +
             | 
| 57 100 | 
             
              end
         | 
| 58 101 |  | 
| 59 102 | 
             
              class Gone < StandardError
         | 
| @@ -66,6 +109,13 @@ module ActiveRecordSamplooper | |
| 66 109 | 
             
              end
         | 
| 67 110 | 
             
            end
         | 
| 68 111 |  | 
| 112 | 
            +
            class ::Array
         | 
| 113 | 
            +
              def sampler
         | 
| 114 | 
            +
                ActiveRecordSamplooper.(ActiveRecordSamplooper::ArrayLooper.(self))
         | 
| 115 | 
            +
              end
         | 
| 116 | 
            +
            end
         | 
| 117 | 
            +
             | 
| 118 | 
            +
             | 
| 69 119 | 
             
            class ::ActiveRecord::Base
         | 
| 70 120 | 
             
              class << self
         | 
| 71 121 | 
             
                def sample
         | 
| @@ -59,7 +59,7 @@ RSpec.describe SampleModel, type: :model do | |
| 59 59 | 
             
                  it 'loop get destroyed raise exception' do
         | 
| 60 60 | 
             
                    sampler
         | 
| 61 61 | 
             
                    SampleModel.first.destroy
         | 
| 62 | 
            -
                    SampleModel. | 
| 62 | 
            +
                    SampleModel.last.destroy
         | 
| 63 63 | 
             
                    expect {
         | 
| 64 64 | 
             
                      10.times { sampler.loop }
         | 
| 65 65 | 
             
                    }.to raise_exception(ActiveRecordSamplooper::Gone)
         | 
| @@ -89,4 +89,53 @@ RSpec.describe SampleModel, type: :model do | |
| 89 89 | 
             
                  expect(sampler.pick).to be_nil
         | 
| 90 90 | 
             
                end
         | 
| 91 91 | 
             
              end
         | 
| 92 | 
            +
             | 
| 93 | 
            +
              describe 'array samplooper' do
         | 
| 94 | 
            +
                let(:array) { (:a..:f).to_a }
         | 
| 95 | 
            +
             | 
| 96 | 
            +
                describe 'from sampler' do
         | 
| 97 | 
            +
                  let(:sampler) { array.sampler }
         | 
| 98 | 
            +
             | 
| 99 | 
            +
                  describe 'sampler pick' do
         | 
| 100 | 
            +
                    it 'any times, get SampleModel instance' do
         | 
| 101 | 
            +
                      36.times { expect(sampler.sample).to be_a(Symbol) }
         | 
| 102 | 
            +
                    end
         | 
| 103 | 
            +
                  end
         | 
| 104 | 
            +
             | 
| 105 | 
            +
                  it 'sample get once each' do
         | 
| 106 | 
            +
                    6.times { expect(sampler.pick).to be_a(Symbol) }
         | 
| 107 | 
            +
                    expect(sampler.pick).to be_nil
         | 
| 108 | 
            +
                  end
         | 
| 109 | 
            +
             | 
| 110 | 
            +
                  it 'loop get any times each' do
         | 
| 111 | 
            +
                    100.times do
         | 
| 112 | 
            +
                      all = (:a..:f).to_a if all.blank?
         | 
| 113 | 
            +
             | 
| 114 | 
            +
                      expect(all.delete(sampler.loop)).not_to be_nil
         | 
| 115 | 
            +
                    end
         | 
| 116 | 
            +
                  end
         | 
| 117 | 
            +
             | 
| 118 | 
            +
                  it 'sample get destroyed raise exception' do
         | 
| 119 | 
            +
                    array[4] = nil
         | 
| 120 | 
            +
                    sampler
         | 
| 121 | 
            +
                    expect {
         | 
| 122 | 
            +
                      7.times { sampler.pick }
         | 
| 123 | 
            +
                    }.to raise_exception(ActiveRecordSamplooper::Gone)
         | 
| 124 | 
            +
                  end
         | 
| 125 | 
            +
             | 
| 126 | 
            +
                  it 'loop get destroyed raise exception' do
         | 
| 127 | 
            +
                    array[4] = nil
         | 
| 128 | 
            +
                    sampler
         | 
| 129 | 
            +
                    expect {
         | 
| 130 | 
            +
                      7.times { sampler.loop }
         | 
| 131 | 
            +
                    }.to raise_exception(ActiveRecordSamplooper::Gone)
         | 
| 132 | 
            +
                  end
         | 
| 133 | 
            +
             | 
| 134 | 
            +
                  it 'sampler not include new instance' do
         | 
| 135 | 
            +
                    sampler
         | 
| 136 | 
            +
                    array.push(:aa)
         | 
| 137 | 
            +
                    1000.times { expect(sampler.sample).not_to eq(:aa) }
         | 
| 138 | 
            +
                  end
         | 
| 139 | 
            +
                end
         | 
| 140 | 
            +
              end
         | 
| 92 141 | 
             
            end
         | 
    
        metadata
    CHANGED
    
    | @@ -1,14 +1,14 @@ | |
| 1 1 | 
             
            --- !ruby/object:Gem::Specification
         | 
| 2 2 | 
             
            name: active_record_samplooper
         | 
| 3 3 | 
             
            version: !ruby/object:Gem::Version
         | 
| 4 | 
            -
              version: 0.0. | 
| 4 | 
            +
              version: 0.0.4
         | 
| 5 5 | 
             
            platform: ruby
         | 
| 6 6 | 
             
            authors:
         | 
| 7 7 | 
             
            - mmmpa
         | 
| 8 8 | 
             
            autorequire: 
         | 
| 9 9 | 
             
            bindir: bin
         | 
| 10 10 | 
             
            cert_chain: []
         | 
| 11 | 
            -
            date: 2015-06- | 
| 11 | 
            +
            date: 2015-06-26 00:00:00.000000000 Z
         | 
| 12 12 | 
             
            dependencies:
         | 
| 13 13 | 
             
            - !ruby/object:Gem::Dependency
         | 
| 14 14 | 
             
              name: rails
         |