stash 0.1.1 → 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
data/Rakefile CHANGED
@@ -12,7 +12,7 @@ begin
12
12
  gem.authors = ["Tony Arcieri"]
13
13
  gem.add_dependency "redis", "~> 2.1.0"
14
14
  gem.add_dependency "redis-namespace", "~> 0.10.0"
15
- gem.add_development_dependency "rspec", "~> 2.1.0"
15
+ gem.add_development_dependency "rspec", ">= 2.2.0"
16
16
  # gem is a Gem::Specification... see http://www.rubygems.org/read/chapter/20 for additional settings
17
17
  end
18
18
  Jeweler::GemcutterTasks.new
@@ -25,7 +25,7 @@ RSpec::Core::RakeTask.new(:rspec) do |rspec|
25
25
  rspec.rspec_opts = %w[-fs -c -b]
26
26
  end
27
27
 
28
- RSpec::Core::RakeTask.new(:rspec) do |rspec|
28
+ RSpec::Core::RakeTask.new(:rcov) do |rspec|
29
29
  rspec.rspec_opts = %w[-fs -c -b]
30
30
  rspec.rcov = true
31
31
  end
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.1.1
1
+ 1.0.0
@@ -7,16 +7,7 @@ class Stash
7
7
 
8
8
  def initialize(config)
9
9
  raise ArgumentError, "no adapter specified" unless config[:adapter]
10
- adapter_name = config[:adapter].to_s.split('_').map { |s| s.capitalize }.join
11
- adapter_name += "Adapter"
12
-
13
- begin
14
- adapter_class = Stash.const_get adapter_name
15
- rescue NameError
16
- raise ArgumentError, "unknown adapter: #{config[:adapter]}"
17
- end
18
-
19
- @adapter = adapter_class.new config
10
+ @adapter = Stash.adapter_class(config[:adapter]).new config
20
11
  end
21
12
 
22
13
  # Store an object in the Stash
@@ -41,6 +41,18 @@ class Stash
41
41
  default.delete(key)
42
42
  true
43
43
  end
44
+
45
+ # Obtain the class for a given adapter name
46
+ def adapter_class(name)
47
+ adapter_name = name.to_s.split('_').map { |s| s.capitalize }.join
48
+ adapter_name << "Adapter"
49
+
50
+ begin
51
+ Stash.const_get adapter_name
52
+ rescue NameError
53
+ raise ArgumentError, "unknown adapter: #{name}"
54
+ end
55
+ end
44
56
  end
45
57
 
46
58
  extend ClassMethods
@@ -4,30 +4,32 @@ require 'redis-namespace'
4
4
  class Stash
5
5
  # Adapter for the Redis data structures server.
6
6
  # See http://code.google.com/p/redis/
7
- class RedisAdapter
8
- attr_reader :capabilities
9
-
7
+ class RedisAdapter
10
8
  def initialize(config)
11
- # Symbolize keys in config
12
- config = config.inject({}) { |h, (k, v)| h[k.to_sym] = v; h }
9
+ # Convert config keys to symbols
10
+ config = symbolize_config(config)
13
11
 
14
12
  raise ArgumentError, "missing 'host' key" unless config[:host]
15
13
  config[:port] ||= 6379 # Default Redis port
16
14
 
17
- redis = Redis.new config
18
- redis = Redis::Namespace.new config[:namespace], :redis => redis if config[:namespace]
19
-
20
- @capabilities = [:string, :list, :hash]
21
-
22
- # Redis 2.0RC+ supports blocking pop
23
- @capabilities << :bpop if redis.info['redis_version'] >= "1.3.0"
15
+ @config = config
16
+ end
17
+
18
+ # Obtain a connection to Redis
19
+ def connection
20
+ redis = Redis.new @config
21
+ redis = Redis::Namespace.new @config[:namespace], :redis => redis if @config[:namespace]
24
22
 
25
- @redis = redis
23
+ begin
24
+ yield redis
25
+ ensure
26
+ redis.quit
27
+ end
26
28
  end
27
29
 
28
30
  # Set a given key within Redis
29
31
  def []=(key, value)
30
- @redis.set key.to_s, value.to_s
32
+ connection { |redis| redis.set key.to_s, value.to_s }
31
33
  end
32
34
 
33
35
  # Retrieve a given key from Redis
@@ -42,91 +44,109 @@ class Stash
42
44
 
43
45
  # Retrieve the type for a given key
44
46
  def type(key)
45
- @redis.type key.to_s
47
+ connection { |redis| redis.type key.to_s }
46
48
  end
47
49
 
48
50
  # Retrieve a key as a string
