Module: CanTango::Adaptor

Defined in:
lib/cantango/adaptor.rb,
lib/cantango/adaptor/mongo.rb,
lib/cantango/adaptor/generic.rb,
lib/cantango/adaptor/mongoid.rb,
lib/cantango/adaptor/relational.rb,
lib/cantango/adaptor/data_mapper.rb,
lib/cantango/adaptor/mongo_mapper.rb,
lib/cantango/adaptor/active_record.rb

Defined Under Namespace

Modules: ActiveRecord, DataMapper, Generic, Mongo, MongoMapper, Mongoid, Relational

Instance Method Summary collapse

Instance Method Details

#adaptor_for(object) ⇒ Object



11
12
13
# File 'lib/cantango/adaptor.rb', line 11

def adaptor_for object
  "CanTango::Adaptor::#{orm_for(object).to_s.camelize}".constantize
end

#matching_orm(object) ⇒ Object



19
20
21
22
23
24
25
26
27
28
29
# File 'lib/cantango/adaptor.rb', line 19

def matching_orm object
  orm = orm_map.select do |orm, const|
    begin
      orm_const = const.constantize
      object.kind_of?(orm_const)
    rescue
      next
    end
  end
  orm.empty? ? nil : orm.keys.first
end

#orm_for(object) ⇒ Object



15
16
17
# File 'lib/cantango/adaptor.rb', line 15

def orm_for object
  matching_orm(object) || :generic
end

#orm_mapObject



31
32
33
34
35
36
37
38
# File 'lib/cantango/adaptor.rb', line 31

def orm_map
  {
    :active_record  => "ActiveRecord::Base",
    :data_mapper    => "DataMapper::Resource",
    :mongoid        => "Mongoid::Document",
    :mongo_mapper   => "MongoMapper::Document"
  }
end

#use_adaptor!(base, object) ⇒ Object

include adaptor depending on which ORM the object inherits from or includes



7
8
9
# File 'lib/cantango/adaptor.rb', line 7

def use_adaptor! base, object
  base.class.send :include, adaptor_for(object)
end