Module: CanTango::Rules::Adaptor

Included in:
UserRelation
Defined in:
lib/cantango/rules/adaptor.rb,
lib/cantango/rules/adaptor/mongo.rb,
lib/cantango/rules/adaptor/generic.rb,
lib/cantango/rules/adaptor/relational.rb,
lib/cantango/rules/adaptor/data_mapper.rb,
lib/cantango/rules/adaptor/active_record.rb

Defined Under Namespace

Modules: ActiveRecord, DataMapper, Generic, Mongo, Relational

Instance Method Summary collapse

Instance Method Details

#adaptor_for(orm) ⇒ Object



22
23
24
# File 'lib/cantango/rules/adaptor.rb', line 22

def adaptor_for orm
  "CanTango::Rules::Adaptor::#{orm.to_s.camelize}".constantize
end

#get_adapter(object, adaptor_class, orm) ⇒ Object



18
19
20
# File 'lib/cantango/rules/adaptor.rb', line 18

def get_adapter object, adaptor_class, orm
  object.kind_of?(adaptor_class) ? adaptor_for(orm) : adaptor_for(:generic)
end

#orm_mapObject



26
27
28
29
30
31
32
33
# File 'lib/cantango/rules/adaptor.rb', line 26

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



8
9
10
11
12
13
14
15
16
# File 'lib/cantango/rules/adaptor.rb', line 8

def use_adaptor! base, object
  orm_map.each_pair do |orm, const|
    begin
      base.class.send :include, get_adapter(object, const.constantize, orm)
    rescue
      next
    end
  end
end