devfu-hash-cache 0.1.0
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/README.rdoc +65 -0
- data/Rakefile +68 -0
- data/VERSION +1 -0
- data/lib/hash-cache.rb +80 -0
- data/lib/hash/cache.rb +2 -0
- data/spec/hash-cache_spec.rb +30 -0
- data/spec/spec.opts +4 -0
- metadata +60 -0
data/README.rdoc
ADDED
|
@@ -0,0 +1,65 @@
|
|
|
1
|
+
= Hash Cache
|
|
2
|
+
|
|
3
|
+
Hashes make good in-memory caches. We all do it!
|
|
4
|
+
|
|
5
|
+
I often create projects that allow some kind of caching repository to exist.
|
|
6
|
+
Usually, I use an instance of a Hash during development, but I might actually
|
|
7
|
+
want to swap it out with memcached or another cache store, in production.
|
|
8
|
+
|
|
9
|
+
Whenever I do this, I always have to add #get and #set methods to Hash, as these
|
|
10
|
+
methods are pretty conventional for cache stores.
|
|
11
|
+
|
|
12
|
+
Well ... I'm sick of it. Introducting a mini RubyGem that does this for you.
|
|
13
|
+
|
|
14
|
+
== Installation
|
|
15
|
+
|
|
16
|
+
sudo gem install devfu-hash-cache --source http://gems.github.com
|
|
17
|
+
|
|
18
|
+
== Usage
|
|
19
|
+
|
|
20
|
+
>> cache = { }
|
|
21
|
+
=> {}
|
|
22
|
+
|
|
23
|
+
>> cache.set 'chunky', 'bacon'
|
|
24
|
+
NoMethodError: undefined method `set' for {}:Hash
|
|
25
|
+
from (irb):2
|
|
26
|
+
|
|
27
|
+
>> require 'hash/cache'
|
|
28
|
+
=> true
|
|
29
|
+
|
|
30
|
+
>> cache.set 'chunky', 'bacon'
|
|
31
|
+
=> "bacon"
|
|
32
|
+
|
|
33
|
+
>> cache
|
|
34
|
+
=> {"chunky"=>"bacon"}
|
|
35
|
+
|
|
36
|
+
Ta Da!
|
|
37
|
+
|
|
38
|
+
If you won't want to apply these methods to all Hashes, just a particular instance of a Hash:
|
|
39
|
+
|
|
40
|
+
>> require 'hash-cache'
|
|
41
|
+
=> true
|
|
42
|
+
|
|
43
|
+
>> cache = { }
|
|
44
|
+
=> {}
|
|
45
|
+
|
|
46
|
+
>> cache.set 'chunky', 'bacon'
|
|
47
|
+
NoMethodError: undefined method `set' for {}:Hash
|
|
48
|
+
from (irb):2
|
|
49
|
+
|
|
50
|
+
>> cache = Hash::Cache.new(cache)
|
|
51
|
+
=> {}
|
|
52
|
+
|
|
53
|
+
>> cache.set 'chunky', 'bacon'
|
|
54
|
+
=> "bacon"
|
|
55
|
+
|
|
56
|
+
>> cache
|
|
57
|
+
=> {"chunky"=>"bacon"}
|
|
58
|
+
|
|
59
|
+
>> Hash.new.get 'foo'
|
|
60
|
+
NoMethodError: undefined method `get' for {}:Hash
|
|
61
|
+
from (irb):11
|
|
62
|
+
|
|
63
|
+
For more information, check out:
|
|
64
|
+
RDoc:: http://code.devfu.com/hash-cache
|
|
65
|
+
Specs:: http://github.com/devfu/hash-cache/blob/master/spec/hash-cache_spec.rb
|
data/Rakefile
ADDED
|
@@ -0,0 +1,68 @@
|
|
|
1
|
+
require 'rake'
|
|
2
|
+
require 'rubygems'
|
|
3
|
+
require 'rake/rdoctask'
|
|
4
|
+
require 'spec/rake/spectask'
|
|
5
|
+
|
|
6
|
+
puts "\nGem: hash-cache\n\n"
|
|
7
|
+
|
|
8
|
+
begin
|
|
9
|
+
require 'jeweler'
|
|
10
|
+
Jeweler::Tasks.new do |s|
|
|
11
|
+
s.name = 'hash-cache'
|
|
12
|
+
s.summary = 'Adds #get and #set methods to Hash(es)'
|
|
13
|
+
s.email = 'remi@remitaylor.com'
|
|
14
|
+
s.homepage = 'http://github.com/devfu/hash-cache'
|
|
15
|
+
s.description = 'Adds #get and #set methods to Hash(es) so they can be used as conventional cache stores'
|
|
16
|
+
s.authors = %w( remi )
|
|
17
|
+
s.files = FileList['[A-Z]*', '{lib,spec,bin,examples}/**/*']
|
|
18
|
+
# s.add_dependency 'person-gemname'
|
|
19
|
+
# s.executables << 'script'
|
|
20
|
+
# s.rubyforge_project = 'gemname'
|
|
21
|
+
# s.extra_rdoc_files = %w( README.rdoc )
|
|
22
|
+
end
|
|
23
|
+
rescue LoadError
|
|
24
|
+
puts "Jeweler not available. Install it with: sudo gem install technicalpickles-jeweler -s http://gems.github.com"
|
|
25
|
+
end
|
|
26
|
+
|
|
27
|
+
Spec::Rake::SpecTask.new do |t|
|
|
28
|
+
t.spec_files = FileList['spec/**/*_spec.rb']
|
|
29
|
+
end
|
|
30
|
+
|
|
31
|
+
desc "Run all examples with RCov"
|
|
32
|
+
Spec::Rake::SpecTask.new('rcov') do |t|
|
|
33
|
+
t.spec_files = FileList['spec/**/*_spec.rb']
|
|
34
|
+
t.rcov = true
|
|
35
|
+
end
|
|
36
|
+
|
|
37
|
+
# require 'hanna'
|
|
38
|
+
# require 'darkfish-rdoc'
|
|
39
|
+
|
|
40
|
+
Rake::RDocTask.new do |rdoc|
|
|
41
|
+
rdoc.rdoc_dir = 'rdoc'
|
|
42
|
+
rdoc.title = 'hash-cache'
|
|
43
|
+
rdoc.options << '--line-numbers' << '--inline-source'
|
|
44
|
+
# rdoc.options += ["--template=#{`allison --path`}"] # sudo gem install allison
|
|
45
|
+
# rdoc.options += %w( -f darkfish ) # sudo gem install darkfish-rdoc
|
|
46
|
+
# rdoc.options += %w( -T hanna ) # sudo gem install mislav-hanna
|
|
47
|
+
rdoc.options += %w( -m README.rdoc ) # the initial page displayed
|
|
48
|
+
rdoc.rdoc_files.include('README.rdoc')
|
|
49
|
+
rdoc.rdoc_files.include('lib/**/*.rb')
|
|
50
|
+
end
|
|
51
|
+
|
|
52
|
+
desc 'Confirm that gemspec is $SAFE'
|
|
53
|
+
task :safe do
|
|
54
|
+
require 'yaml'
|
|
55
|
+
require 'rubygems/specification'
|
|
56
|
+
data = File.read('hash-cache.gemspec')
|
|
57
|
+
spec = nil
|
|
58
|
+
if data !~ %r{!ruby/object:Gem::Specification}
|
|
59
|
+
Thread.new { spec = eval("$SAFE = 3\n#{data}") }.join
|
|
60
|
+
else
|
|
61
|
+
spec = YAML.load(data)
|
|
62
|
+
end
|
|
63
|
+
spec.validate
|
|
64
|
+
puts spec
|
|
65
|
+
puts "OK"
|
|
66
|
+
end
|
|
67
|
+
|
|
68
|
+
task :default => :spec
|
data/VERSION
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
0.1.0
|
data/lib/hash-cache.rb
ADDED
|
@@ -0,0 +1,80 @@
|
|
|
1
|
+
class Hash #:nodoc:
|
|
2
|
+
|
|
3
|
+
# A module which adds #get and #set methods to an object, usually a Hash
|
|
4
|
+
#
|
|
5
|
+
# To get and set values, we call the object's indexer methods, eg.
|
|
6
|
+
#
|
|
7
|
+
# return object[ key ]
|
|
8
|
+
#
|
|
9
|
+
# object[ key ] = value
|
|
10
|
+
#
|
|
11
|
+
# Any object which has [] and []= methods can have Hash::Cache included
|
|
12
|
+
# in it to get these #get and #set method implementations.
|
|
13
|
+
module Cache
|
|
14
|
+
|
|
15
|
+
def self.included base
|
|
16
|
+
methods = base.respond_to?(:instance_methods) ? base.instance_methods : base.methods
|
|
17
|
+
raise "Hash::Cache can only be included in objects that implement the [] method" unless methods.include?('[]')
|
|
18
|
+
raise "Hash::Cache can only be included in objects that implement the []= method" unless methods.include?('[]=')
|
|
19
|
+
end
|
|
20
|
+
|
|
21
|
+
# Get a new Hash object with Hash::Cache included
|
|
22
|
+
#
|
|
23
|
+
# This allows you to easily get Hash instances with #get and #set
|
|
24
|
+
# methods without causing all instances of Hash to get these methods.
|
|
25
|
+
#
|
|
26
|
+
# >> require 'hash-cache'
|
|
27
|
+
# => true
|
|
28
|
+
#
|
|
29
|
+
# >> cache = Hash::Cache.new
|
|
30
|
+
# => {}
|
|
31
|
+
#
|
|
32
|
+
# >> cache.set 'chunky', 'bacon'
|
|
33
|
+
# => "bacon"
|
|
34
|
+
#
|
|
35
|
+
# >> cache.get 'chunky'
|
|
36
|
+
# => "bacon"
|
|
37
|
+
#
|
|
38
|
+
# >> cache['chunky']
|
|
39
|
+
# => "bacon"
|
|
40
|
+
#
|
|
41
|
+
# >> cache
|
|
42
|
+
# => {"chunky"=>"bacon"}
|
|
43
|
+
#
|
|
44
|
+
# This accepts an optional Hash instance as an argument which
|
|
45
|
+
# will include Hash::Cache in just that particular instance)
|
|
46
|
+
#
|
|
47
|
+
# >> hash = { :chunky => 'bacon' }
|
|
48
|
+
# => {:chunky=>"bacon"}
|
|
49
|
+
#
|
|
50
|
+
# >> hash.get :chunky
|
|
51
|
+
# NoMethodError: undefined method `get' for {:chunky=>"bacon"}:Hash
|
|
52
|
+
# from (irb):7
|
|
53
|
+
#
|
|
54
|
+
# >> Hash::Cache.new(hash).get :chunky
|
|
55
|
+
# => "bacon"
|
|
56
|
+
#
|
|
57
|
+
# >> hash.get :chunky # the original object has the new methods too (we don't dup/clone)
|
|
58
|
+
# => "bacon"
|
|
59
|
+
#
|
|
60
|
+
# >> Hash.new.get 'foo'
|
|
61
|
+
# NoMethodError: undefined method `get' for {}:Hash
|
|
62
|
+
# from (irb):11
|
|
63
|
+
#
|
|
64
|
+
def self.new hash = nil
|
|
65
|
+
hash = hash || Hash.new
|
|
66
|
+
hash.send :extend, self
|
|
67
|
+
hash
|
|
68
|
+
end
|
|
69
|
+
|
|
70
|
+
def get key
|
|
71
|
+
self[ key ]
|
|
72
|
+
end
|
|
73
|
+
|
|
74
|
+
def set key, value
|
|
75
|
+
self[ key ] = value
|
|
76
|
+
end
|
|
77
|
+
|
|
78
|
+
end
|
|
79
|
+
|
|
80
|
+
end
|
data/lib/hash/cache.rb
ADDED
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
require 'rubygems'
|
|
2
|
+
require 'spec'
|
|
3
|
+
require File.dirname(__FILE__) + '/../lib/hash-cache'
|
|
4
|
+
|
|
5
|
+
describe Hash::Cache do
|
|
6
|
+
|
|
7
|
+
it 'should not have #get or #set methods by default' do
|
|
8
|
+
Hash.new.should_not respond_to('get')
|
|
9
|
+
Hash.new.should_not respond_to('set')
|
|
10
|
+
end
|
|
11
|
+
|
|
12
|
+
it 'should get a useful message if including Hash::Cache in an incompatible class' do
|
|
13
|
+
lambda {
|
|
14
|
+
Fixnum.send :include, Hash::Cache
|
|
15
|
+
}.should raise_error(/can only be included in objects that implement the \[\]= method/)
|
|
16
|
+
end
|
|
17
|
+
|
|
18
|
+
it 'should have an easy way to get an instance of Hash with Hash::Cache included' do
|
|
19
|
+
cache = Hash::Cache.new
|
|
20
|
+
|
|
21
|
+
cache.get('chunky').should be_nil
|
|
22
|
+
cache.set 'chunky', 'bacon'
|
|
23
|
+
cache.get('chunky').should == 'bacon'
|
|
24
|
+
end
|
|
25
|
+
|
|
26
|
+
it 'should accept an existing Hash in the constructor' do
|
|
27
|
+
Hash::Cache.new( :x => 5 ).get(:x).should == 5
|
|
28
|
+
end
|
|
29
|
+
|
|
30
|
+
end
|
data/spec/spec.opts
ADDED
metadata
ADDED
|
@@ -0,0 +1,60 @@
|
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
|
2
|
+
name: devfu-hash-cache
|
|
3
|
+
version: !ruby/object:Gem::Version
|
|
4
|
+
version: 0.1.0
|
|
5
|
+
platform: ruby
|
|
6
|
+
authors:
|
|
7
|
+
- remi
|
|
8
|
+
autorequire:
|
|
9
|
+
bindir: bin
|
|
10
|
+
cert_chain: []
|
|
11
|
+
|
|
12
|
+
date: 2009-08-24 00:00:00 -07:00
|
|
13
|
+
default_executable:
|
|
14
|
+
dependencies: []
|
|
15
|
+
|
|
16
|
+
description: "Adds #get and #set methods to Hash(es) so they can be used as conventional cache stores"
|
|
17
|
+
email: remi@remitaylor.com
|
|
18
|
+
executables: []
|
|
19
|
+
|
|
20
|
+
extensions: []
|
|
21
|
+
|
|
22
|
+
extra_rdoc_files:
|
|
23
|
+
- README.rdoc
|
|
24
|
+
files:
|
|
25
|
+
- README.rdoc
|
|
26
|
+
- Rakefile
|
|
27
|
+
- VERSION
|
|
28
|
+
- lib/hash-cache.rb
|
|
29
|
+
- lib/hash/cache.rb
|
|
30
|
+
- spec/hash-cache_spec.rb
|
|
31
|
+
- spec/spec.opts
|
|
32
|
+
has_rdoc: false
|
|
33
|
+
homepage: http://github.com/devfu/hash-cache
|
|
34
|
+
licenses:
|
|
35
|
+
post_install_message:
|
|
36
|
+
rdoc_options:
|
|
37
|
+
- --charset=UTF-8
|
|
38
|
+
require_paths:
|
|
39
|
+
- lib
|
|
40
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
|
41
|
+
requirements:
|
|
42
|
+
- - ">="
|
|
43
|
+
- !ruby/object:Gem::Version
|
|
44
|
+
version: "0"
|
|
45
|
+
version:
|
|
46
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
|
47
|
+
requirements:
|
|
48
|
+
- - ">="
|
|
49
|
+
- !ruby/object:Gem::Version
|
|
50
|
+
version: "0"
|
|
51
|
+
version:
|
|
52
|
+
requirements: []
|
|
53
|
+
|
|
54
|
+
rubyforge_project:
|
|
55
|
+
rubygems_version: 1.3.5
|
|
56
|
+
signing_key:
|
|
57
|
+
specification_version: 3
|
|
58
|
+
summary: "Adds #get and #set methods to Hash(es)"
|
|
59
|
+
test_files:
|
|
60
|
+
- spec/hash-cache_spec.rb
|