Permalink
Browse files

adding model adapter

  • Loading branch information...
1 parent b7e8d09 commit 027c3f0ab2d437e81d87d726af5e74d1ca47ca2f @ryanb committed Jul 28, 2011
View
27 Gemfile
@@ -1,16 +1,19 @@
source "http://rubygems.org"
-# case ENV["MODEL_ADAPTER"]
-# when nil, "active_record"
-# gem "sqlite3"
-# gem "activerecord", :require => "active_record"
-# gem "with_model"
-# when "data_mapper"
-# gem "dm-core", "~> 1.0.2"
-# gem "dm-sqlite-adapter", "~> 1.0.2"
-# gem "dm-migrations", "~> 1.0.2"
-# else
-# raise "Unknown model adapter: #{ENV["MODEL_ADAPTER"]}"
-# end
+case ENV["MODEL_ADAPTER"]
+when nil, "active_record"
+ gem "sqlite3"
+ gem "activerecord", :require => "active_record"
+ gem "with_model"
+when "data_mapper"
+ gem "dm-core", "~> 1.0.2"
+ gem "dm-sqlite-adapter", "~> 1.0.2"
+ gem "dm-migrations", "~> 1.0.2"
+when "mongoid"
+ gem "bson_ext", "~> 1.1"
+ gem "mongoid", "~> 2.0.0.beta.20"
+else
+ raise "Unknown model adapter: #{ENV["MODEL_ADAPTER"]}"
+end
gemspec
View
3 lib/xapit.rb
@@ -62,6 +62,9 @@ def serialize_value(value)
require 'xapit/client/facet'
require 'xapit/client/facet_option'
require 'xapit/client/remote_database'
+require 'xapit/client/model_adapters/abstract_model_adapter'
+require 'xapit/client/model_adapters/default_model_adapter'
+require 'xapit/client/model_adapters/active_record_adapter'
require 'xapit/server/database'
require 'xapit/server/query'
require 'xapit/server/indexer'
View
4 lib/xapit/client/membership.rb
@@ -11,6 +11,10 @@ def xapit(&block)
@xapit_index_builder.instance_eval(&block)
include AdditionalMethods
end
+
+ def model_adapter
+ @xapit_model_adapter ||= Xapit::Client::AbstractModelAdapter.adapter_class(self).new(self)
+ end
end
module AdditionalMethods
View
22 lib/xapit/client/model_adapters/abstract_model_adapter.rb
@@ -0,0 +1,22 @@
+module Xapit
+ module Client
+ class AbstractModelAdapter
+ def self.inherited(subclass)
+ @@subclasses ||= []
+ @@subclasses << subclass
+ end
+
+ def self.adapter_class(model_class)
+ @@subclasses.detect { |subclass| subclass.for_class?(model_class) } || DefaultModelAdapter
+ end
+
+ def self.for_class?(model_class)
+ false # override in subclass
+ end
+
+ def initialize(model_class)
+ @model_class = model_class
+ end
+ end
+ end
+end
View
9 lib/xapit/client/model_adapters/active_record_adapter.rb
@@ -0,0 +1,9 @@
+module Xapit
+ module Client
+ class ActiveRecordAdapter < AbstractModelAdapter
+ def self.for_class?(model_class)
+ model_class <= ActiveRecord::Base
+ end
+ end
+ end
+end
View
7 lib/xapit/client/model_adapters/default_model_adapter.rb
@@ -0,0 +1,7 @@
+module Xapit
+ module Client
+ class DefaultModelAdapter < AbstractModelAdapter
+ # This adapter is used when no matching adapter is found
+ end
+ end
+end
View
4 spec/xapit/client/membership_spec.rb
@@ -30,4 +30,8 @@
@member_class.xapit { facet :foo }
@member_class.search.clauses.should eq([{:in_classes => [@member_class]}, {:include_facets => [:foo]}])
end
+
+ it "includes facets" do
+ @member_class.model_adapter.should be_kind_of(Xapit::Client::DefaultModelAdapter)
+ end
end
View
23 spec/xapit/client/model_adapters/active_record_adapter_spec.rb
@@ -0,0 +1,23 @@
+if ENV["MODEL_ADAPTER"].nil? || ENV["MODEL_ADAPTER"] == "active_record"
+ require "spec_helper"
+
+ RSpec.configure do |config|
+ config.extend WithModel
+ end
+
+ ActiveRecord::Base.establish_connection(:adapter => "sqlite3", :database => ":memory:")
+
+ describe Xapit::Client::ActiveRecordAdapter do
+ with_model :article do
+ table do |t|
+ t.string "name"
+ end
+ end
+
+ it "is only for active record classes" do
+ Xapit::Client::ActiveRecordAdapter.should_not be_for_class(Object)
+ Xapit::Client::ActiveRecordAdapter.should be_for_class(Article)
+ Xapit::Client::AbstractModelAdapter.adapter_class(Article).should == Xapit::Client::ActiveRecordAdapter
+ end
+ end
+end
View
7 spec/xapit/client/model_adapters/default_model_adapter_spec.rb
@@ -0,0 +1,7 @@
+require "spec_helper"
+
+describe Xapit::Client::DefaultModelAdapter do
+ it "should be default for generic classes" do
+ Xapit::Client::DefaultModelAdapter.adapter_class(Object).should == Xapit::Client::DefaultModelAdapter
+ end
+end

0 comments on commit 027c3f0

Please sign in to comment.