module Ohm::Sorted

Public Class Methods

included(model) click to toggle source
# File lib/ohm/sorted.rb, line 148
def self.included(model)
  model.extend(ClassMethods)
end

Protected Instance Methods

add_sorted_indices() click to toggle source
# File lib/ohm/sorted.rb, line 220
def add_sorted_indices
  update_sorted_indices do |key, attribute, options|
    attr = send(attribute)
    if attr
      score = attr.to_f
      db.zadd(key, score, id)
    else
      db.zrem(key, id)
    end
  end
end
after_create() click to toggle source
Calls superclass method
# File lib/ohm/sorted.rb, line 200
def after_create
  add_sorted_indices
  super
end
after_update() click to toggle source
Calls superclass method
# File lib/ohm/sorted.rb, line 210
def after_update
  add_sorted_indices unless new?
  super
end
before_delete() click to toggle source
Calls superclass method
# File lib/ohm/sorted.rb, line 215
def before_delete
  remove_sorted_indices
  super
end
before_update() click to toggle source
Calls superclass method
# File lib/ohm/sorted.rb, line 205
def before_update
  prune_sorted_indices
  super
end
prune_sorted_indices() click to toggle source
# File lib/ohm/sorted.rb, line 238
def prune_sorted_indices
  return if new?
  update_sorted_indices do |key, attribute, options|
    return unless options.include?(:group_by)

    old_value = db.hget(self.key, options[:group_by])
    new_value = send(options[:group_by])

    if old_value != new_value
      opts = {options[:group_by] => old_value}
      key = self.class.sorted_index_key(attribute, opts)
      db.zrem(key, id)
    end
  end
end
remove_sorted_indices() click to toggle source
# File lib/ohm/sorted.rb, line 232
def remove_sorted_indices
  update_sorted_indices do |key, attribute, options|
    db.zrem(key, id)
  end
end
update_sorted_indices() { |key, attribute, options| ... } click to toggle source
# File lib/ohm/sorted.rb, line 254
def update_sorted_indices
  self.class.sorted_indices.each do |attribute, options|
    opts = {}
    if options.include?(:group_by)
      group_by = options[:group_by]
      opts[group_by] = send(group_by)
    end
    key = self.class.sorted_index_key(attribute, opts)

    yield(key, attribute, options)
  end
end