module Monocle

Constants

VERSION

Public Instance Methods

cache_click_count(view_type, count) click to toggle source
# File lib/monocle.rb, line 141
def cache_click_count(view_type, count)
  update_column(cache_field_for_click(view_type), count) if respond_to?(:update_column)
  set(cache_field_for_click(view_type), count) if respond_to?(:set)
end
cache_field_for_click(view_type) click to toggle source
# File lib/monocle.rb, line 124
def cache_field_for_click(view_type)
  :"#{view_type}_clicks"
end
cache_field_for_view(view_type) click to toggle source
# File lib/monocle.rb, line 120
def cache_field_for_view(view_type)
  :"#{view_type}_views"
end
cache_view_count(view_type, count) click to toggle source
# File lib/monocle.rb, line 136
def cache_view_count(view_type, count)
  update_column(cache_field_for_view(view_type), count) if respond_to?(:update_column)
  set(cache_field_for_view(view_type), count) if respond_to?(:set)
end
click!() click to toggle source
# File lib/monocle.rb, line 102
def click!
  results = self._monocle_redis_connection.pipelined do
    self._monocle_view_types.keys.each do |view_type|
      self._monocle_redis_connection.hincrby(self.class.monocle_key(id), self.send("#{view_type}_clicks_field"), 1)
    end
    self._monocle_redis_connection.zadd(self.class.monocle_key('recently_clicked'), Time.now.to_i, id)
    self._monocle_redis_connection.zincrby(self.class.monocle_key('click_counts'), 1, id)
  end

  if should_cache_view_count?
    self._monocle_view_types.keys.each_with_index do |view_type, i|
      cache_click_count(view_type, results[i])
    end
    self.update_column(self._monocle_options[:cache_threshold_check_field].to_sym, Time.now) if respond_to?(:update_column)
    self.set(self._monocle_options[:cache_threshold_check_field].to_sym, Time.now) if respond_to?(:set)
  end
end
destroy_views() click to toggle source
# File lib/monocle.rb, line 146
def destroy_views
  self._monocle_redis_connection.del(self.class.monocle_key(id))
end
should_cache_view_count?() click to toggle source
# File lib/monocle.rb, line 128
def should_cache_view_count?
  if self._monocle_options[:cache_view_counts]
    self.send(self._monocle_options[:cache_threshold_check_field]) < (Time.now - self._monocle_options[:cache_threshold])
  else
    false
  end
end
view!() click to toggle source
# File lib/monocle.rb, line 84
def view!
  results = self._monocle_redis_connection.pipelined do
    self._monocle_view_types.keys.each do |view_type|
      self._monocle_redis_connection.hincrby(self.class.monocle_key(id), self.send("#{view_type}_views_field"), 1)
    end
    self._monocle_redis_connection.zadd(self.class.monocle_key('recently_viewed'), Time.now.to_i, id)
    self._monocle_redis_connection.zincrby(self.class.monocle_key('view_counts'), 1, id)
  end

  if should_cache_view_count?
    self._monocle_view_types.keys.each_with_index do |view_type, i|
      cache_view_count(view_type, results[i])
    end
    self.update_column(self._monocle_options[:cache_threshold_check_field].to_sym, Time.now) if respond_to?(:update_column)
    self.set(self._monocle_options[:cache_threshold_check_field].to_sym, Time.now) if respond_to?(:set)
  end
end