cache_key_monster 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- data/.gitignore +17 -0
- data/.rspec +2 -0
- data/Gemfile +4 -0
- data/LICENSE.txt +22 -0
- data/README.md +29 -0
- data/Rakefile +1 -0
- data/cache_key_monster.gemspec +21 -0
- data/cache_key_monster.jpg +0 -0
- data/lib/cache_key_monster/cache_key.rb +41 -0
- data/lib/cache_key_monster/strategy.rb +22 -0
- data/lib/cache_key_monster/version.rb +3 -0
- data/lib/cache_key_monster.rb +6 -0
- data/spec/lib/cache_key_monster/cache_key_spec.rb +50 -0
- data/spec/lib/cache_key_monster_spec.rb +7 -0
- data/spec/spec_helper.rb +20 -0
- data/spec/support/batting_leaders.rb +15 -0
- data/spec/support/live_event.rb +18 -0
- data/spec/support/no_key_provided.rb +9 -0
- data/spec/support/player.rb +10 -0
- metadata +91 -0
data/.gitignore
ADDED
data/.rspec
ADDED
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2013 Doug Rohde
|
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,29 @@
|
|
1
|
+
# CacheKeyMonster
|
2
|
+
|
3
|
+
CacheKeyMonster provides a lightweight, declarative-style syntax for adding the cache_key method which is used by Rails' "cache" method. It allows you to cache Plain Old Ruby objects based on their constituent data. This is useful for objects that are transient by their very nature.
|
4
|
+
|
5
|
+
## Installation
|
6
|
+
|
7
|
+
Add this line to your application's Gemfile:
|
8
|
+
|
9
|
+
gem 'cache_key_monster'
|
10
|
+
|
11
|
+
And then execute:
|
12
|
+
|
13
|
+
$ bundle
|
14
|
+
|
15
|
+
Or install it yourself as:
|
16
|
+
|
17
|
+
$ gem install cache_key_monster
|
18
|
+
|
19
|
+
## Usage
|
20
|
+
|
21
|
+
TODO: Write usage instructions here
|
22
|
+
|
23
|
+
## Contributing
|
24
|
+
|
25
|
+
1. Fork it
|
26
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
27
|
+
3. Commit your changes (`git commit -am 'Add some feature'`)
|
28
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
29
|
+
5. Create new Pull Request
|
data/Rakefile
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
require "bundler/gem_tasks"
|
@@ -0,0 +1,21 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'cache_key_monster/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |gem|
|
7
|
+
gem.name = "cache_key_monster"
|
8
|
+
gem.version = CacheKeyMonster::VERSION
|
9
|
+
gem.authors = ["Doug Rohde"]
|
10
|
+
gem.email = ["doug.rohde@tstmedia.com"]
|
11
|
+
gem.description = %q{A simple way to provide cache_key method to Plain Old Ruby objects}
|
12
|
+
gem.summary = %q{A simple way to provide cache_key method to Plain Old Ruby objects}
|
13
|
+
gem.homepage = ""
|
14
|
+
|
15
|
+
gem.files = `git ls-files`.split($/)
|
16
|
+
gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
|
17
|
+
gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
|
18
|
+
gem.require_paths = ["lib"]
|
19
|
+
|
20
|
+
gem.add_development_dependency "rspec"
|
21
|
+
end
|
Binary file
|
@@ -0,0 +1,41 @@
|
|
1
|
+
require "cache_key_monster/strategy"
|
2
|
+
|
3
|
+
module CacheKeyMonster
|
4
|
+
module CacheKey
|
5
|
+
|
6
|
+
module ClassMethods
|
7
|
+
attr_reader :cache_key_monster_strategies
|
8
|
+
|
9
|
+
def cache_key(options={})
|
10
|
+
@cache_key_monster_strategies ||= []
|
11
|
+
strategy = Strategy.new(options[:key], options[:duration], options[:condition])
|
12
|
+
@cache_key_monster_strategies << strategy
|
13
|
+
end
|
14
|
+
end
|
15
|
+
|
16
|
+
def self.included(base)
|
17
|
+
base.extend(ClassMethods)
|
18
|
+
end
|
19
|
+
|
20
|
+
def cache_key
|
21
|
+
self.class.cache_key_monster_strategies.each do |strategy|
|
22
|
+
if strategy.condition_applies?(self)
|
23
|
+
return "#{prefix}-#{cache_key_body(strategy)}"
|
24
|
+
end
|
25
|
+
end
|
26
|
+
"#{prefix}-#{default_cache_key_monster_key}"
|
27
|
+
end
|
28
|
+
|
29
|
+
def cache_key_body(strategy)
|
30
|
+
strategy.execute_key_definition(self)
|
31
|
+
end
|
32
|
+
|
33
|
+
def prefix
|
34
|
+
self.class.name.downcase
|
35
|
+
end
|
36
|
+
|
37
|
+
def default_cache_key_monster_key
|
38
|
+
self.hash
|
39
|
+
end
|
40
|
+
end
|
41
|
+
end
|
@@ -0,0 +1,22 @@
|
|
1
|
+
module CacheKeyMonster
|
2
|
+
class Strategy
|
3
|
+
|
4
|
+
attr_reader :key_definition, :condition
|
5
|
+
|
6
|
+
def initialize(key_definition, duration, condition)
|
7
|
+
@key_definition = key_definition
|
8
|
+
@duration = duration || 0
|
9
|
+
@condition = condition || Proc.new{|obj| true}
|
10
|
+
end
|
11
|
+
|
12
|
+
def condition_applies?(obj)
|
13
|
+
return true if condition.nil?
|
14
|
+
condition.call(obj)
|
15
|
+
end
|
16
|
+
|
17
|
+
def execute_key_definition(obj)
|
18
|
+
key_definition ? key_definition.call(obj) : obj.default_cache_key_monster_key
|
19
|
+
end
|
20
|
+
|
21
|
+
end
|
22
|
+
end
|
@@ -0,0 +1,50 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe CacheKeyMonster::CacheKey do
|
4
|
+
|
5
|
+
context "a list of batting leaders" do
|
6
|
+
|
7
|
+
before(:each) do
|
8
|
+
@players = Player.new(14, "Andres", 0.371), Player.new(19, "Tony", 0.358), Player.new(25, "Gregg", 0.342)
|
9
|
+
@leaders = BattingLeaders.new(@players)
|
10
|
+
end
|
11
|
+
|
12
|
+
it "should respond to cache_key method" do
|
13
|
+
@leaders.should respond_to(:cache_key)
|
14
|
+
end
|
15
|
+
|
16
|
+
it "should create a cache key from constituent data" do
|
17
|
+
@leaders.cache_key.should eq("battingleaders-1419250.3710.3580.342")
|
18
|
+
end
|
19
|
+
|
20
|
+
it "should return the hash of the object if no definition provided" do
|
21
|
+
no_key = NoKeyProvided.new
|
22
|
+
no_key.cache_key.should eq("nokeyprovided-#{no_key.hash.to_s}")
|
23
|
+
end
|
24
|
+
end
|
25
|
+
|
26
|
+
context "a live event" do
|
27
|
+
|
28
|
+
before(:each) do
|
29
|
+
@event = LiveEvent.new(id: 99, title: "Live Event")
|
30
|
+
end
|
31
|
+
|
32
|
+
it "should return pending status key" do
|
33
|
+
@event.status = "pending"
|
34
|
+
@event.cache_key.should eq("liveevent-Live Event")
|
35
|
+
end
|
36
|
+
|
37
|
+
it "should return completed status key" do
|
38
|
+
@event.status = "completed"
|
39
|
+
@event.cache_key.should eq("liveevent-99")
|
40
|
+
end
|
41
|
+
|
42
|
+
it "should return default key if status does not match" do
|
43
|
+
@event.status = "cancelled"
|
44
|
+
obj_hash = @event.hash
|
45
|
+
@event.cache_key.should eq("liveevent-#{obj_hash}")
|
46
|
+
end
|
47
|
+
end
|
48
|
+
|
49
|
+
end
|
50
|
+
|
data/spec/spec_helper.rb
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
# This file was generated by the `rspec --init` command. Conventionally, all
|
2
|
+
# specs live under a `spec` directory, which RSpec adds to the `$LOAD_PATH`.
|
3
|
+
# Require this file using `require "spec_helper"` to ensure that it is only
|
4
|
+
# loaded once.
|
5
|
+
#
|
6
|
+
# See http://rubydoc.info/gems/rspec-core/RSpec/Core/Configuration
|
7
|
+
Dir["spec/support/**/*.rb"].each {|f| require "./#{f}"}
|
8
|
+
|
9
|
+
|
10
|
+
RSpec.configure do |config|
|
11
|
+
config.treat_symbols_as_metadata_keys_with_true_values = true
|
12
|
+
config.run_all_when_everything_filtered = true
|
13
|
+
config.filter_run :focus
|
14
|
+
|
15
|
+
# Run specs in random order to surface order dependencies. If you find an
|
16
|
+
# order dependency and want to debug it, you can fix the order by providing
|
17
|
+
# the seed, which is printed after each run.
|
18
|
+
# --seed 1234
|
19
|
+
config.order = 'random'
|
20
|
+
end
|
@@ -0,0 +1,15 @@
|
|
1
|
+
require 'cache_key_monster'
|
2
|
+
|
3
|
+
class BattingLeaders
|
4
|
+
|
5
|
+
include CacheKeyMonster::CacheKey
|
6
|
+
|
7
|
+
cache_key key: ->(obj){obj.leaders.map(&:id).join + obj.leaders.map(&:avg).join}
|
8
|
+
|
9
|
+
attr_accessor :leaders
|
10
|
+
|
11
|
+
def initialize(leaders)
|
12
|
+
@leaders = leaders
|
13
|
+
end
|
14
|
+
|
15
|
+
end
|
@@ -0,0 +1,18 @@
|
|
1
|
+
require 'cache_key_monster'
|
2
|
+
|
3
|
+
class LiveEvent
|
4
|
+
|
5
|
+
include CacheKeyMonster::CacheKey
|
6
|
+
|
7
|
+
cache_key key: ->(obj){obj.title}, condition: ->(obj){obj.status == "pending"}
|
8
|
+
cache_key key: ->(obj){obj.id}, condition: ->(obj){obj.status == "completed"}
|
9
|
+
|
10
|
+
attr_accessor :status
|
11
|
+
attr_reader :id, :title
|
12
|
+
|
13
|
+
def initialize(params={})
|
14
|
+
@id = params[:id]
|
15
|
+
@title = params[:title]
|
16
|
+
end
|
17
|
+
|
18
|
+
end
|
metadata
ADDED
@@ -0,0 +1,91 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: cache_key_monster
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
prerelease:
|
5
|
+
version: 0.0.1
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- Doug Rohde
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
|
13
|
+
date: 2013-07-08 00:00:00 -05:00
|
14
|
+
default_executable:
|
15
|
+
dependencies:
|
16
|
+
- !ruby/object:Gem::Dependency
|
17
|
+
name: rspec
|
18
|
+
prerelease: false
|
19
|
+
requirement: &id001 !ruby/object:Gem::Requirement
|
20
|
+
none: false
|
21
|
+
requirements:
|
22
|
+
- - ">="
|
23
|
+
- !ruby/object:Gem::Version
|
24
|
+
version: "0"
|
25
|
+
type: :development
|
26
|
+
version_requirements: *id001
|
27
|
+
description: A simple way to provide cache_key method to Plain Old Ruby objects
|
28
|
+
email:
|
29
|
+
- doug.rohde@tstmedia.com
|
30
|
+
executables: []
|
31
|
+
|
32
|
+
extensions: []
|
33
|
+
|
34
|
+
extra_rdoc_files: []
|
35
|
+
|
36
|
+
files:
|
37
|
+
- .gitignore
|
38
|
+
- .rspec
|
39
|
+
- Gemfile
|
40
|
+
- LICENSE.txt
|
41
|
+
- README.md
|
42
|
+
- Rakefile
|
43
|
+
- cache_key_monster.gemspec
|
44
|
+
- cache_key_monster.jpg
|
45
|
+
- lib/cache_key_monster.rb
|
46
|
+
- lib/cache_key_monster/cache_key.rb
|
47
|
+
- lib/cache_key_monster/strategy.rb
|
48
|
+
- lib/cache_key_monster/version.rb
|
49
|
+
- spec/lib/cache_key_monster/cache_key_spec.rb
|
50
|
+
- spec/lib/cache_key_monster_spec.rb
|
51
|
+
- spec/spec_helper.rb
|
52
|
+
- spec/support/batting_leaders.rb
|
53
|
+
- spec/support/live_event.rb
|
54
|
+
- spec/support/no_key_provided.rb
|
55
|
+
- spec/support/player.rb
|
56
|
+
has_rdoc: true
|
57
|
+
homepage: ""
|
58
|
+
licenses: []
|
59
|
+
|
60
|
+
post_install_message:
|
61
|
+
rdoc_options: []
|
62
|
+
|
63
|
+
require_paths:
|
64
|
+
- lib
|
65
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
66
|
+
none: false
|
67
|
+
requirements:
|
68
|
+
- - ">="
|
69
|
+
- !ruby/object:Gem::Version
|
70
|
+
version: "0"
|
71
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
72
|
+
none: false
|
73
|
+
requirements:
|
74
|
+
- - ">="
|
75
|
+
- !ruby/object:Gem::Version
|
76
|
+
version: "0"
|
77
|
+
requirements: []
|
78
|
+
|
79
|
+
rubyforge_project:
|
80
|
+
rubygems_version: 1.5.0
|
81
|
+
signing_key:
|
82
|
+
specification_version: 3
|
83
|
+
summary: A simple way to provide cache_key method to Plain Old Ruby objects
|
84
|
+
test_files:
|
85
|
+
- spec/lib/cache_key_monster/cache_key_spec.rb
|
86
|
+
- spec/lib/cache_key_monster_spec.rb
|
87
|
+
- spec/spec_helper.rb
|
88
|
+
- spec/support/batting_leaders.rb
|
89
|
+
- spec/support/live_event.rb
|
90
|
+
- spec/support/no_key_provided.rb
|
91
|
+
- spec/support/player.rb
|