Class: NoBrainer::Document::OrmAdapter

Inherits:
OrmAdapter::Base
  • Object
show all
Defined in:
lib/orm_adapter-nobrainer/nobrainer.rb

Instance Method Summary collapse

Instance Method Details

#column_namesObject

Get a list of column/property/field names



11
12
13
# File 'lib/orm_adapter-nobrainer/nobrainer.rb', line 11

def column_names
  klass.fields.keys
end

#create!(attributes = {}) ⇒ Object

Create a model using attributes



55
56
57
# File 'lib/orm_adapter-nobrainer/nobrainer.rb', line 55

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

#destroy(object) ⇒ Object

Destroy an instance by passing in the instance itself.



60
61
62
# File 'lib/orm_adapter-nobrainer/nobrainer.rb', line 60

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


49
50
51
52
# File 'lib/orm_adapter-nobrainer/nobrainer.rb', line 49

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



42
43
44
45
# File 'lib/orm_adapter-nobrainer/nobrainer.rb', line 42

def find_first(options = {})
  conditions, order = extract_conditions!(options)
  klass.where(conditions).order_by(order_to_nql(order)).first
end

#get(id) ⇒ Object

Get an instance by id of the model. Returns nil if a model is not found.



21
22
23
# File 'lib/orm_adapter-nobrainer/nobrainer.rb', line 21

def get(id)
  klass.find(wrap_key(id))
end

#get!(id) ⇒ Object

Get an instance by id of the model. Raises an error if a model is not found.



16
17
18
# File 'lib/orm_adapter-nobrainer/nobrainer.rb', line 16

def get!(id)
  klass.find!(wrap_key(id))
end