Module: LeaderboardFactory::ClassMethods

Defined in:
lib/leaderboard_factory.rb

Instance Method Summary collapse

Instance Method Details

#collection_leaderboard(board, options = {}, redis_options = {}) ⇒ Object

As leaderboard, but on the class rather than the instance. it does not take a scope_key parameter, but all other functionality is the same.

Example

class Player < ActiveRecord::Base
  include LeaderboardFactory
  collection_leaderboard 'strength_of_schedule'
end

Player.strength_of_schedule # => the leaderboard object
Player.rank_strength_of_schedule 232, 66.3, { player_name: 'Bob' } # => ["OK", true]

Parameters:

  • board (String)

    the name of the leaderboard

  • scope_key (String/Symbol)

    the name of an attribute on the instance that will scope the leaderboard.

  • options (Hash) (defaults to: {})

    optional; these are passed along to the leaderboard object

  • redis_options (Hash) (defaults to: {})

    optional; these are also passed along to the leaderboard object



73
74
75
76
77
78
79
80
# File 'lib/leaderboard_factory.rb', line 73

def collection_leaderboard board, options = {}, redis_options = {}
  self.leaderboard_specs[:collection][board] = {
    options: options,
    redis_options: redis_options
  }

  define_methods board, :collection, 'self.'
end

#define_methods(board, type = :instance, prefix = '') ⇒ Object

Method what actually does the defining.



83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
# File 'lib/leaderboard_factory.rb', line 83

def define_methods board, type = :instance, prefix = ''
  singluar_object, *remainder_of_name = board.split("_")
  singluar_object = singluar_object.singularize
  accessor_method_name = ([singluar_object] + remainder_of_name).join("_")

  class_eval <<-METHODS, __FILE__, __LINE__
    def #{prefix}#{board}
      return @#{board} if @#{board}
      options               = leaderboard_specs[:#{type}]['#{board}'][:options]
      redis_options         = leaderboard_specs[:#{type}]['#{board}'][:redis_options]
      redis_key             = leaderboard_specs[:#{type}]['#{board}'][:key]

      redis_options.merge!({
        redis_connection: LeaderboardFactory.redis
      })

      @#{board} = Leaderboard.new( leaderboards.board_name('#{board}', redis_key),
                                   Leaderboard::DEFAULT_OPTIONS.merge(options),
                                   redis_options )
    end

    def #{prefix}rank_#{accessor_method_name} object, rank, member_data = nil
      #{board}.rank_member object, rank, member_data
    end
  METHODS
end

#leaderboard(board, scope_key, options = {}, redis_options = {}) ⇒ Object

Defines a new leaderboard that will be scoped to some unique property on the object instance. Use this if you want to, say, track a specific player’s personal bests, or some other

stat that is specific to the individual.

This method defines an accessor that will return the leaderboard, as well as a shorthand

method for ranking a new item in the leaderboard

Example

class Player < ActiveRecord::Base
  include LeaderboardFactory
  leaderboard 'maps_by_wins', :id
end

p = Player.new(id: 77)
p.maps_by_wins # => the player's personal maps leaderboard
p.rank_map_by_wins map.name, 33, { map_id: 42 } # => ["OK", true]

Parameters:

  • board (String)

    the name of the leaderboard

  • scope_key (String/Symbol)

    the name of an attribute on the instance that will scope the leaderboard.

  • options (Hash) (defaults to: {})

    optional; these are passed along to the leaderboard object

  • redis_options (Hash) (defaults to: {})

    optional; these are also passed along to the leaderboard object



46
47
48
49
50
51
52
53
54
# File 'lib/leaderboard_factory.rb', line 46

def leaderboard board, scope_key, options = {}, redis_options = {}
  self.leaderboard_specs[:instance][board] = {
    key: scope_key,
    options: options,
    redis_options: redis_options
  }

  define_methods board, :instance
end