module Ohm::Sorted::ClassMethods

Public Instance Methods

sorted(attribute, options={}) click to toggle source
# File lib/ohm/sorted.rb, line 153
def sorted(attribute, options={})
  sorted_indices << [attribute, options]
end
sorted_find(attribute, dict={}) click to toggle source
# File lib/ohm/sorted.rb, line 161
def sorted_find(attribute, dict={})
  unless sorted_index_exists?(attribute, to_options(dict))
    raise index_not_found(attribute)
  end

  index_key = sorted_index_key(attribute, dict)
  Ohm::SortedSet.new(index_key, key, self)
end
sorted_index_exists?(attribute, options=nil) click to toggle source
# File lib/ohm/sorted.rb, line 170
def sorted_index_exists?(attribute, options=nil)
  !!sorted_indices.detect { |i| i == [attribute, options] }
end
sorted_index_key(attribute, dict={}) click to toggle source
# File lib/ohm/sorted.rb, line 174
def sorted_index_key(attribute, dict={})
  index_key = [key, "sorted", attribute]
  if dict.size == 1
    index_key.concat(dict.first)
  elsif dict.keys.size > 1
    raise ArgumentError
  end
  index_key.join(":")
end
sorted_indices() click to toggle source
# File lib/ohm/sorted.rb, line 157
def sorted_indices
  @sorted_indices ||= []
end

Protected Instance Methods

index_not_found(attribute) click to toggle source
# File lib/ohm/sorted.rb, line 185
def index_not_found(attribute)
  if defined?(IndexNotFound)
    IndexNotFound
  else
    Model::IndexNotFound.new(attribute)
  end
end
to_options(dict) click to toggle source
# File lib/ohm/sorted.rb, line 193
def to_options(dict)
  return {} if dict.empty?
  {group_by: dict.keys.first}
end