Skip to content
This repository has been archived by the owner on Dec 12, 2021. It is now read-only.

Commit

Permalink
adding model adapter
Browse files Browse the repository at this point in the history
  • Loading branch information
ryanb committed Jul 28, 2011
1 parent b7e8d09 commit 027c3f0
Show file tree
Hide file tree
Showing 9 changed files with 94 additions and 12 deletions.
27 changes: 15 additions & 12 deletions Gemfile
Original file line number Diff line number Diff line change
@@ -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
3 changes: 3 additions & 0 deletions lib/xapit.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down
4 changes: 4 additions & 0 deletions lib/xapit/client/membership.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
22 changes: 22 additions & 0 deletions lib/xapit/client/model_adapters/abstract_model_adapter.rb
Original file line number Diff line number Diff line change
@@ -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
9 changes: 9 additions & 0 deletions lib/xapit/client/model_adapters/active_record_adapter.rb
Original file line number Diff line number Diff line change
@@ -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
7 changes: 7 additions & 0 deletions lib/xapit/client/model_adapters/default_model_adapter.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
module Xapit
module Client
class DefaultModelAdapter < AbstractModelAdapter
# This adapter is used when no matching adapter is found
end
end
end
4 changes: 4 additions & 0 deletions spec/xapit/client/membership_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
23 changes: 23 additions & 0 deletions spec/xapit/client/model_adapters/active_record_adapter_spec.rb
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
@@ -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.