Class: Her::Model::OrmAdapter

Inherits:
OrmAdapter::Base
  • Object
show all
Defined in:
lib/orm_adapter/her/adapter.rb

Instance Method Summary collapse

Instance Method Details

#column_namesObject

Get a list of column/property/field names



23
24
25
# File 'lib/orm_adapter/her/adapter.rb', line 23

def column_names
  klass.model_attributes
end

#create!(attributes = {}) ⇒ Object

Create a model using attributes



75
76
77
# File 'lib/orm_adapter/her/adapter.rb', line 75

def create!(attributes = {})
  klass.create(attributes)
end

#destroy(object) ⇒ Object

Destroy an instance by passing in the instance itself.



80
81
82
# File 'lib/orm_adapter/her/adapter.rb', line 80

def destroy(object)
  object.destroy if valid_object?(object)
end

#find_all(options = {}) ⇒ Object

Find all models, optionally matching conditions, and specifying order

See Also:

  • for how to specify order and conditions


69
70
71
72
# File 'lib/orm_adapter/her/adapter.rb', line 69

def find_all(options = {})
  conditions, order = extract_conditions!(options)
  klass.where(conditions).all
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]]



62
63
64
65
# File 'lib/orm_adapter/her/adapter.rb', line 62

def find_first(options = {})
  conditions, order = extract_conditions!(options)
  klass.where(conditions).first
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


41
42
43
# File 'lib/orm_adapter/her/adapter.rb', line 41

def get(id)
  klass.find(klass.primary_key => wrap_key(id))
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


32
33
34
# File 'lib/orm_adapter/her/adapter.rb', line 32

def get!(id)
  klass.find(klass.primary_key => wrap_key(id)) || raise(ArgumentError, "#{klass.name} not found with #{klass.primary_key} of #{wrap_key(id)}")
end