Module: Monocle

Extended by:
ActiveSupport::Concern
Defined in:
lib/monocle.rb,
lib/monocle/server.rb,
lib/monocle/version.rb

Defined Under Namespace

Modules: ClassMethods Classes: Server

Constant Summary collapse

VERSION =
'0.2.6'

Instance Method Summary collapse

Instance Method Details

#cache_click_count(view_type, count) ⇒ Object



141
142
143
144
# 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) ⇒ Object



124
125
126
# File 'lib/monocle.rb', line 124

def cache_field_for_click(view_type)
  :"#{view_type}_clicks"
end

#cache_field_for_view(view_type) ⇒ Object



120
121
122
# File 'lib/monocle.rb', line 120

def cache_field_for_view(view_type)
  :"#{view_type}_views"
end

#cache_view_count(view_type, count) ⇒ Object



136
137
138
139
# 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!Object



102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
# 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_viewsObject



146
147
148
# File 'lib/monocle.rb', line 146

def destroy_views
  self._monocle_redis_connection.del(self.class.monocle_key(id))
end

#should_cache_view_count?Boolean

Returns:

  • (Boolean)


128
129
130
131
132
133
134
# 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!Object



84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
# 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