Class: OrmAdapter::Base

Inherits:
Object
  • Object
show all
Defined in:
lib/orm_adapter/base.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(klass) ⇒ Base

Returns a new instance of Base.



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

def initialize(klass)
  @klass = klass
end

Instance Attribute Details

#klassObject (readonly)

Returns the value of attribute klass.



3
4
5
# File 'lib/orm_adapter/base.rb', line 3

def klass
  @klass
end

Class Method Details

.inherited(adapter) ⇒ Object

Your ORM adapter needs to inherit from this Base class and its adapter will be registered. To create an adapter you should create an inner constant “OrmAdapter” e.g. ActiveRecord::Base::OrmAdapter

See Also:

  • orm_adapters/active_record
  • orm_adapters/datamapper
  • orm_adapters/mongoid


12
13
14
15
# File 'lib/orm_adapter/base.rb', line 12

def self.inherited(adapter)
  OrmAdapter.adapters << adapter
  super
end

Instance Method Details

#column_namesObject

Get a list of column/property/field names

Raises:



22
23
24
# File 'lib/orm_adapter/base.rb', line 22

def column_names
  raise NotSupportedError
end

#create!(attributes = {}) ⇒ Object

Create a model using attributes

Raises:



72
73
74
# File 'lib/orm_adapter/base.rb', line 72

def create!(attributes = {})
  raise NotSupportedError
end

#destroy(object) ⇒ Object

Destroy an instance by passing in the instance itself.

Raises:



77
78
79
# File 'lib/orm_adapter/base.rb', line 77

def destroy(object)
  raise NotSupportedError
end

#find_all(options = {}) ⇒ Object

Find all models, optionally matching conditions, and specifying order



67
68
69
# File 'lib/orm_adapter/base.rb', line 67

def find_all(options = {})
  raise NotSupportedError
end

#find_first(options = {}) ⇒ Object

Find the first instance, optionally matching conditions, and specifying order

You can call with just conditions, providing a hash

User.to_adapter.find_first :name => "Fred", :age => 23

Or you can specify :order, and :conditions as keys

User.to_adapter.find_first :conditions => {:name => "Fred", :age => 23}
User.to_adapter.find_first :order => [:age, :desc]
User.to_adapter.find_first :order => :name, :conditions => {:age => 18}

When specifying :order, it may be

  • a single arg e.g. :order => :name

  • a single pair with :asc, or :desc as last, e.g. :order => [:name, :desc]

  • an array of single args or pairs (with :asc or :desc as last), e.g. :order => [[:name, :asc], [:age, :desc]]

Raises:



61
62
63
# File 'lib/orm_adapter/base.rb', line 61

def find_first(options = {})
  raise NotSupportedError
end

#get(id) ⇒ Object

Get an instance by id of the model. Returns nil if a model is not found. This should comply with ActiveModel#to_key API, i.e.:

User.to_adapter.get(@user.to_key) == @user

Raises:



40
41
42
# File 'lib/orm_adapter/base.rb', line 40

def get(id)
  raise NotSupportedError
end

#get!(id) ⇒ Object

Get an instance by id of the model. Raises an error if a model is not found. This should comply with ActiveModel#to_key API, i.e.:

User.to_adapter.get!(@user.to_key) == @user

Raises:



31
32
33
# File 'lib/orm_adapter/base.rb', line 31

def get!(id)
  raise NotSupportedError
end