Class: Lolita::DBI::Base

Inherits:
Object show all
Defined in:
lib/lolita/dbi/base.rb

Overview

Lolita::DBI::Base is ORM Interface class, that handle the request to ORM classes. Depending on given class DBI::Base detect which ORM class is used and include right adapter for that class. Other Lolita classes that need to manipulate with data need to have dbi object or it can be created in that class. Lolita::DBI::Base support Mongoid and ActiveRecord::Base, or details see Lolita::Adapter.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(class_object) ⇒ Base

Expect ORM class that is supported by Lolita. See Adapter for available adapters.



20
21
22
23
24
# File 'lib/lolita/dbi/base.rb', line 20

def initialize(class_object) 
  @klass=class_object
  detect_adapter
  connect_adapter
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(metod, *args, &block) ⇒ Object



42
43
44
# File 'lib/lolita/dbi/base.rb', line 42

def method_missing(metod,*args,&block)
  @adapter.send(metod,*args,&block)
end

Instance Attribute Details

#adapterObject (readonly)

connected Adaptee for adapter



17
18
19
# File 'lib/lolita/dbi/base.rb', line 17

def adapter
  @adapter
end

#adapter_nameObject (readonly)

return connected adapter name



15
16
17
# File 'lib/lolita/dbi/base.rb', line 15

def adapter_name
  @adapter_name
end

#klassObject (readonly)

return related orm class object



16
17
18
# File 'lib/lolita/dbi/base.rb', line 16

def klass
  @klass
end

Class Method Details

.adaptersObject

Return Array of available adapters.



48
49
50
51
52
# File 'lib/lolita/dbi/base.rb', line 48

def adapters
  Dir[File.expand_path(File.join(File.dirname(__FILE__),'..','adapter','**','*.rb'))].map {|f|
    File.basename(f,".rb").to_sym
  }.reject{|el| el==:abstract_adapter}
end

.create(class_object) ⇒ Object



10
11
12
13
# File 'lib/lolita/dbi/base.rb', line 10

def self.create(class_object)
  dbi = self.new(class_object)
  return dbi.adapter
end

Instance Method Details

#connect_adapterObject

Connect Adapter by including adapter module into DBI::Base class.



38
39
40
# File 'lib/lolita/dbi/base.rb', line 38

def connect_adapter()
  @adapter="Lolita::Adapter::#{self.adapter_name.to_s.camelize}".constantize.new(self)
end

#detect_adapterObject

Detect which ORM class is given and based on it connect Adapter.



27
28
29
30
31
32
33
34
35
# File 'lib/lolita/dbi/base.rb', line 27

def detect_adapter
  if defined?(Mongoid) && defined?(Mongoid::Document) && self.klass.ancestors.include?(Mongoid::Document)
    @adapter_name=:mongoid
  elsif defined?(ActiveRecord) && defined?(ActiveRecord::Base) && self.klass.ancestors.include?(ActiveRecord::Base)
    @adapter_name=:active_record
  else
    raise NotORMClassError.new("Lolita::DBI::Base can not find appropriate #{self.klass} class adapter.")
  end
end