module ORMivore::Port

Attributes

adapter[R]

Public Class Methods

included(base) click to toggle source
# File lib/ormivore/port.rb, line 24
def self.included(base)
  base.extend(ClassMethods)
end
new(adapter) click to toggle source

a good place to add generic storage functionality, like ‘around’ logging/performance monitoring/notifications/etc first obvious candidate is exception handling

# File lib/ormivore/port.rb, line 31
def initialize(adapter)
  @adapter = adapter
end

Public Instance Methods

create(attrs) click to toggle source
# File lib/ormivore/port.rb, line 42
def create(attrs)
  begin
    adapter.create(attrs)
  rescue => e
    raise ORMivore::StorageError, e.message
  end
end
find(conditions, attributes_to_load, options = {}) click to toggle source
# File lib/ormivore/port.rb, line 35
def find(conditions, attributes_to_load, options = {})
  # TODO verify conditions to contain only keys that match attribute names and value of proper type
  validate_finder_options(options, attributes_to_load)

  adapter.find(conditions, attributes_to_load, options)
end
update(attrs, conditions) click to toggle source
# File lib/ormivore/port.rb, line 50
def update(attrs, conditions)
  adapter.update(attrs, conditions)
rescue => e
  raise ORMivore::StorageError, e.message
end

Private Instance Methods

validate_finder_options(options, attributes_to_load) click to toggle source

def attributes

self.class.attributes

end

def validate_conditions(conditions)

extra = conditions.keys - attributes.keys
raise BadConditionsError, extra.join("\n") unless extra.empty?

end

# File lib/ormivore/port.rb, line 71
def validate_finder_options(options, attributes_to_load)
  options = options.dup
  valid = true

  # TODO how about other finder options, like limit and offset?
  order = options.delete(:order) || {}
  valid = false unless options.empty?

  raise ORMivore::BadArgumentError, "Invalid finder options #{options.inspect}" unless valid

  validate_order(order, attributes_to_load)

  nil
end
validate_order(order, attributes_to_load) click to toggle source
# File lib/ormivore/port.rb, line 86
def validate_order(order, attributes_to_load)
  # TODO matching agains attributes_to_load is not good, sometimes user wants to sort on non-loaded attribute
  return if order.empty?

  unless order.keys.all? { |k| attributes_to_load.include?(k) }
    raise BadArgumentError, "Invalid order key in #{order.inspect}"
  end
end