with_timed_cache 0.0.1
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 +7 -0
- data/.gitignore +17 -0
- data/Gemfile +4 -0
- data/Guardfile +9 -0
- data/LICENSE.txt +22 -0
- data/README.md +30 -0
- data/Rakefile +1 -0
- data/lib/with_timed_cache/cache.rb +47 -0
- data/lib/with_timed_cache/caches.rb +38 -0
- data/lib/with_timed_cache/json_cache.rb +21 -0
- data/lib/with_timed_cache/version.rb +3 -0
- data/lib/with_timed_cache/yaml_cache.rb +29 -0
- data/lib/with_timed_cache.rb +21 -0
- data/spec/lib/with_timed_cache/cache_spec.rb +17 -0
- data/spec/lib/with_timed_cache/caches_spec.rb +29 -0
- data/spec/lib/with_timed_cache/json_cache_spec.rb +17 -0
- data/spec/lib/with_timed_cache/yaml_cache_spec.rb +17 -0
- data/spec/lib/with_timed_cache_spec.rb +110 -0
- data/spec/spec_helper.rb +7 -0
- data/with_timed_cache.gemspec +27 -0
- metadata +154 -0
    
        checksums.yaml
    ADDED
    
    | @@ -0,0 +1,7 @@ | |
| 1 | 
            +
            ---
         | 
| 2 | 
            +
            SHA1:
         | 
| 3 | 
            +
              metadata.gz: db6c5fa7ef0d80a8b7f22c41b4866bfc72ddfb11
         | 
| 4 | 
            +
              data.tar.gz: 7a48bf47391b62f553ea51111247128380e5801c
         | 
| 5 | 
            +
            SHA512:
         | 
| 6 | 
            +
              metadata.gz: 0234405ce5740080331826bb34fdd213f9be4d93d092204cc82e8c348a9346ae524778ede7d5ec73e993ec4da11ae9eb128e2be819323e949f8f978f43691acb
         | 
| 7 | 
            +
              data.tar.gz: 9534829271c2aeb0c0f8fb67db6eee415a5067a2561d59c054cace9e846f238accc98ac1dc8d197d51854a8214bcaa11e9a16d97f27a7b3c2204c139fb073e5b
         | 
    
        data/.gitignore
    ADDED
    
    
    
        data/Gemfile
    ADDED
    
    
    
        data/Guardfile
    ADDED
    
    
    
        data/LICENSE.txt
    ADDED
    
    | @@ -0,0 +1,22 @@ | |
| 1 | 
            +
            Copyright (c) 2013 Jordan Stephens
         | 
| 2 | 
            +
             | 
| 3 | 
            +
            MIT License
         | 
| 4 | 
            +
             | 
| 5 | 
            +
            Permission is hereby granted, free of charge, to any person obtaining
         | 
| 6 | 
            +
            a copy of this software and associated documentation files (the
         | 
| 7 | 
            +
            "Software"), to deal in the Software without restriction, including
         | 
| 8 | 
            +
            without limitation the rights to use, copy, modify, merge, publish,
         | 
| 9 | 
            +
            distribute, sublicense, and/or sell copies of the Software, and to
         | 
| 10 | 
            +
            permit persons to whom the Software is furnished to do so, subject to
         | 
| 11 | 
            +
            the following conditions:
         | 
| 12 | 
            +
             | 
| 13 | 
            +
            The above copyright notice and this permission notice shall be
         | 
| 14 | 
            +
            included in all copies or substantial portions of the Software.
         | 
| 15 | 
            +
             | 
| 16 | 
            +
            THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
         | 
| 17 | 
            +
            EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
         | 
| 18 | 
            +
            MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
         | 
| 19 | 
            +
            NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
         | 
| 20 | 
            +
            LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
         | 
| 21 | 
            +
            OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
         | 
| 22 | 
            +
            WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
         | 
    
        data/README.md
    ADDED
    
    | @@ -0,0 +1,30 @@ | |
| 1 | 
            +
            # WithTimedCache
         | 
| 2 | 
            +
             | 
| 3 | 
            +
            A simple, time-based cache.
         | 
| 4 | 
            +
             | 
| 5 | 
            +
            ## Installation
         | 
| 6 | 
            +
             | 
| 7 | 
            +
            Add this line to your application's Gemfile:
         | 
| 8 | 
            +
             | 
| 9 | 
            +
                gem 'with_timed_cache'
         | 
| 10 | 
            +
             | 
| 11 | 
            +
            And then execute:
         | 
| 12 | 
            +
             | 
| 13 | 
            +
                $ bundle
         | 
| 14 | 
            +
             | 
| 15 | 
            +
            Or install it yourself as:
         | 
| 16 | 
            +
             | 
| 17 | 
            +
                $ gem install with_timed_cache
         | 
| 18 | 
            +
             | 
| 19 | 
            +
            ## Usage
         | 
| 20 | 
            +
             | 
| 21 | 
            +
                with_timed_cache(key, max_age: 1.hour) do
         | 
