Skip to content

Commit

Permalink
Reshuffles the placement of the Merb::Cache methods
Browse files Browse the repository at this point in the history
  • Loading branch information
Daniel Neighman committed Jun 22, 2008
1 parent ca7b9e4 commit 177a0b2
Show file tree
Hide file tree
Showing 5 changed files with 79 additions and 74 deletions.
21 changes: 20 additions & 1 deletion lib/merb-cache.rb
Expand Up @@ -4,4 +4,23 @@
%w(gzip/gzip).each{|dep| require File.join(File.dirname(__FILE__), '..', 'vendor', dep) }

require 'merb-cache/merb-cache'
end

require 'merb-cache/controller'
require 'merb-cache/cache_store'

path = File.expand_path(File.join(File.dirname(__FILE__))) / "merb-cache" / "cache_stores"
puts "Path is #{path}"
Merb::Cache.register(:memcached, :path => (path / "memcached_store"), :class_name => "MemcachedStore")
Merb::Cache.register(:mintcache, :path => (path / "mintcache_store"), :class_name => "MintcachedStore")


Merb::BootLoader.before_app_loads do
# code that can be required after the application loads
# Initialize the cache store if there is not one setup for the default
Merb::Cache.setup(:default, :memcached) if Merb::Cache[:default].empty?
end

Merb::BootLoader.after_app_loads do

end
end
53 changes: 0 additions & 53 deletions lib/merb-cache/cache_store.rb
@@ -1,58 +1,5 @@
module Merb
module Cache

class << self
# Addess to registered stores
# name<Symbol> : The Name of the store type
# value<Hash> : Requires a hash with to describe the cache. See Merb::Cache.register
def [](name)
active_stores
@active_stores[name]
end

# Register a cache store with Merb::Cache
# name<Symbol> : A label for the cache store
# options<Hash>: A hash of options
# Required Options
# :path => "path/to/cache/store"
# :class_name => "ClassNameOfStore"
def register(name, options = {})
raise ArgumentError, "Requires :path and :class_name options" unless ([:path, :class_name] - options.keys).empty?
@registered_stores ||= Hash.new{|h,k| h[k] = {}}
@registered_stores[name.to_sym] = options
end

# Sets up a cache store
# name<Symbol> : A label or name to give the cache
# store<Symbol> : A registered store type. By default :memcached, and :mintstore are supported
# opts<Hash> : A hash to pass through to the store for configuration
def setup(name, store, opts = {})
active_stores
load_store(store)
@active_stores[name] = self.const_get(registered_stores[store.to_sym][:class_name]).new(opts)
end

# === Returns
# A Hash of registered store types
def registered_stores
@registered_stores || Hash.new{|h,k| h[k] = {}}
end

# Returns a Hash of active stores.
def active_stores
@active_stores ||= {}
end

protected
def []=(name,value)
@active_stores[name] = value
end

def load_store(store)
require registered_stores[store.to_sym][:path] rescue raise Store::NotFound.new(store)
end
end

class Store
attr_accessor :config

Expand Down
71 changes: 55 additions & 16 deletions lib/merb-cache/merb-cache.rb
@@ -1,18 +1,57 @@
# Require components
require 'merb-cache/controller'
require 'merb-cache/cache_store'
module Merb
module Cache
class << self
# Addess to registered stores
# name<Symbol> : The Name of the store type
# value<Hash> : Requires a hash with to describe the cache. See Merb::Cache.register
def [](name)
active_stores
@active_stores[name]
end

path = File.expand_path(File.join(File.dirname(__FILE__)))
Merb::Cache.register(:memcached, :path => (path / "cache_stores" / "memcached_store"), :class_name => "MemcachedStore")
Merb::Cache.register(:mintcache, :path => (path / "cache_stores" / "mintcache_store"), :class_name => "MintcachedStore")


Merb::BootLoader.before_app_loads do
# require code that must be loaded before the application
end

Merb::BootLoader.after_app_loads do
# code that can be required after the application loads
# Initialize the cache store if there is not one setup for the default
Merb::Cache.setup(:default, :memcached) if Merb::Cache[:default].empty?
end
# Register a cache store with Merb::Cache
# name<Symbol> : A label for the cache store
# options<Hash>: A hash of options
# Required Options
# :path => "path/to/cache/store"
# :class_name => "ClassNameOfStore"
def register(name, options = {})
raise ArgumentError, "Requires :path and :class_name options" unless ([:path, :class_name] - options.keys).empty?
raise Merb::Cache::Store::NotFound, "Missing Store File: #{options[:path]}" unless File.exists?("#{options[:path]}.rb")
@registered_stores ||= Hash.new{|h,k| h[k] = {}}
@registered_stores[name.to_sym] = options
end

# Sets up a cache store
# name<Symbol> : A label or name to give the cache
# store<Symbol> : A registered store type. By default :memcached, and :mintstore are supported
# opts<Hash> : A hash to pass through to the store for configuration
def setup(name, store, opts = {})
active_stores
load_store(store)
@active_stores[name] = self.const_get(registered_stores[store.to_sym][:class_name]).new(opts)
end

# === Returns
# A Hash of registered store types
def registered_stores
@registered_stores || Hash.new{|h,k| h[k] = {}}
end

# Returns a Hash of active stores.
def active_stores
@active_stores ||= {}
end

protected
def []=(name,value)
@active_stores[name] = value
end

def load_store(store)
require registered_stores[store.to_sym][:path] rescue raise Store::NotFound.new(store)
end
end # # << self
end # Cache
end # Merb
7 changes: 4 additions & 3 deletions spec/cache_store_spec.rb
Expand Up @@ -28,7 +28,7 @@
end

it "should allow a cache store to register itself" do
file = File.expand_path(File.dirname(__FILE__) / ".." / "lib" / "merb-cache" / "cache_stores", "memcached_store" )
file = File.expand_path(File.dirname(__FILE__) / ".." / "lib" / "merb-cache" / "cache_stores"/ "memcached_store" )
Merb::Cache.register(:tester, :path => file, :class_name => "MemcachedStore")
end

Expand All @@ -48,8 +48,9 @@
end

it "should not allow you to register a store if the file does not exist" do
pending
Merb::Cache.register(:custom_store, "does/not/exist")
lambda do
Merb::Cache.register(:custom_store, :path => "does/not/exist", :class_name => "NotHere")
end.should raise_error(Merb::Cache::Store::NotFound)
Merb::Cache[:custom_store].should be_nil
end

Expand Down
1 change: 0 additions & 1 deletion spec/merb_cache_spec.rb
Expand Up @@ -19,7 +19,6 @@
:fail => 'badconfigoptions'
}


end
end

Expand Down

0 comments on commit 177a0b2

Please sign in to comment.