Class: Devise::Activeresource::Adapter
- Inherits:
-
Object
- Object
- Devise::Activeresource::Adapter
- Defined in:
- lib/devise/activeresource/adapter.rb
Overview
Adapter based on orm_adapter interface github.com/ianwhite/orm_adapter/blob/master/lib/orm_adapter/base.rb
Instance Attribute Summary collapse
-
#klass ⇒ Object
readonly
Returns the value of attribute klass.
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.
-
#get(id) ⇒ Object
Get an instance by id of the model.
-
#get!(id) ⇒ Object
Get an instance by id of the model.
-
#initialize(klass) ⇒ Adapter
constructor
A new instance of Adapter.
Constructor Details
#initialize(klass) ⇒ Adapter
Returns a new instance of Adapter.
8 9 10 |
# File 'lib/devise/activeresource/adapter.rb', line 8 def initialize(klass) @klass = klass end |
Instance Attribute Details
#klass ⇒ Object (readonly)
Returns the value of attribute klass.
6 7 8 |
# File 'lib/devise/activeresource/adapter.rb', line 6 def klass @klass end |
Instance Method Details
#column_names ⇒ Object
Get a list of column/property/field names
13 14 15 |
# File 'lib/devise/activeresource/adapter.rb', line 13 def column_names klass.attributes end |
#create!(attributes = {}) ⇒ Object
Create a model using attributes
62 63 64 |
# File 'lib/devise/activeresource/adapter.rb', line 62 def create!(attributes = {}) klass.create(attributes) end |
#destroy(object) ⇒ Object
Destroy an instance by passing in the instance itself.
67 68 69 |
# File 'lib/devise/activeresource/adapter.rb', line 67 def destroy(object) object.destroy end |
#find_all(options = {}) ⇒ Object
Find all models, optionally matching conditions, and specifying order
55 56 57 58 59 |
# File 'lib/devise/activeresource/adapter.rb', line 55 def find_all( = {}) conditions, limit, offset = extract_conditions!() params = conditions.merge(limit_offset_hash(limit, offset)) klass.find(:all, params: params) end |
#find_first(options = {}) ⇒ Object
Find the first instance, optionally matching conditions
You can call with just conditions, providing a hash
User.to_adapter.find_first :name => "Fred", :age => 23
User.to_adapter.find_first :conditions => {:name => "Fred", :age => 23}
45 46 47 48 49 50 51 |
# File 'lib/devise/activeresource/adapter.rb', line 45 def find_first( = {}) conditions, limit, offset = extract_conditions!() params = conditions.merge(limit_offset_hash(limit, offset)) klass.find(:first, params: params) rescue ActiveResource::InvalidRequestError nil 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
31 32 33 34 35 |
# File 'lib/devise/activeresource/adapter.rb', line 31 def get(id) klass.find(wrap_key(id)) rescue ActiveResource::ResourceNotFound nil 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
22 23 24 |
# File 'lib/devise/activeresource/adapter.rb', line 22 def get!(id) klass.find(wrap_key(id)) end |