class NoBrainer::Document::OrmAdapter

Public Instance Methods

column_names() click to toggle source

Get a list of column/property/field names

# File lib/orm_adapter-nobrainer/nobrainer.rb, line 11
def column_names
  klass.fields.keys
end
create!(attributes = {}) click to toggle source

Create a model using attributes

# File lib/orm_adapter-nobrainer/nobrainer.rb, line 55
def create!(attributes = {})
  klass.create!(attributes)
end
destroy(object) click to toggle source

Destroy an instance by passing in the instance itself.

# File lib/orm_adapter-nobrainer/nobrainer.rb, line 60
def destroy(object)
  object.destroy if valid_object?(object)
end
find_all(options = {}) click to toggle source

Find all models, optionally matching conditions, and specifying order @see OrmAdapter::Base#find_first for how to specify order and conditions

# 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 = {}) click to toggle source

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]]

# 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) click to toggle source

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

# File lib/orm_adapter-nobrainer/nobrainer.rb, line 21
def get(id)
  klass.find(wrap_key(id))
end
get!(id) click to toggle source

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

# File lib/orm_adapter-nobrainer/nobrainer.rb, line 16
def get!(id)
  klass.find!(wrap_key(id))
end

Protected Instance Methods

order_to_nql(order) click to toggle source

NoBrainer does not accept arrays as an ‘order_by` argument.

# File lib/orm_adapter-nobrainer/nobrainer.rb, line 66
def order_to_nql(order)
  if order.empty?
    nil
  else
    order.to_h
  end
end