Parent

OrmAdapter::Base

Attributes

klass[R]

Public Class Methods

inherited(adapter) click to toggle source

Your ORM adapter needs to inherit from this Base class and its adapter will be registered. To create an adapter you should create an inner constant "OrmAdapter" e.g. ActiveRecord::Base::OrmAdapter

@see orm_adapters/active_record @see orm_adapters/datamapper @see orm_adapters/mongoid

# File lib/orm_adapter/base.rb, line 12
def self.inherited(adapter)
  OrmAdapter.adapters << adapter
  super
end
model_classes() click to toggle source

Gets a list of the available models for this adapter

# File lib/orm_adapter/base.rb, line 18
def self.model_classes
  raise NotImplementedError, "return a list of available models for this adapter"
end
new(klass) click to toggle source
# File lib/orm_adapter/base.rb, line 22
def initialize(klass)
  @klass = klass
end

Public Instance Methods

column_names() click to toggle source

Get a list of column/property/field names

# File lib/orm_adapter/base.rb, line 27
def column_names
  raise NotSupportedError
end
create!(attributes) click to toggle source

Create a model using attributes

# File lib/orm_adapter/base.rb, line 77
def create!(attributes)
  raise NotSupportedError
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/base.rb, line 72
def find_all(options)
  raise NotSupportedError
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/base.rb, line 66
def find_first(options)
  raise NotSupportedError
end
get() click to toggle source

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
# File lib/orm_adapter/base.rb, line 45
def get
  raise NotSupportedError
end
get!(id) click to toggle source

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
# File lib/orm_adapter/base.rb, line 36
def get!(id)
  raise NotSupportedError
end

Protected Instance Methods

extract_conditions_and_order!(options = {}) click to toggle source

given an options hash, with optional :conditions and :order keys, returns conditions and normalized order

# File lib/orm_adapter/base.rb, line 88
def extract_conditions_and_order!(options = {})
  order = normalize_order(options.delete(:order))
  conditions = options.delete(:conditions) || options
  [conditions, order]
end
normalize_order(order) click to toggle source

given an order argument, returns an array of pairs, with each pair containing the attribute, and :asc or :desc

# File lib/orm_adapter/base.rb, line 95
def normalize_order(order)
  order = Array(order)
  
  if order.length == 2 && !order[0].is_a?(Array) && [:asc, :desc].include?(order[1])
    order = [order]
  else
    order = order.map {|pair| pair.is_a?(Array) ? pair : [pair, :asc] }
  end
  
  order.each do |pair|
    pair.length == 2 or raise ArgumentError, "each order clause must be a pair (unknown clause #{pair.inspect})"
    [:asc, :desc].include?(pair[1]) or raise ArgumentError, "order must be specified with :asc or :desc (unknown key #{pair[1].inspect})"
  end
  
  order
end
wrap_key(key) click to toggle source
# File lib/orm_adapter/base.rb, line 83
def wrap_key(key)
  key.is_a?(Array) ? key.first : key
end

[Validate]

Generated with the Darkfish Rdoc Generator 2.