class Fatboy
Fatboy
is the main class for interacting with the system. It provides a variety of functionality.
Constants
- DAY_FORMAT_STR
Format string we use to store the views per day
- HOUR_FORMAT_STR
Format string we use to store the views per hour
- MONTH_FORMAT_STR
Format string we use to store the views per month
- VERSION
Gem version of
Fatboy
- YEAR_FORMAT_STR
Format string we use to store the views per year
Public Class Methods
Create a new Fatboy
. Options:
* +redis:+ : The redis to store views in. By default, Redis.new
# File lib/fatboy.rb, line 14 def initialize(redis: Redis.new) @redis = redis end
Public Instance Methods
let users view with a shorthand
# File lib/fatboy.rb, line 45 def [](obj) view(obj) end
Many
provides a way to view many different models at once, for speed reasons.
Great if you need to view a lot of models.
# File lib/fatboy.rb, line 22 def many Fatboy::Many.new(@redis) end
This method returns a Fatboy::Popularity
, the main interface for determining the popularity of your models. Example:
fatboy.popular(Image) fatboy.popular("Image") fatboy.popular(model.class)
# File lib/fatboy.rb, line 55 def popular(model) Popularity.new(model, @redis) end
Say that you have viewed an object, making the proper records for hour, day, month, and year.
-
model
- a model of some sort. Should quack like an ActiveRecord model (that is, responding to .id)
# File lib/fatboy.rb, line 32 def view(obj) throw ArgumentError.new("That doesn't quack like a model!") unless obj.respond_to?(:id) stores = Fatboy::Helpers.all_format(Time.now).map do |time| Fatboy::Helpers.format_store(obj.class.to_s, time) end @redis.pipelined do stores.each do |store| inc_member(store, obj.id) end end end
# File lib/fatboy.rb, line 25 def views_for(model) Fatboy::ViewTracker.new(@redis, model) end
Private Instance Methods
# File lib/fatboy.rb, line 71 def inc_member(store, id) @redis.zincrby(store, 1, id) end