module Horza::Adapters::InstanceMethods

Public Class Methods

new(context) click to toggle source
# File lib/horza/adapters/instance_methods.rb, line 5
def initialize(context)
  @context = context
end

Public Instance Methods

create(options = {}) click to toggle source
# File lib/horza/adapters/instance_methods.rb, line 17
def create(options = {})
  run_quietly { create!(options) }
end
create_as_child(parent_id, options = {}) click to toggle source
# File lib/horza/adapters/instance_methods.rb, line 21
def create_as_child(parent_id, options = {})
  run_quietly { create_as_child!(parent_id, options) }
end
delete(id) click to toggle source
# File lib/horza/adapters/instance_methods.rb, line 25
def delete(id)
  run_quietly { delete!(id) }
end
find_first(options = {}) click to toggle source
# File lib/horza/adapters/instance_methods.rb, line 13
def find_first(options = {})
  run_quietly { find_first!(options) }
end
get(id) click to toggle source
# File lib/horza/adapters/instance_methods.rb, line 9
def get(id)
  run_quietly { get!(id) }
end
update(id, options = {}) click to toggle source
# File lib/horza/adapters/instance_methods.rb, line 29
def update(id, options = {})
  run_quietly { update!(id, options) }
end

Protected Instance Methods

entity(res = @context) click to toggle source
# File lib/horza/adapters/instance_methods.rb, line 47
def entity(res = @context)
  collection?(res) ? ::Horza::Entities.collection_entity_for(entity_symbol, res) :
    ::Horza::Entities.single_entity_for(entity_symbol, res)
end
entity_symbol() click to toggle source
# File lib/horza/adapters/instance_methods.rb, line 52
def entity_symbol
  klass = @context.name.split('::').last
  collection? ? klass.pluralize.symbolize : klass.symbolize
end
extract_conditions!(options = {}) click to toggle source

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

# File lib/horza/adapters/instance_methods.rb, line 60
def extract_conditions!(options = {})
  order      = normalize_order(options.delete(:order))
  limit      = options.delete(:limit)
  offset     = options.delete(:offset)
  conditions = options.delete(:conditions) || options

  [conditions, order, limit, offset]
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/horza/adapters/instance_methods.rb, line 70
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
not_implemented_error() click to toggle source
# File lib/horza/adapters/instance_methods.rb, line 87
def not_implemented_error
  self.class.not_implemented_error
end
run_and_convert_exceptions(&block) click to toggle source

Execute the code block and convert ORM exceptions into Horza exceptions

# File lib/horza/adapters/instance_methods.rb, line 41
def run_and_convert_exceptions(&block)
  block.call
rescue *self.class.expected_errors => e
  raise self.class.horza_error_from_orm_error(e.class).new(e.message)
end
run_quietly(&block) click to toggle source
# File lib/horza/adapters/instance_methods.rb, line 35
def run_quietly(&block)
  block.call
rescue *self.class.expected_horza_errors
end