Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

refactor custom storage initialization

  • Loading branch information...
commit 36f38083295e7a72e9bda18190dbe7956c3fb3c4 1 parent 2e3484e
@igrigorik authored
View
9 lib/rack/speedtracer.rb
@@ -10,14 +10,7 @@
module Rack
module SpeedTracer
def self.new(app, options = {}, &blk)
- storage = case options.delete(:storage)
- when :redis then
- RedisStorage
- else
- MemoryStorage
- end
-
- Context.new(app, {:storage_class => storage}.merge(options), &blk)
+ Context.new(app, options, &blk)
end
end
end
View
6 lib/rack/speedtracer/context.rb
@@ -9,9 +9,13 @@ class Context
attr_accessor :db
def initialize(app, options = {}, &blk)
+ options = {
+ :storage => Storage::Memory
+ }.merge(options)
+
@app = app
@uuid = UUID.new
- @db = options.delete(:storage_class).new(options)
+ @db = options.delete(:storage).new(options)
yield self if block_given?
end
View
14 lib/rack/speedtracer/memory_storage.rb
@@ -1,14 +1,16 @@
require 'forwardable'
+
module Rack
module SpeedTracer
+ module Storage
+ class Memory
+ extend Forwardable
- class MemoryStorage
- extend Forwardable
-
- def_delegators :@db_hash, :[], :[]=
+ def_delegators :@db_hash, :[], :[]=
- def initialize(options)
- @db_hash = {}
+ def initialize(options)
+ @db_hash = {}
+ end
end
end
end
View
38 lib/rack/speedtracer/redis_storage.rb
@@ -2,29 +2,31 @@
module Rack
module SpeedTracer
+ module Storage
- class RedisStorage
- def initialize(options)
- @redis = Redis.new(options[:redis_options] || {})
- @ttl = options[:trace_ttl] || 600
- @namespace = options[:namespace] || "speedtracer"
- end
+ class Redis
+ def initialize(options)
+ @redis = ::Redis.new(options[:redis_options] || {})
+ @ttl = options[:trace_ttl] || 600
+ @namespace = options[:namespace] || "speedtracer"
+ end
- def [](trace_id)
- @redis.get(namespace_key(trace_id))
- end
+ def [](trace_id)
+ @redis.get(namespace_key(trace_id))
+ end
- def []=(trace_id, trace)
- key = namespace_key(trace_id)
- @redis.set(key, trace)
- @redis.expire(key, @ttl)
- end
+ def []=(trace_id, trace)
+ key = namespace_key(trace_id)
+ @redis.set(key, trace)
+ @redis.expire(key, @ttl)
+ end
- private
+ private
- def namespace_key(key)
- "#{@namespace}:#{key}"
+ def namespace_key(key)
+ "#{@namespace}:#{key}"
+ end
end
end
end
-end
+end
View
21 spec/rack/speedtracer/context_spec.rb
@@ -20,13 +20,24 @@
middleware.db.should == db
end
- it "takes an optional storage class" do
- class SomeStorageClass
- def initialize(options)
+ context 'storage engine' do
+ it 'takes an optional storage class' do
+ class SomeStorageClass
+ def initialize(options);end
end
+ st = Rack::SpeedTracer.new(app, :storage => SomeStorageClass)
+ st.db.class.should == SomeStorageClass
+ end
+
+ it 'should default to memory storage' do
+ st = Rack::SpeedTracer.new(app)
+ st.db.class.should == Rack::SpeedTracer::Storage::Memory
+ end
+
+ it 'should accept redis storage' do
+ st = Rack::SpeedTracer.new(app, :storage => Rack::SpeedTracer::Storage::Redis)
+ st.db.class.should == Rack::SpeedTracer::Storage::Redis
end
- middleware = Rack::SpeedTracer.new(app, :storage_class => SomeStorageClass)
- middleware.db.class.should == SomeStorageClass
end
end
View
4 spec/rack/speedtracer/redis_storage_spec.rb
@@ -1,11 +1,11 @@
require 'spec_helper'
require 'rack/speedtracer/redis_storage'
-describe Rack::SpeedTracer::RedisStorage do
+describe Rack::SpeedTracer::Storage::Redis do
before(:each) do
@mock_redis = mock('redis')
Redis.stub(:new).and_return(@mock_redis)
- @storage_klass = Rack::SpeedTracer::RedisStorage
+ @storage_klass = Rack::SpeedTracer::Storage::Redis
end
it "fetches traces from Redis" do

0 comments on commit 36f3808

Please sign in to comment.
Something went wrong with that request. Please try again.