Permalink
Browse files

Setup redis

  • Loading branch information...
0 parents commit 3d44b975caf56fa36cfdaa89cf88e1aab4a751df @felixclack committed Feb 17, 2012
Showing with 253 additions and 0 deletions.
  1. +18 −0 .bnsignore
  2. +4 −0 History.txt
  3. +55 −0 README.md
  4. +17 −0 Rakefile
  5. +7 −0 bin/timeline
  6. +59 −0 lib/timeline.rb
  7. +42 −0 lib/timeline/config.rb
  8. +38 −0 spec/spec_helper.rb
  9. +12 −0 spec/timeline_spec.rb
  10. +1 −0 version.txt
@@ -0,0 +1,18 @@
+# The list of files that should be ignored by Mr Bones.
+# Lines that start with '#' are comments.
+#
+# A .gitignore file can be used instead by setting it as the ignore
+# file in your Rakefile:
+#
+# Bones {
+# ignore_file '.gitignore'
+# }
+#
+# For a project with a C extension, the following would be a good set of
+# exclude patterns (uncomment them if you want to use them):
+# *.[oa]
+# *~
+announcement.txt
+coverage
+doc
+pkg
@@ -0,0 +1,4 @@
+== 0.1.0 / 2012-02-17
+
+* 1 major enhancement
+ * Birthday!
@@ -0,0 +1,55 @@
+timeline
+===========
+
+Redis backed timelines in your app.
+
+Features
+--------
+
+* FIXME (list of features and unsolved problems)
+
+Examples
+--------
+
+ FIXME (code sample of usage)
+
+Requirements
+------------
+
+* FIXME (list of requirements)
+
+Install
+-------
+
+* FIXME (sudo gem install, anything else)
+
+Author
+------
+
+Original author: Felix Clack
+
+License
+-------
+
+(The MIT License) FIXME (different license?)
+
+Copyright (c) 2012 FIXME (author's name)
+
+Permission is hereby granted, free of charge, to any person obtaining
+a copy of this software and associated documentation files (the
+'Software'), to deal in the Software without restriction, including
+without limitation the rights to use, copy, modify, merge, publish,
+distribute, sublicense, and/or sell copies of the Software, and to
+permit persons to whom the Software is furnished to do so, subject to
+the following conditions:
+
+The above copyright notice and this permission notice shall be
+included in all copies or substantial portions of the Software.
+
+THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND,
+EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
+IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
+CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
+TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
+SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,17 @@
+
+begin
+ require 'bones'
+rescue LoadError
+ abort '### Please install the "bones" gem ###'
+end
+
+task :default => 'test:run'
+task 'gem:release' => 'test:run'
+
+Bones {
+ name 'timeline'
+ authors 'FIXME (who is writing this software)'
+ email 'FIXME (your e-mail)'
+ url 'FIXME (project homepage)'
+}
+
@@ -0,0 +1,7 @@
+#!/usr/bin/env ruby
+
+root = File.expand_path('../..', __FILE__)
+require File.join(root, %w[lib timeline])
+
+# Put your code here
+
@@ -0,0 +1,59 @@
+module Timeline
+
+ # :stopdoc:
+ LIBPATH = ::File.expand_path('..', __FILE__) + ::File::SEPARATOR
+ PATH = ::File.dirname(LIBPATH) + ::File::SEPARATOR
+ VERSION = ::File.read(PATH + 'version.txt').strip
+ # :startdoc:
+
+ # Returns the library path for the module. If any arguments are given,
+ # they will be joined to the end of the libray path using
+ # <tt>File.join</tt>.
+ #
+ def self.libpath( *args )
+ rv = args.empty? ? LIBPATH : ::File.join(LIBPATH, args.flatten)
+ if block_given?
+ begin
+ $LOAD_PATH.unshift LIBPATH
+ rv = yield
+ ensure
+ $LOAD_PATH.shift
+ end
+ end
+ return rv
+ end
+
+ # Returns the lpath for the module. If any arguments are given,
+ # they will be joined to the end of the path using
+ # <tt>File.join</tt>.
+ #
+ def self.path( *args )
+ rv = args.empty? ? PATH : ::File.join(PATH, args.flatten)
+ if block_given?
+ begin
+ $LOAD_PATH.unshift PATH
+ rv = yield
+ ensure
+ $LOAD_PATH.shift
+ end
+ end
+ return rv
+ end
+
+ # Utility method used to require all files ending in .rb that lie in the
+ # directory below this file that has the same name as the filename passed
+ # in. Optionally, a specific _directory_ name can be passed in such that
+ # the _filename_ does not have to be equivalent to the directory.
+ #
+ def self.require_all_libs_relative_to( fname, dir = nil )
+ dir ||= ::File.basename(fname, '.*')
+ search_me = ::File.expand_path(
+ ::File.join(::File.dirname(fname), dir, '**', '*.rb'))
+
+ Dir.glob(search_me).sort.each {|rb| require rb}
+ end
+
+end # module Timeline
+
+Timeline.require_all_libs_relative_to(__FILE__)
+
@@ -0,0 +1,42 @@
+require 'redis/namespace'
+
+module Timeline
+ class << self
+
+ # Accepts:
+ # 1. A 'hostname:port' String
+ # 2. A 'hostname:port:db' String (to select the Redis db)
+ # 3. A 'hostname:port/namespace' String (to set the Redis namespace)
+ # 4. A Redis URL String 'redis://host:port'
+ # 5. An instance of `Redis`, `Redis::Client`, `Redis::DistRedis`,
+ # or `Redis::Namespace`.
+ def redis=(server)
+ case server
+ when String
+ if server =~ /redis\:\/\//
+ redis = Redis.connect(:url => server, :thread_safe => true)
+ else
+ server, namespace = server.split('/', 2)
+ host, port, db = server.split(':')
+ redis = Redis.new(:host => host, :port => port,
+ :thread_safe => true, :db => db)
+ end
+ namespace ||= :timeline
+
+ @redis = Redis::Namespace.new(namespace, :redis => redis)
+ when Redis::Namespace
+ @redis = server
+ else
+ @redis = Redis::Namespace.new(:resque, :redis => server)
+ end
+ end
+
+ # Returns the current Redis connection. If none has been created, will
+ # create a new one.
+ def redis
+ return @redis if @redis
+ self.redis = Redis.respond_to?(:connect) ? Redis.connect : "localhost:6379"
+ self.redis
+ end
+ end
+end
@@ -0,0 +1,38 @@
+require File.expand_path(File.join(File.dirname(__FILE__), %w[.. lib timeline]))
+dir = File.dirname(File.expand_path(__FILE__))
+
+RSpec.configure do |config|
+
+end
+
+#
+# make sure we can run redis
+#
+
+if !system("which redis-server")
+ puts '', "** can't find `redis-server` in your path"
+ puts "** try running `sudo rake install`"
+ abort ''
+end
+
+
+#
+# start our own redis when the tests start,
+# kill it when they end
+#
+
+at_exit do
+ next if $!
+
+ exit_code = RSpec::Runner.autorun
+
+ pid = `ps -A -o pid,command | grep [r]edis-test`.split(" ")[0]
+ puts "Killing test redis server..."
+ `rm -f #{dir}/dump.rdb`
+ Process.kill("KILL", pid.to_i)
+ exit exit_code
+end
+
+puts "Starting redis for testing at localhost:9736..."
+`redis-server #{dir}/redis-test.conf`
+Timeline.redis = 'localhost:9736'
@@ -0,0 +1,12 @@
+require File.join(File.dirname(__FILE__), %w[spec_helper])
+
+describe Timeline do
+ it("can set a redis instance") { Timeline.should respond_to(:redis=) }
+ it("has a namespace, timeline") { Timeline.redis.namespace.should == :timeline }
+
+ it "sets the namespace through a url-like string" do
+ Timeline.redis = 'localhost:9736/namespace'
+ Timeline.redis.namespace.should == 'namespace'
+ end
+end
+
@@ -0,0 +1 @@
+0.1.0

0 comments on commit 3d44b97

Please sign in to comment.