| 22 | 
            +
                  # some operation you would like cached for a certain amount of time
         | 
| 23 | 
            +
                end
         | 
| 24 | 
            +
             | 
| 25 | 
            +
            **Options**
         | 
| 26 | 
            +
             | 
| 27 | 
            +
            * `max_age`: how long to use the cached data, ex: `30.minutes`
         | 
| 28 | 
            +
            * `location`: the directory path in which to store cache files
         | 
| 29 | 
            +
            * `format`: cache data may be persisted as `:json`, `:yaml`, or the default of marshaling objects to strings will be used.
         | 
| 30 | 
            +
             | 
    
        data/Rakefile
    ADDED
    
    | @@ -0,0 +1 @@ | |
| 1 | 
            +
            require "bundler/gem_tasks"
         | 
| @@ -0,0 +1,47 @@ | |
| 1 | 
            +
            require "active_support/core_ext/numeric/time"
         | 
| 2 | 
            +
             | 
| 3 | 
            +
            module WithTimedCache
         | 
| 4 | 
            +
              class Cache
         | 
| 5 | 
            +
             | 
| 6 | 
            +
                attr_reader :key, :location, :max_age
         | 
| 7 | 
            +
             | 
| 8 | 
            +
                def initialize(key, opts = {})
         | 
| 9 | 
            +
                  @key = key
         | 
| 10 | 
            +
                  @max_age = opts[:max_age] || 1.hour
         | 
| 11 | 
            +
                  @location = opts[:location] || File.join("tmp", filename)
         | 
| 12 | 
            +
                  @location = File.join(@location, filename) if File.directory?(@location)
         | 
| 13 | 
            +
                  self
         | 
| 14 | 
            +
                end
         | 
| 15 | 
            +
             | 
| 16 | 
            +
                def stale?
         | 
| 17 | 
            +
                  File.mtime(@location) < @max_age.ago
         | 
| 18 | 
            +
                end
         | 
| 19 | 
            +
             | 
| 20 | 
            +
                def exists?
         | 
| 21 | 
            +
                  File.exists? @location
         | 
| 22 | 
            +
                end
         | 
| 23 | 
            +
             | 
| 24 | 
            +
                def read
         | 
| 25 | 
            +
                  Marshal.load(File.read(@location))
         | 
| 26 | 
            +
                end
         | 
| 27 | 
            +
             | 
| 28 | 
            +
                def write(data)
         | 
| 29 | 
            +
                  File.open(@location, 'w') { |f| f.write Marshal.dump(data) }
         | 
| 30 | 
            +
                end
         | 
| 31 | 
            +
             | 
| 32 | 
            +
                def filename
         | 
| 33 | 
            +
                  extension = self.class.file_extension
         | 
| 34 | 
            +
                  filename = "#{@key}_cache"
         | 
| 35 | 
            +
                  filename += ".#{extension}" unless extension.empty?
         | 
| 36 | 
            +
                  filename
         | 
| 37 | 
            +
                end
         | 
| 38 | 
            +
             | 
| 39 | 
            +
                def self.file_extension
         | 
| 40 | 
            +
                  @file_extension ||= self.local_class_name.sub(/Cache$/, "").downcase
         | 
| 41 | 
            +
                end
         | 
| 42 | 
            +
             | 
| 43 | 
            +
                def self.local_class_name
         | 
| 44 | 
            +
                  @local_class_name ||= self.to_s.split("::").last rescue self.to_s
         | 
| 45 | 
            +
                end
         | 
| 46 | 
            +
              end
         | 
| 47 | 
            +
            end
         | 
| @@ -0,0 +1,38 @@ | |
| 1 | 
            +
             | 
| 2 | 
            +
            require "with_timed_cache/json_cache"
         | 
| 3 | 
            +
            require "with_timed_cache/yaml_cache"
         | 
| 4 | 
            +
             | 
| 5 | 
            +
            module WithTimedCache
         | 
| 6 | 
            +
              class InvalidCacheFormatException < Exception; end
         | 
| 7 | 
            +
             | 
| 8 | 
            +
              class Caches
         | 
| 9 | 
            +
             | 
| 10 | 
            +
                @caches = []
         | 
| 11 | 
            +
             | 
| 12 | 
            +
                class << self
         | 
| 13 | 
            +
                  def find_or_create(key, opts = {})
         | 
| 14 | 
            +
                    cache = find(key) || create(key, opts)
         | 
| 15 | 
            +
                    @caches << cache
         | 
| 16 | 
            +
                    cache
         | 
| 17 | 
            +
                  end
         | 
| 18 | 
            +
             | 
| 19 | 
            +
                  def find(key)
         | 
| 20 | 
            +
                    @caches.select { |c| c.key == key }.first
         | 
| 21 | 
            +
                  end
         | 
| 22 | 
            +
             | 
| 23 | 
            +
                  def create(key, opts = {})
         | 
| 24 | 
            +
                    cache_class(opts[:format]).new(key, opts)
         | 
