Module: LeaderboardFactory::Helpers
- Included in:
- HelperProxy
- Defined in:
- lib/leaderboard_factory/helpers.rb
Overview
This system is in the most flux. These are some helpers that I found very, ahem, helpful. I’m just not sure I’m sold on the API, and I know that there will be more.
Instance Method Summary collapse
- #board_name(name, key = nil) ⇒ Object
-
#get(leaderboard) ⇒ Object
returns the given leaderboard.
-
#pivot(leaderboard, options) ⇒ Object
Take the leaderboard and pivot the data by the score.
-
#top(leaderboard, options = {}) ⇒ Object
Takes the top item off of the pivot and optionally passes the result through a callable mapper.
Instance Method Details
#board_name(name, key = nil) ⇒ Object
5 6 7 8 9 |
# File 'lib/leaderboard_factory/helpers.rb', line 5 def board_name name, key = nil name = name.gsub('_', '-') name += "-#{context.send(key)}" if key name end |
#get(leaderboard) ⇒ Object
returns the given leaderboard
Example
leaderboards.get(:personal_bests) # => Leaderboard
80 81 82 |
# File 'lib/leaderboard_factory/helpers.rb', line 80 def get leaderboard context.send(leaderboard) end |
#pivot(leaderboard, options) ⇒ Object
Take the leaderboard and pivot the data by the score. By default it will group the members by their score.
Available options
+:member_data+ whether or not to include member data
+:score+ a callable object that will transform the score
+:pluck+ the name of a +member_data+ attribute. If you don't want all of the data.
Examples
leaderboards.pivot(:personal_bests) # => { 16.0 => ['Event ID 1', 'Event ID 3'], 14.2 => ['Event ID 2'] }
leaderboards.pivot(:personal_bests, member_data: true)
# => { 16.0 => [{ 'event_name' => 'My Event Name', 'timestamp' => '12323425' },
{ 'event_name' => 'My Third Attempt', 'timestamp' => '12323488' } ], ...
leaderboards.pivot(:personal_bests, member_data: true, pluck: 'event_name')
# => { 16.0 => ['My Event Name', 'My Third Attempt'], 14.2 => ['Followup'] }
leaderboards.pivot(:personal_bests, score: Proc.new { |s| s.to_i })
# => { 16 => ['Event ID 1', 'Event ID 3'], 14 => ['Event ID 2'] }
53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 |
# File 'lib/leaderboard_factory/helpers.rb', line 53 def pivot leaderboard, = {} if [:member_data] .merge!(with_member_data: true) end context.send(leaderboard).all_members().inject({}) do |buf, member| key = [:score] ? [:score].call(member[:score]) : member[:score] buf[key] ||= [] item = if [:pluck] member[:member_data][[:pluck]] elsif [:member_data] member[:member_data] else member[:member] end buf[key] << item buf end end |
#top(leaderboard, options = {}) ⇒ Object
Takes the top item off of the pivot and optionally passes the result through a callable mapper.
Example
leaderboards.top(:personal_bests) # => [16.0, ['Event ID 1', 'Event ID 3']]
leaderboards.top(:personal_bests, mapper: Proc.new do |score, bests|
{ high_score: score.to_i, tied: bests.size, best_events: bests }
end) # => { high_score: 16, tied: 2, best_events: ['Event ID 1', 'Event ID 3'] }
22 23 24 25 26 27 28 29 |
# File 'lib/leaderboard_factory/helpers.rb', line 22 def top leaderboard, = {} result = pivot(leaderboard, ).first if [:mapper] [:mapper].call(*result) else result end end |