Class: OrmAdapter::Base
- Inherits:
-
Object
- Object
- OrmAdapter::Base
- Defined in:
- lib/orm_adapter/base.rb
Direct Known Subclasses
DataMapper::Resource::OrmAdapter, MongoMapper::Document::OrmAdapter, Mongoid::Document::OrmAdapter, ActiveRecord
Instance Attribute Summary collapse
-
#klass ⇒ Object
readonly
Returns the value of attribute klass.
Class Method Summary collapse
-
.inherited(adapter) ⇒ Object
Your ORM adapter needs to inherit from this Base class and its adapter will be registered.
Instance Method Summary collapse
-
#column_names ⇒ Object
Get a list of column/property/field names.
-
#create!(attributes = {}) ⇒ Object
Create a model using attributes.
-
#destroy(object) ⇒ Object
Destroy an instance by passing in the instance itself.
-
#find_all(options = {}) ⇒ Object
Find all models, optionally matching conditions, and specifying order.
-
#find_first(options = {}) ⇒ Object
Find the first instance, optionally matching conditions, and specifying order.
-
#get(id) ⇒ Object
Get an instance by id of the model.
-
#get!(id) ⇒ Object
Get an instance by id of the model.
-
#initialize(klass) ⇒ Base
constructor
A new instance of Base.
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
#klass ⇒ Object (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
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_names ⇒ Object
Get a list of column/property/field names
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
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.
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( = {}) 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]]
61 62 63 |
# File 'lib/orm_adapter/base.rb', line 61 def find_first( = {}) 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
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
31 32 33 |
# File 'lib/orm_adapter/base.rb', line 31 def get!(id) raise NotSupportedError end |