| 25 | 
            +
                  end
         | 
| 26 | 
            +
             | 
| 27 | 
            +
                  def cache_class(format = "")
         | 
| 28 | 
            +
                    format = format.to_s.upcase
         | 
| 29 | 
            +
                    begin
         | 
| 30 | 
            +
                      Object.const_get("WithTimedCache::#{format}Cache")
         | 
| 31 | 
            +
                    rescue
         | 
| 32 | 
            +
                      raise WithTimedCache::InvalidCacheFormatException
         | 
| 33 | 
            +
                    end
         | 
| 34 | 
            +
                  end
         | 
| 35 | 
            +
                end
         | 
| 36 | 
            +
              end
         | 
| 37 | 
            +
            end
         | 
| 38 | 
            +
             | 
| @@ -0,0 +1,21 @@ | |
| 1 | 
            +
            require "with_timed_cache/cache"
         | 
| 2 | 
            +
            require "json"
         | 
| 3 | 
            +
             | 
| 4 | 
            +
            module WithTimedCache
         | 
| 5 | 
            +
              class JSONCache < Cache
         | 
| 6 | 
            +
                def initialize(key, opts = {})
         | 
| 7 | 
            +
                  super
         | 
| 8 | 
            +
                  @location = opts[:location] || File.join("tmp", filename)
         | 
| 9 | 
            +
                  @location = File.join(@location, filename) if File.directory?(@location)
         | 
| 10 | 
            +
                end
         | 
| 11 | 
            +
             | 
| 12 | 
            +
                def read
         | 
| 13 | 
            +
                  JSON.parse(File.read(@location), symbolize_names: true)
         | 
| 14 | 
            +
                end
         | 
| 15 | 
            +
             | 
| 16 | 
            +
                def write(data)
         | 
| 17 | 
            +
                  File.open(@location, 'w') { |f| f.write data.to_json }
         | 
| 18 | 
            +
                end
         | 
| 19 | 
            +
              end
         | 
| 20 | 
            +
            end
         | 
| 21 | 
            +
             | 
| @@ -0,0 +1,29 @@ | |
| 1 | 
            +
            require "with_timed_cache/cache"
         | 
| 2 | 
            +
            require "yaml"
         | 
| 3 | 
            +
             | 
| 4 | 
            +
            module WithTimedCache
         | 
| 5 | 
            +
              class YAMLCache < Cache
         | 
| 6 | 
            +
             | 
| 7 | 
            +
                @file_extension = "yml"
         | 
| 8 | 
            +
             | 
| 9 | 
            +
                def initialize(key, opts = {})
         | 
| 10 | 
            +
                  super
         | 
| 11 | 
            +
                  filename = "#{key}_cache.yml"
         | 
| 12 | 
            +
                  @location = opts[:location] || "tmp/#{filename}"
         | 
| 13 | 
            +
                  @location = File.join(@location, "#{filename}") if File.directory?(@location)
         | 
| 14 | 
            +
                end
         | 
| 15 | 
            +
             | 
| 16 | 
            +
                def read
         | 
| 17 | 
            +
                  YAML.load @location
         | 
| 18 | 
            +
                end
         | 
| 19 | 
            +
             | 
| 20 | 
            +
                def write(data)
         | 
| 21 | 
            +
                  File.open(@location, 'w') { |f| f.write data.to_yaml }
         | 
| 22 | 
            +
                end
         | 
| 23 | 
            +
             | 
| 24 | 
            +
                def self.file_extension
         | 
| 25 | 
            +
                  "yml"
         | 
| 26 | 
            +
                end
         | 
| 27 | 
            +
              end
         | 
| 28 | 
            +
            end
         | 
| 29 | 
            +
             | 
| @@ -0,0 +1,21 @@ | |
| 1 | 
            +
            require "with_timed_cache/version"
         | 
| 2 | 
            +
            require "with_timed_cache/caches"
         | 
| 3 | 
            +
             | 
| 4 | 
            +
            module WithTimedCache
         | 
| 5 | 
            +
              def with_timed_cache(key, opts)
         | 
| 6 | 
            +
                cache = Caches.find_or_create(key, opts)
         | 
| 7 | 
            +
             | 
| 8 | 
            +
                if cache.exists? && !cache.stale?
         | 
| 9 | 
            +
                  data = cache.read
         | 
| 10 | 
            +
                else
         | 
| 11 | 
            +
                  begin
         | 
| 12 | 
            +
                    data = yield
         | 
| 13 | 
            +
                    cache.write(data)
         | 
| 14 | 
            +
                  rescue Exception => e
         | 
| 15 | 
            +
                    data = cache.read rescue nil
         | 
| 16 | 
            +
                  end
         | 
| 17 | 
            +
                end
         | 
| 18 | 
            +
                data
         | 
| 19 | 
            +
              end
         | 
| 20 | 
            +
            end
         | 
| 21 | 
            +
             | 
| @@ -0,0 +1,17 @@ | |
| 1 | 
            +
            require 'spec_helper'
         | 
