prosto_cache 0.2.2 → 0.2.3
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.
- data/lib/prosto_cache/prosto_model_cache.rb +9 -5
 - data/spec/prosto_cache_spec.rb +24 -0
 - metadata +2 -2
 
| 
         @@ -137,10 +137,11 @@ module ProstoCache 
     | 
|
| 
       137 
137 
     | 
    
         | 
| 
       138 
138 
     | 
    
         
             
                  cache_values = model_class.all
         
     | 
| 
       139 
139 
     | 
    
         
             
                  self.cache = build_cache(cache_values, accessor_keys)
         
     | 
| 
       140 
     | 
    
         
            -
                  cache.values(sorted_cache_values(cache_values))
         
     | 
| 
       141 
     | 
    
         
            -
                  cache.keys(sorted_keys(cache.values))
         
     | 
| 
       142 
140 
     | 
    
         
             
                  self.validated_at = time
         
     | 
| 
       143 
141 
     | 
    
         
             
                  self.signature = current_cache_signature
         
     | 
| 
      
 142 
     | 
    
         
            +
             
     | 
| 
      
 143 
     | 
    
         
            +
                  cache.values(sorted_cache_values(cache_values))
         
     | 
| 
      
 144 
     | 
    
         
            +
                  cache.keys(sorted_keys(cache.values))
         
     | 
| 
       144 
145 
     | 
    
         
             
                end
         
     | 
| 
       145 
146 
     | 
    
         | 
| 
       146 
147 
     | 
    
         | 
| 
         @@ -169,8 +170,9 @@ module ProstoCache 
     | 
|
| 
       169 
170 
     | 
    
         | 
| 
       170 
171 
     | 
    
         
             
                def sorted_cache_values(cache_values)
         
     | 
| 
       171 
172 
     | 
    
         
             
                  cache_values.sort_by { |o|
         
     | 
| 
       172 
     | 
    
         
            -
                    sort_keys. 
     | 
| 
       173 
     | 
    
         
            -
                       
     | 
| 
      
 173 
     | 
    
         
            +
                    sort_keys.each_with_object('') { |k, memo|
         
     | 
| 
      
 174 
     | 
    
         
            +
                      sort_key = o.public_send(k)
         
     | 
| 
      
 175 
     | 
    
         
            +
                      memo << sort_key if sort_key
         
     | 
| 
       174 
176 
     | 
    
         
             
                    }
         
     | 
| 
       175 
177 
     | 
    
         
             
                  }
         
     | 
| 
       176 
178 
     | 
    
         
             
                end
         
     | 
| 
         @@ -178,7 +180,9 @@ module ProstoCache 
     | 
|
| 
       178 
180 
     | 
    
         
             
                def sorted_keys(cache_values)
         
     | 
| 
       179 
