lib/cockpit/core/store.rb in cockpit-0.1.1 vs lib/cockpit/core/store.rb in cockpit-0.2.0

- old
+ new

@@ -1,51 +1,47 @@ module Cockpit class Store class << self attr_accessor :stores + def find(name) + return store(name) if store?(name) + + base_path = "#{File.dirname(__FILE__)}/../stores" + + @stores[name.to_sym] = case name.to_sym + when :active_record + require "#{base_path}/active_record" + "::Cockpit::AR" + when :mongo + require "#{base_path}/mongo" + "::Cockpit::Mongo" + else + require "#{base_path}/memory" + "::Cockpit::Memory" + end + + store(name) + end + + def use(options) + eval("::#{find(options[:store])}::Store".gsub(/::[:]+/, "::")).new(options[:record], options[:name]) + end + + def support(name) + eval("::#{find(name)}::Support".gsub(/::[:]+/, "::")) + end + def stores @stores ||= {} end - def adapter(store) - stores[store.name.to_s] ||= {} -# unless stores[store.name.to_s].has_key?(store.value.to_s) - require 'moneta' - stores[store.name.to_s][store.value.to_s] = case store.value.to_s - when "mongo", "mongodb" - require 'moneta/adapters/mongodb' - Moneta::Adapters::MongoDB.new(:collection => store.name) - when "active_record" - require File.dirname(__FILE__) + '/../moneta/active_record' - Moneta::Adapters::ActiveRecord.new(:record => store.scope) - when "file" - require 'moneta/adapters/basic_file' - Moneta::Adapters::BasicFile.new(:path => "./.cockpit") - when "redis" - require 'moneta/adapters/redis' - Moneta::Adapters::Redis.new - when "memory" - require 'moneta/adapters/memory' - Moneta::Adapters::Memory.new - when "yaml" - require 'moneta/adapters/yaml' - Moneta::Adapters::YAML.new - end -# end - stores[store.name.to_s][store.value.to_s] + def store(name) + stores[name.to_sym] end - end - - attr_reader :name, :value, :scope - - def initialize(name, value, scope = nil) - @name = name - @value = value - @scope = scope - end - - def adapter - self.class.adapter(self) + + def store?(name) + stores.has_key?(name.to_sym) + end end end end