| 2 | 
            +
             | 
| 3 | 
            +
            describe WithTimedCache::Cache do
         | 
| 4 | 
            +
              it "knows it's local class name" do
         | 
| 5 | 
            +
                WithTimedCache::Cache.local_class_name.should == "Cache"
         | 
| 6 | 
            +
              end
         | 
| 7 | 
            +
             | 
| 8 | 
            +
              it "has an empty file extension" do
         | 
| 9 | 
            +
                WithTimedCache::Cache.file_extension.should be_empty
         | 
| 10 | 
            +
              end
         | 
| 11 | 
            +
             | 
| 12 | 
            +
              it "should have a filename with no extension by default" do
         | 
| 13 | 
            +
                cache = WithTimedCache::Cache.new(:foo)
         | 
| 14 | 
            +
                (cache.filename =~ /^.+\..+$/).should be_false
         | 
| 15 | 
            +
              end
         | 
| 16 | 
            +
            end
         | 
| 17 | 
            +
             | 
| @@ -0,0 +1,29 @@ | |
| 1 | 
            +
            require 'spec_helper'
         | 
| 2 | 
            +
             | 
| 3 | 
            +
            describe WithTimedCache::Caches do
         | 
| 4 | 
            +
              describe ".find_or_create" do
         | 
| 5 | 
            +
                it "will not create a cache twice, but will retrieve a cache by it's key if it exists" do
         | 
| 6 | 
            +
                  key = :find_or_create
         | 
| 7 | 
            +
                  first_ref = WithTimedCache::Caches.find_or_create(key)
         | 
| 8 | 
            +
                  second_ref = WithTimedCache::Caches.find_or_create(key)
         | 
| 9 | 
            +
                  first_ref.eql?(second_ref).should be_true
         | 
| 10 | 
            +
                end
         | 
| 11 | 
            +
              end
         | 
| 12 | 
            +
             | 
| 13 | 
            +
              describe ".cache_class" do
         | 
| 14 | 
            +
                it "will return the constant of the cache class for the given format" do
         | 
| 15 | 
            +
                  WithTimedCache::Caches.cache_class(:json).should == WithTimedCache::JSONCache
         | 
| 16 | 
            +
                  WithTimedCache::Caches.cache_class("json").should == WithTimedCache::JSONCache
         | 
| 17 | 
            +
                  WithTimedCache::Caches.cache_class(:yaml).should == WithTimedCache::YAMLCache
         | 
| 18 | 
            +
                  WithTimedCache::Caches.cache_class("").should == WithTimedCache::Cache
         | 
| 19 | 
            +
                  WithTimedCache::Caches.cache_class.should == WithTimedCache::Cache
         | 
| 20 | 
            +
                end
         | 
| 21 | 
            +
             | 
| 22 | 
            +
                it "will return the constant of the cache class for the given format" do
         | 
| 23 | 
            +
                  expect {
         | 
| 24 | 
            +
                    WithTimedCache::Caches.cache_class(:foobar).should == WithTimedCache::JSONCache
         | 
| 25 | 
            +
                  }.to raise_error WithTimedCache::InvalidCacheFormatException
         | 
| 26 | 
            +
                end
         | 
| 27 | 
            +
              end
         | 
| 28 | 
            +
            end
         | 
| 29 | 
            +
             | 
| @@ -0,0 +1,17 @@ | |
| 1 | 
            +
            require 'spec_helper'
         | 
| 2 | 
            +
             | 
| 3 | 
            +
            describe WithTimedCache::JSONCache do
         | 
| 4 | 
            +
              it "knows it's local class name" do
         | 
| 5 | 
            +
                WithTimedCache::JSONCache.local_class_name.should == "JSONCache"
         | 
| 6 | 
            +
              end
         | 
| 7 | 
            +
             | 
| 8 | 
            +
              it "has a file_extension" do
         | 
| 9 | 
            +
                WithTimedCache::JSONCache.file_extension.should == "json"
         | 
| 10 | 
            +
              end
         | 
| 11 | 
            +
             | 
| 12 | 
            +
              it "should have a filename with the '.json' extension" do
         | 
| 13 | 
            +
                cache = WithTimedCache::JSONCache.new(:foo)
         | 
| 14 | 
            +
                (cache.filename =~ /^.+\.json$/).should be_true
         | 
| 15 | 
            +
              end
         | 
| 16 | 
            +
            end
         | 
| 17 | 
            +
             | 
| @@ -0,0 +1,17 @@ | |
| 1 | 
            +
            require 'spec_helper'
         | 
| 2 | 
            +
             | 
| 3 | 
            +
            describe WithTimedCache::YAMLCache do
         | 
| 4 | 
            +
              it "knows it's local class name" do
         | 
| 5 | 
            +
                WithTimedCache::YAMLCache.local_class_name.should == "YAMLCache"
         | 
| 6 | 
            +
              end
         | 
| 7 | 
            +
             | 
| 8 | 
            +
              it "has a file_extension" do
         | 