49
51
  def get(key)
50
- @redis.get key.to_s
52
+ connection { |redis| redis.get key.to_s }
51
53
  end
52
54
 
53
55
  # Delete a key
54
56
  def delete(key)
55
- @redis.del key.to_s
57
+ connection { |redis| redis.del key.to_s }
56
58
  end
57
59
 
58
60
  # Push an element onto a list
59
61
  def list_push(name, value, side)
60
- case side
61
- when :right
62
- @redis.rpush name.to_s, value.to_s
63
- when :left
64
- @redis.lpush name.to_s, value.to_s
65
- else raise ArgumentError, "left or right plztks"
62
+ connection do |redis|
63
+ case side
64
+ when :right
65
+ redis.rpush name.to_s, value.to_s
66
+ when :left
67
+ redis.lpush name.to_s, value.to_s
68
+ else raise ArgumentError, "left or right plztks"
69
+ end
66
70
  end
67
71
  end
68
72
 
69
73
  # Pop from a list
70
74
  def list_pop(name, side)
71
- case side
72
- when :right
73
- @redis.rpop name.to_s
74
- when :left
75
- @redis.lpop name.to_s
76
- else raise ArgumentError, "left or right plztks"
75
+ connection do |redis|
76
+ case side
77
+ when :right
78
+ redis.rpop name.to_s
79
+ when :left
80
+ redis.lpop name.to_s
81
+ else raise ArgumentError, "left or right plztks"
82
+ end
77
83
  end
78
84
  end
79
85
 
80
86
  # Blocking pop from a list
81
87
  def list_blocking_pop(name, side, timeout = nil)
82
- timeout ||= 0
83
- res = case side
84
- when :left
85
- @redis.blpop name, timeout
86
- when :right
87
- @redis.brpop name, timeout
88
- else raise ArgumentError, "left or right plztks"
89
- end
88
+ connection do |redis|
89
+ timeout ||= 0
90
+ res = case side
91
+ when :left
92
+ redis.blpop name, timeout
93
+ when :right
94
+ redis.brpop name, timeout
95
+ else raise ArgumentError, "left or right plztks"
96
+ end
90
97
 
91
- return res[1] if res
92
- raise Stash::TimeoutError, "request timed out"
98
+ return res[1] if res
99
+ raise Stash::TimeoutError, "request timed out"
100
+ end
93
101
  end
94
102
 
95
103
  # Retrieve the length of a list
96
104
  def list_length(name)
97
- @redis.llen name.to_s
105
+ connection { |redis| redis.llen name.to_s }
98
106
  end
99
107
 
100
108
  # Retrieve the given range from a list
101
109
  def list_range(name, from, to)
102
- @redis.lrange name.to_s, from, to
110
+ connection { |redis| redis.lrange name.to_s, from, to }
103
111
  end
104
112
 
105
113
  # Retrieve a value from a hash
106
114
  def hash_get(name, key)
107
- res = @redis.hget name.to_s, key.to_s
115
+ res = connection { |redis| redis.hget name.to_s, key.to_s }
108
116
  return if res == ""
109
117
  res
110
118
  end
111
119
 
112
120
  # Store a value in a hash
113
121
  def hash_set(name, key, value)
114
- @redis.hset name.to_s, key.to_s, value.to_s
122
+ connection { |redis| redis.hset name.to_s, key.to_s, value.to_s }
115
123
  end
116
124
 
117
125
  # Retrieve the contents of a hash as a Ruby hash
118
126
  def hash_value(name)
119
- @redis.hgetall name.to_s
127
+ connection { |redis| redis.hgetall name.to_s }
120
128
  end
121
129
 
122
130
  # Delete an entry from a hash
123
131
  def hash_delete(name, key)
124
- @redis.hdel name.to_s, key.to_s
132
+ connection { |redis| redis.hdel name.to_s, key.to_s }
125
133
  end
126
134
 
127
135
  # Return the length of a hash
128
136
  def hash_length(name)
129
- @redis.hlen name.to_s
137
+ connection { |redis| redis.hlen name.to_s }
138
+ end
139
+
140
+ #######
141
+ private
142
+ #######
143
+
144
+ def symbolize_config(config)
145
+ new_config = {}
146
+ config.each do |k, v|
147
+ new_config[k.to_sym] = v
148
+ end
149
+ new_config
130
150
  end
131
151
  end
132
152
  end
