class NoBrainer::Document::OrmAdapter

Public Instance Methods

column_names() click to toggle source

get a list of column names for a given class

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

@see OrmAdapter::Base#create!

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

@see OrmAdapter::Base#destroy

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

@see OrmAdapter::Base#find_all

# File lib/orm_adapter/adapters/nobrainer.rb, line 33
def find_all(options = {})
  conditions, order, limit, offset = extract_conditions!(options)
  klass.where(conditions_to_fields(conditions)).order_by(*order_clause(order)).limit(limit).offset(offset)
end
find_first(options = {}) click to toggle source

@see OrmAdapter::Base#find_first

# File lib/orm_adapter/adapters/nobrainer.rb, line 27
def find_first(options = {})
  conditions, order = extract_conditions!(options)
  klass.where(conditions_to_fields(conditions)).order_by(*order_clause(order)).first
end
get(id) click to toggle source

@see OrmAdapter::Base#get

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

@see OrmAdapter::Base#get!

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

Protected Instance Methods

conditions_to_fields(conditions) click to toggle source

converts and documents to ids

# File lib/orm_adapter/adapters/nobrainer.rb, line 51
def conditions_to_fields(conditions)
  conditions.inject({}) do |fields, (key, value)|
    if value.is_a?(NoBrainer::Document) && column_names.include?(:"#{key}_id")
      fields.merge(:"#{key}_id" => value.id)
    else
      fields.merge(key => value)
    end
  end
end
order_clause(order) click to toggle source
# File lib/orm_adapter/adapters/nobrainer.rb, line 61
def order_clause(order)
  order.map { |k,v| {k => v} }
end