| 9 | 
            +
                WithTimedCache::YAMLCache.file_extension.should == "yml"
         | 
| 10 | 
            +
              end
         | 
| 11 | 
            +
             | 
| 12 | 
            +
              it "should have a filename with the '.yml' extension" do
         | 
| 13 | 
            +
                cache = WithTimedCache::YAMLCache.new(:foo)
         | 
| 14 | 
            +
                (cache.filename =~ /^.+\.yml$/).should be_true
         | 
| 15 | 
            +
              end
         | 
| 16 | 
            +
            end
         | 
| 17 | 
            +
             | 
| @@ -0,0 +1,110 @@ | |
| 1 | 
            +
            require 'spec_helper'
         | 
| 2 | 
            +
             | 
| 3 | 
            +
            describe WithTimedCache do
         | 
| 4 | 
            +
              include_context :with_timed_cache
         | 
| 5 | 
            +
             | 
| 6 | 
            +
              before(:all) do
         | 
| 7 | 
            +
                @cache_directory = "spec/data/tmp"
         | 
| 8 | 
            +
                clean_cache_directory
         | 
| 9 | 
            +
              end
         | 
| 10 | 
            +
             | 
| 11 | 
            +
              after(:all) do
         | 
| 12 | 
            +
                clean_cache_directory
         | 
| 13 | 
            +
              end
         | 
| 14 | 
            +
             | 
| 15 | 
            +
              it "caches data for a defined max_age" do
         | 
| 16 | 
            +
                original_val = "original value"
         | 
| 17 | 
            +
                updated_val = "updated value"
         | 
| 18 | 
            +
                2.times do |i|
         | 
| 19 | 
            +
                  data = with_timed_cache(:max_age_1, location: @cache_directory, max_age: 1.minute) do
         | 
| 20 | 
            +
                    i == 0 ? original_val : updated_val
         | 
| 21 | 
            +
                  end
         | 
| 22 | 
            +
                  data.should == original_val
         | 
| 23 | 
            +
                end
         | 
| 24 | 
            +
              end
         | 
| 25 | 
            +
             | 
| 26 | 
            +
              it "updates the cache after a defined max_age" do
         | 
| 27 | 
            +
                original_val = "original value"
         | 
| 28 | 
            +
                updated_val = "updated value"
         | 
| 29 | 
            +
                data = nil
         | 
| 30 | 
            +
                2.times do |i|
         | 
| 31 | 
            +
                  data = with_timed_cache(:max_age_2, location: @cache_directory, max_age: 1.second) do
         | 
| 32 | 
            +
                    i == 0 ? original_val : updated_val
         | 
| 33 | 
            +
                  end
         | 
| 34 | 
            +
                  if i == 0
         | 
| 35 | 
            +
                    data.should == original_val
         | 
| 36 | 
            +
                    sleep 1.5 # sleep longer than max_age
         | 
| 37 | 
            +
                  end
         | 
| 38 | 
            +
                end
         | 
| 39 | 
            +
                data.should == updated_val
         | 
| 40 | 
            +
              end
         | 
| 41 | 
            +
             | 
| 42 | 
            +
              it "caches marshaled data to a file" do
         | 
| 43 | 
            +
                key = :foo
         | 
| 44 | 
            +
                filename = "#{key}_cache"
         | 
| 45 | 
            +
                data = [1, 2, 3]
         | 
| 46 | 
            +
             | 
| 47 | 
            +
                cached_data = with_timed_cache(key, location: @cache_directory) { data }
         | 
| 48 | 
            +
                cached_data.should == data
         | 
| 49 | 
            +
                File.exists?(File.join(@cache_directory, filename)).should be_true
         | 
| 50 | 
            +
             | 
| 51 | 
            +
                raw_cache_data = File.read(File.join(@cache_directory, filename))
         | 
| 52 | 
            +
                Marshal.load(raw_cache_data).should == data
         | 
| 53 | 
            +
              end
         | 
| 54 | 
            +
             | 
| 55 | 
            +
              it "lets you store cache data as json" do
         | 
| 56 | 
            +
                key = :json
         | 
| 57 | 
            +
                filename = "#{key}_cache.json"
         | 
| 58 | 
            +
                filepath = File.join(@cache_directory, filename)
         | 
| 59 | 
            +
                data = { foo: 'bar', baz: 'qux' }
         | 
| 60 | 
            +
             | 
| 61 | 
            +
                cached_data = with_timed_cache(key, location: @cache_directory, format: :json) { data }
         | 
| 62 | 
            +
                cached_data.should == data
         | 
| 63 | 
            +
                File.exists?(filepath).should be_true
         | 
| 64 | 
            +
             | 
| 65 | 
            +
                raw_cache_data = File.read(filepath)
         | 
| 66 | 
            +
                JSON.parse(raw_cache_data, symbolize_names: true).should == data
         | 
| 67 | 
            +
              end
         | 
| 68 | 
            +
             | 
| 69 | 
            +
              it "lets you store cache data as yaml" do
         | 