@@ -0,0 +1,68 @@
1
+ # Generated by jeweler
2
+ # DO NOT EDIT THIS FILE DIRECTLY
3
+ # Instead, edit Jeweler::Tasks in Rakefile, and run 'rake gemspec'
4
+ # -*- encoding: utf-8 -*-
5
+
6
+ Gem::Specification.new do |s|
7
+ s.name = %q{stash}
8
+ s.version = "1.0.0"
9
+
10
+ s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
+ s.authors = ["Tony Arcieri"]
12
+ s.date = %q{2010-12-07}
13
+ s.description = %q{Stash maps the facilities provided by data structures servers onto classes which mimic Ruby's built-in types}
14
+ s.email = %q{tony@medioh.com}
15
+ s.extra_rdoc_files = [
16
+ "LICENSE",
17
+ "README.markdown"
18
+ ]
19
+ s.files = [
20
+ ".document",
21
+ "LICENSE",
22
+ "README.markdown",
23
+ "Rakefile",
24
+ "VERSION",
25
+ "lib/stash.rb",
26
+ "lib/stash/class_methods.rb",
27
+ "lib/stash/hash.rb",
28
+ "lib/stash/list.rb",
29
+ "lib/stash/redis_adapter.rb",
30
+ "lib/stash/string.rb",
31
+ "spec/hash_spec.rb",
32
+ "spec/list_spec.rb",
33
+ "spec/spec.opts",
34
+ "spec/spec_helper.rb",
35
+ "spec/string_spec.rb",
36
+ "stash.gemspec"
37
+ ]
38
+ s.homepage = %q{http://github.com/tarcieri/stash}
39
+ s.require_paths = ["lib"]
40
+ s.rubygems_version = %q{1.3.7}
41
+ s.summary = %q{Abstract interface to data structures servers}
42
+ s.test_files = [
43
+ "spec/hash_spec.rb",
44
+ "spec/list_spec.rb",
45
+ "spec/spec_helper.rb",
46
+ "spec/string_spec.rb"
47
+ ]
48
+
49
+ if s.respond_to? :specification_version then
50
+ current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
51
+ s.specification_version = 3
52
+
53
+ if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('1.2.0') then
54
+ s.add_runtime_dependency(%q<redis>, ["~> 2.1.0"])
55
+ s.add_runtime_dependency(%q<redis-namespace>, ["~> 0.10.0"])
56
+ s.add_development_dependency(%q<rspec>, [">= 2.2.0"])
57
+ else
58
+ s.add_dependency(%q<redis>, ["~> 2.1.0"])
59
+ s.add_dependency(%q<redis-namespace>, ["~> 0.10.0"])
60
+ s.add_dependency(%q<rspec>, [">= 2.2.0"])
61
+ end
62
+ else
63
+ s.add_dependency(%q<redis>, ["~> 2.1.0"])
64
+ s.add_dependency(%q<redis-namespace>, ["~> 0.10.0"])
65
+ s.add_dependency(%q<rspec>, [">= 2.2.0"])
66
+ end
67
+ end
68
+
metadata CHANGED
@@ -1,13 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: stash
3
3
  version: !ruby/object:Gem::Version
4
- hash: 25
4
+ hash: 23
5
5
  prerelease: false
6
6
  segments:
7
- - 0
8
- - 1
9
7
  - 1
10
- version: 0.1.1
8
+ - 0
9
+ - 0
10
+ version: 1.0.0
11
11
  platform: ruby
12
12
  authors:
13
13
  - Tony Arcieri
@@ -15,7 +15,7 @@ autorequire:
15
15
  bindir: bin
16
16
  cert_chain: []
17
17
 
18
- date: 2010-11-30 00:00:00 -07:00
18
+ date: 2010-12-07 00:00:00 -07:00
19
19
  default_executable:
20
20
  dependencies:
21
21
  - !ruby/object:Gem::Dependency
@@ -56,14 +56,14 @@ dependencies:
56
56
  requirement: &id003 !ruby/object:Gem::Requirement
57
57
  none: false
58
58
  requirements:
59
- - - ~>
59
+ - - ">="
60
60
  - !ruby/object:Gem::Version
61
- hash: 11
61
+ hash: 7
62
62
  segments:
63
63
  - 2
64
- - 1
64
+ - 2
65
65
  - 0
66
- version: 2.1.0
66
+ version: 2.2.0
67
67
  type: :development
68
68
  version_requirements: *id003
69
69
  description: Stash maps the facilities provided by data structures servers onto classes which mimic Ruby's built-in types
@@ -92,6 +92,7 @@ files:
92
92
  - spec/spec.opts
93
93
  - spec/spec_helper.rb
94
94
  - spec/string_spec.rb
95
+ - stash.gemspec
95
96
  has_rdoc: true
96
97
  homepage: http://github.com/tarcieri/stash
97
98
  licenses: []