hash-cache 0.1.1
Sign up to get free protection for your applications and to get access to all the features.
- data/lib/hash-cache.rb +80 -0
- data/lib/hash/cache.rb +2 -0
- metadata +56 -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
metadata
ADDED
@@ -0,0 +1,56 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: hash-cache
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.1
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- remi
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
|
12
|
+
date: 2010-01-10 00:00:00 -07:00
|
13
|
+
default_executable:
|
14
|
+
dependencies: []
|
15
|
+
|
16
|
+
description: "Adds #get and #set methods to hashes 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
|
+
|
24
|
+
files:
|
25
|
+
- lib/hash/cache.rb
|
26
|
+
- lib/hash-cache.rb
|
27
|
+
has_rdoc: true
|
28
|
+
homepage: http://github.com/devfu/hash-cache
|
29
|
+
licenses: []
|
30
|
+
|
31
|
+
post_install_message:
|
32
|
+
rdoc_options: []
|
33
|
+
|
34
|
+
require_paths:
|
35
|
+
- lib
|
36
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - ">="
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: "0"
|
41
|
+
version:
|
42
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
43
|
+
requirements:
|
44
|
+
- - ">="
|
45
|
+
- !ruby/object:Gem::Version
|
46
|
+
version: "0"
|
47
|
+
version:
|
48
|
+
requirements: []
|
49
|
+
|
50
|
+
rubyforge_project:
|
51
|
+
rubygems_version: 1.3.5
|
52
|
+
signing_key:
|
53
|
+
specification_version: 3
|
54
|
+
summary: "Adds #get and #set methods to hashes"
|
55
|
+
test_files: []
|
56
|
+
|