| 70 | 
            +
                key = :yaml
         | 
| 71 | 
            +
                filename = "#{key}_cache.yml"
         | 
| 72 | 
            +
                filepath = File.join(@cache_directory, filename)
         | 
| 73 | 
            +
                data = { foo: 'bar', baz: 'qux' }
         | 
| 74 | 
            +
             | 
| 75 | 
            +
                cached_data = with_timed_cache(key, location: @cache_directory, format: :yaml) { data }
         | 
| 76 | 
            +
                cached_data.should == data
         | 
| 77 | 
            +
                File.exists?(filepath).should be_true
         | 
| 78 | 
            +
             | 
| 79 | 
            +
                raw_cache_data = File.read(filepath)
         | 
| 80 | 
            +
                YAML.load_file(filepath).should == data
         | 
| 81 | 
            +
              end
         | 
| 82 | 
            +
             | 
| 83 | 
            +
              it "returns last cached data if present when an exception is raised" do
         | 
| 84 | 
            +
                string = "lorem ipsum dolor sit amet"
         | 
| 85 | 
            +
                data = nil
         | 
| 86 | 
            +
                2.times do |i|
         | 
| 87 | 
            +
                  data = with_timed_cache(:exception, location: @cache_directory, max_age: 1.second) do
         | 
| 88 | 
            +
                    if i == 0
         | 
| 89 | 
            +
                      string
         | 
| 90 | 
            +
                    else
         | 
| 91 | 
            +
                      raise Exception
         | 
| 92 | 
            +
                    end
         | 
| 93 | 
            +
                  end
         | 
| 94 | 
            +
                  sleep 1.5 if i == 0
         | 
| 95 | 
            +
                end
         | 
| 96 | 
            +
                data.should == string
         | 
| 97 | 
            +
              end
         | 
| 98 | 
            +
             | 
| 99 | 
            +
              it "returns nil when an exception is raised if no previous cache is present" do
         | 
| 100 | 
            +
                data = with_timed_cache(:exception_nil, location: @cache_directory, max_age: 1.second) do
         | 
| 101 | 
            +
                  raise Exception
         | 
| 102 | 
            +
                end
         | 
| 103 | 
            +
                data.should be_nil
         | 
| 104 | 
            +
              end
         | 
| 105 | 
            +
             | 
| 106 | 
            +
              def clean_cache_directory
         | 
| 107 | 
            +
                FileUtils.rm_f(Dir.glob(File.join(@cache_directory, "*")))
         | 
| 108 | 
            +
              end
         | 
| 109 | 
            +
            end
         | 
| 110 | 
            +
             | 
    
        data/spec/spec_helper.rb
    ADDED
    
    
| @@ -0,0 +1,27 @@ | |
| 1 | 
            +
            # coding: utf-8
         | 
| 2 | 
            +
            lib = File.expand_path('../lib', __FILE__)
         | 
| 3 | 
            +
            $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
         | 
| 4 | 
            +
            require 'with_timed_cache/version'
         | 
| 5 | 
            +
             | 
| 6 | 
            +
            Gem::Specification.new do |spec|
         | 
| 7 | 
            +
              spec.name          = "with_timed_cache"
         | 
| 8 | 
            +
              spec.version       = WithTimedCache::VERSION
         | 
| 9 | 
            +
              spec.authors       = ["Jordan Stephens"]
         | 
| 10 | 
            +
              spec.email         = ["iam@jordanstephens.net"]
         | 
| 11 | 
            +
              spec.description   = %q{A simple time based cache}
         | 
| 12 | 
            +
              spec.summary       = %q{Cache data to a local file for a defined amount of time}
         | 
| 13 | 
            +
              spec.homepage      = ""
         | 
| 14 | 
            +
              spec.license       = "MIT"
         | 
| 15 | 
            +
             | 
| 16 | 
            +
              spec.files         = `git ls-files`.split($/)
         | 
| 17 | 
            +
              spec.executables   = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
         | 
| 18 | 
            +
              spec.test_files    = spec.files.grep(%r{^spec/})
         | 
| 19 | 
            +
              spec.require_paths = ["lib"]
         | 
| 20 | 
            +
             | 
| 21 | 
            +
              spec.add_development_dependency "bundler", "~> 1.3"
         | 
| 22 | 
            +
              spec.add_development_dependency "rake"
         | 
| 23 | 
            +
              spec.add_development_dependency "rspec"
         | 
| 24 | 
            +
              spec.add_development_dependency "guard-rspec"
         | 
| 25 | 
            +
              spec.add_dependency "activesupport"
         | 
| 26 | 
            +
              spec.add_dependency "json"
         | 
| 27 | 
            +
            end
         | 
    
        metadata
    ADDED
    
    | @@ -0,0 +1,154 @@ | |
| 1 | 
            +
            --- !ruby/object:Gem::Specification
         | 
| 2 | 
            +
            name: with_timed_cache
         | 
| 3 | 
            +
            version: !ruby/object:Gem::Version
         | 