181 
     | 
    
         
             
                  cache_values.map { |o|
         
     | 
| 
       180 
182 
     | 
    
         
             
                    accessor_keys.inject([]) { |memo, k|
         
     | 
| 
       181 
     | 
    
         
            -
                       
     | 
| 
      
 183 
     | 
    
         
            +
                      sort_key = o.public_send(k)
         
     | 
| 
      
 184 
     | 
    
         
            +
                      raise BadCacheValuesError, "Null key '#{k}' found in value #{o.inspect}" unless sort_key
         
     | 
| 
      
 185 
     | 
    
         
            +
                      memo << sort_key.to_sym
         
     | 
| 
       182 
186 
     | 
    
         
             
                    }
         
     | 
| 
       183 
187 
     | 
    
         
             
                  }.tap { |rtn|
         
     | 
| 
       184 
188 
     | 
    
         
             
                    rtn.flatten! if accessor_keys.length == 1
         
     | 
    
        data/spec/prosto_cache_spec.rb
    CHANGED
    
    | 
         @@ -45,6 +45,14 @@ describe ProstoCache::ProstoModelCache do 
     | 
|
| 
       45 
45 
     | 
    
         
             
                  it "should return all keys from the cache" do
         
     | 
| 
       46 
46 
     | 
    
         
             
                    model_class.cache.keys.should == [:bar, :foo]
         
     | 
| 
       47 
47 
     | 
    
         
             
                  end
         
     | 
| 
      
 48 
     | 
    
         
            +
             
     | 
| 
      
 49 
     | 
    
         
            +
                  context 'when key for one of the rows is nil' do
         
     | 
| 
      
 50 
     | 
    
         
            +
                    it "should raise meaningful error" do
         
     | 
| 
      
 51 
     | 
    
         
            +
                      model_class.should_receive(:all).once.and_return(['foo', nil].map { |n| model_class.new(n) })
         
     | 
| 
      
 52 
     | 
    
         
            +
             
     | 
| 
      
 53 
     | 
    
         
            +
                      expect { model_class.cache.keys }.to raise_error ProstoCache::BadCacheValuesError
         
     | 
| 
      
 54 
     | 
    
         
            +
                    end
         
     | 
| 
      
 55 
     | 
    
         
            +
                  end
         
     | 
| 
       48 
56 
     | 
    
         
             
                end
         
     | 
| 
       49 
57 
     | 
    
         | 
| 
       50 
58 
     | 
    
         
             
                describe "#values" do
         
     | 
| 
         @@ -61,6 +69,14 @@ describe ProstoCache::ProstoModelCache do 
     | 
|
| 
       61 
69 
     | 
    
         
             
                    values.map(&:name).should == %w(bar foo)
         
     | 
| 
       62 
70 
     | 
    
         
             
                  end
         
     | 
| 
       63 
71 
     | 
    
         | 
| 
      
 72 
     | 
    
         
            +
                  context 'when key for one of the rows is nil' do
         
     | 
| 
      
 73 
     | 
    
         
            +
                    it "should raise meaningful error" do
         
     | 
| 
      
 74 
     | 
    
         
            +
                      model_class.should_receive(:all).once.and_return(['foo', nil].map { |n| model_class.new(n) })
         
     | 
| 
      
 75 
     | 
    
         
            +
             
     | 
| 
      
 76 
     | 
    
         
            +
                      expect { model_class.cache.values }.to raise_error ProstoCache::BadCacheValuesError
         
     | 
| 
      
 77 
     | 
    
         
            +
                    end
         
     | 
| 
      
 78 
     | 
    
         
            +
                  end
         
     | 
| 
      
 79 
     | 
    
         
            +
             
     | 
| 
       64 
80 
     | 
    
         
             
                  context 'when sort_keys are provided' do
         
     | 
| 
       65 
81 
     | 
    
         
             
                    let(:model_class) {
         
     | 
| 
       66 
82 
     | 
    
         
             
                      Class.new {
         
     | 
| 
         @@ -98,6 +114,14 @@ describe ProstoCache::ProstoModelCache do 
     | 
|
| 
       98 
114 
     | 
    
         
             
                    model_class.cache[:foo]
         
     | 
| 
       99 
115 
     | 
    
         
             
                  end
         
     | 
| 
       100 
116 
     | 
    
         | 
| 
      
 117 
     | 
    
         
            +
                  context 'when key for one of the rows is nil' do
         
     | 
| 
      
 118 
     | 
    
         
            +
                    it "should raise meaningful error when other key is accessed" do
         
     | 
| 
      
 119 
     | 
    
         
            +
                      model_class.should_receive(:all).once.and_return(['foo', nil].map { |n| model_class.new(n) })
         
     | 
| 
      
 120 
     | 
    
         
            +
             
     | 
| 
      
 121 
     | 
    
         
            +
                      expect { model_class.cache[:foo] }.to raise_error ProstoCache::BadCacheValuesError
         
     | 
| 
      
 122 
     | 
    
         
            +
                    end
         
     | 
| 
      
 123 
     | 
    
         
            +
                  end
         
     | 
| 
      
 124 
     | 
    
         
            +
             
     | 
| 
       101 
125 
     | 
    
         
             
                  context 'when key is symbol' do
         
     | 
| 
       102 
126 
     | 
    
         
             
                    it "should raise an error for key that was not found" do
         
     | 
| 
       103 
127 
     | 
    
         
             
                      expect { model_class.cache[:nondef] }.to raise_error ProstoCache::BadCacheKeyError
         
     | 
    
        metadata
    CHANGED
    
    | 
         @@ -1,7 +1,7 @@ 
     | 
|
| 
       1 
1 
     | 
    
         
             
            --- !ruby/object:Gem::Specification
         
     | 
| 
       2 
2 
     | 
    
         
             
            name: prosto_cache
         
     | 
| 
       3 
3 
     | 
    
         
             
            version: !ruby/object:Gem::Version
         
     | 
| 
       4 
     | 
    
         
            -
              version: 0.2. 
     | 
| 
      
 4 
     | 
    
         
            +
              version: 0.2.3
         
     | 
| 
       5 
5 
     | 
    
         
             
              prerelease: 
         
     | 
| 
       6 
6 
     | 
    
         
             
            platform: ruby
         
     | 
| 
       7 
7 
     | 
    
         
             
            authors:
         
     | 
| 
         @@ -9,7 +9,7 @@ authors: 
     | 
|
| 
       9 
9 
     | 
    
         
             
            autorequire: 
         
     | 
| 
       10 
10 
     | 
    
         
             
            bindir: bin
         
     | 
| 
       11 
11 
     | 
    
         
             
            cert_chain: []
         
     | 
| 
       12 
     | 
    
         
            -
            date: 2013- 
     | 
| 
      
 12 
     | 
    
         
            +
            date: 2013-11-04 00:00:00.000000000 Z
         
     | 
| 
       13 
13 
     | 
    
         
             
            dependencies:
         
     | 
| 
       14 
14 
     | 
    
         
             
            - !ruby/object:Gem::Dependency
         
     | 
| 
       15 
15 
     | 
    
         
             
              name: rspec
         
     |