| 4 | 
            +
              version: 0.0.1
         | 
| 5 | 
            +
            platform: ruby
         | 
| 6 | 
            +
            authors:
         | 
| 7 | 
            +
            - Jordan Stephens
         | 
| 8 | 
            +
            autorequire: 
         | 
| 9 | 
            +
            bindir: bin
         | 
| 10 | 
            +
            cert_chain: []
         | 
| 11 | 
            +
            date: 2013-08-14 00:00:00.000000000 Z
         | 
| 12 | 
            +
            dependencies:
         | 
| 13 | 
            +
            - !ruby/object:Gem::Dependency
         | 
| 14 | 
            +
              name: bundler
         | 
| 15 | 
            +
              requirement: !ruby/object:Gem::Requirement
         | 
| 16 | 
            +
                requirements:
         | 
| 17 | 
            +
                - - ~>
         | 
| 18 | 
            +
                  - !ruby/object:Gem::Version
         | 
| 19 | 
            +
                    version: '1.3'
         | 
| 20 | 
            +
              type: :development
         | 
| 21 | 
            +
              prerelease: false
         | 
| 22 | 
            +
              version_requirements: !ruby/object:Gem::Requirement
         | 
| 23 | 
            +
                requirements:
         | 
| 24 | 
            +
                - - ~>
         | 
| 25 | 
            +
                  - !ruby/object:Gem::Version
         | 
| 26 | 
            +
                    version: '1.3'
         | 
| 27 | 
            +
            - !ruby/object:Gem::Dependency
         | 
| 28 | 
            +
              name: rake
         | 
| 29 | 
            +
              requirement: !ruby/object:Gem::Requirement
         | 
| 30 | 
            +
                requirements:
         | 
| 31 | 
            +
                - - '>='
         | 
| 32 | 
            +
                  - !ruby/object:Gem::Version
         | 
| 33 | 
            +
                    version: '0'
         | 
| 34 | 
            +
              type: :development
         | 
| 35 | 
            +
              prerelease: false
         | 
| 36 | 
            +
              version_requirements: !ruby/object:Gem::Requirement
         | 
| 37 | 
            +
                requirements:
         | 
| 38 | 
            +
                - - '>='
         | 
| 39 | 
            +
                  - !ruby/object:Gem::Version
         | 
| 40 | 
            +
                    version: '0'
         | 
| 41 | 
            +
            - !ruby/object:Gem::Dependency
         | 
| 42 | 
            +
              name: rspec
         | 
| 43 | 
            +
              requirement: !ruby/object:Gem::Requirement
         | 
| 44 | 
            +
                requirements:
         | 
| 45 | 
            +
                - - '>='
         | 
| 46 | 
            +
                  - !ruby/object:Gem::Version
         | 
| 47 | 
            +
                    version: '0'
         | 
| 48 | 
            +
              type: :development
         | 
| 49 | 
            +
              prerelease: false
         | 
| 50 | 
            +
              version_requirements: !ruby/object:Gem::Requirement
         | 
| 51 | 
            +
                requirements:
         | 
| 52 | 
            +
                - - '>='
         | 
| 53 | 
            +
                  - !ruby/object:Gem::Version
         | 
| 54 | 
            +
                    version: '0'
         | 
| 55 | 
            +
            - !ruby/object:Gem::Dependency
         | 
| 56 | 
            +
              name: guard-rspec
         | 
| 57 | 
            +
              requirement: !ruby/object:Gem::Requirement
         | 
| 58 | 
            +
                requirements:
         | 
| 59 | 
            +
                - - '>='
         | 
| 60 | 
            +
                  - !ruby/object:Gem::Version
         | 
| 61 | 
            +
                    version: '0'
         | 
| 62 | 
            +
              type: :development
         | 
| 63 | 
            +
              prerelease: false
         | 
| 64 | 
            +
              version_requirements: !ruby/object:Gem::Requirement
         | 
| 65 | 
            +
                requirements:
         | 
| 66 | 
            +
                - - '>='
         | 
| 67 | 
            +
                  - !ruby/object:Gem::Version
         | 
| 68 | 
            +
                    version: '0'
         | 
| 69 | 
            +
            - !ruby/object:Gem::Dependency
         | 
| 70 | 
            +
              name: activesupport
         | 
| 71 | 
            +
              requirement: !ruby/object:Gem::Requirement
         | 
| 72 | 
            +
                requirements:
         | 
| 73 | 
            +
                - - '>='
         | 
| 74 | 
            +
                  - !ruby/object:Gem::Version
         | 
| 75 | 
            +
                    version: '0'
         | 
| 76 | 
            +
              type: :runtime
         | 
| 77 | 
            +
              prerelease: false
         | 
| 78 | 
            +
              version_requirements: !ruby/object:Gem::Requirement
         | 
| 79 | 
            +
                requirements:
         | 
| 80 | 
            +
                - - '>='
         | 
| 81 | 
            +
                  - !ruby/object:Gem::Version
         | 
| 82 | 
            +
                    version: '0'
         | 
| 83 | 
            +
            - !ruby/object:Gem::Dependency
         | 
| 84 | 
            +
              name: json
         | 
| 85 | 
            +
              requirement: !ruby/object:Gem::Requirement
         | 
| 86 | 
            +
                requirements:
         | 
| 87 | 
            +
                - - '>='
         | 
| 88 | 
            +
                  - !ruby/object:Gem::Version
         | 
| 89 | 
            +
                    version: '0'
         | 
| 90 | 
            +
              type: :runtime
         | 
| 91 | 
            +
              prerelease: false
         | 
| 92 | 
            +
              version_requirements: !ruby/object:Gem::Requirement
         | 
| 93 | 
            +
                requirements:
         | 
| 94 | 
            +
                - - '>='
         | 
| 95 | 
            +
                  - !ruby/object:Gem::Version
         | 
| 96 | 
            +
                    version: '0'
         | 
| 97 | 
            +
            description: A simple time based cache
         | 
| 98 | 
            +
            email:
         | 
| 99 | 
            +
            - iam@jordanstephens.net
         | 
| 100 | 
            +
            executables: []
         | 
| 101 | 
            +
            extensions: []
         | 
| 102 | 
            +
            extra_rdoc_files: []
         | 
| 103 | 
            +
            files:
         | 
| 104 | 
            +
            - .gitignore
         | 
| 105 | 
            +
            - Gemfile
         | 
| 106 | 
            +
            - Guardfile
         | 
| 107 | 
            +
            - LICENSE.txt
         | 
| 108 | 
            +
            - README.md
         | 
| 109 | 
            +
            - Rakefile
         | 
| 110 | 
            +
            - lib/with_timed_cache.rb
         | 
| 111 | 
            +
            - lib/with_timed_cache/cache.rb
         | 
| 112 | 
            +
            - lib/with_timed_cache/caches.rb
         | 
| 113 | 
            +
            - lib/with_timed_cache/json_cache.rb
         | 
| 114 | 
            +
            - lib/with_timed_cache/version.rb
         | 
| 115 | 
            +
            - lib/with_timed_cache/yaml_cache.rb
         | 
| 116 | 
            +
            - spec/lib/with_timed_cache/cache_spec.rb
         | 
| 117 | 
            +
            - spec/lib/with_timed_cache/caches_spec.rb
         | 
| 118 | 
            +
            - spec/lib/with_timed_cache/json_cache_spec.rb
         | 
| 119 | 
            +
            - spec/lib/with_timed_cache/yaml_cache_spec.rb
         | 
| 120 | 
            +
            - spec/lib/with_timed_cache_spec.rb
         | 
| 121 | 
            +
            - spec/spec_helper.rb
         | 
| 122 | 
            +
            - with_timed_cache.gemspec
         | 
| 123 | 
            +
            homepage: ''
         | 
| 124 | 
            +
            licenses:
         | 
| 125 | 
            +
            - MIT
         | 
| 126 | 
            +
            metadata: {}
         | 
| 127 | 
            +
            post_install_message: 
         | 
| 128 | 
            +
            rdoc_options: []
         | 
| 129 | 
            +
            require_paths:
         | 
| 130 | 
            +
            - lib
         | 
| 131 | 
            +
            required_ruby_version: !ruby/object:Gem::Requirement
         | 
| 132 | 
            +
              requirements:
         | 
| 133 | 
            +
              - - '>='
         | 
| 134 | 
            +
                - !ruby/object:Gem::Version
         | 
| 135 | 
            +
                  version: '0'
         | 
| 136 | 
            +
            required_rubygems_version: !ruby/object:Gem::Requirement
         | 
| 137 | 
            +
              requirements:
         | 
| 138 | 
            +
              - - '>='
         | 
| 139 | 
            +
                - !ruby/object:Gem::Version
         | 
| 140 | 
            +
                  version: '0'
         | 
| 141 | 
            +
            requirements: []
         | 
| 142 | 
            +
            rubyforge_project: 
         | 
| 143 | 
            +
            rubygems_version: 2.0.0
         | 
| 144 | 
            +
            signing_key: 
         | 
| 145 | 
            +
            specification_version: 4
         | 
| 146 | 
            +
            summary: Cache data to a local file for a defined amount of time
         | 
| 147 | 
            +
            test_files:
         | 
| 148 | 
            +
            - spec/lib/with_timed_cache/cache_spec.rb
         | 
| 149 | 
            +
            - spec/lib/with_timed_cache/caches_spec.rb
         | 
| 150 | 
            +
            - spec/lib/with_timed_cache/json_cache_spec.rb
         | 
| 151 | 
            +
            - spec/lib/with_timed_cache/yaml_cache_spec.rb
         | 
| 152 | 
            +
            - spec/lib/with_timed_cache_spec.rb
         | 
| 153 | 
            +
            - spec/spec_helper.rb
         | 
| 154 | 
            +
            has_rdoc: 
         |