Created
March 31, 2012 18:57
-
-
Save charliemoseley/2267539 to your computer and use it in GitHub Desktop.
Starting point
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'digest/sha1' | |
class EmoteList < Array | |
attr_accessor :type, :sort, :tags, :user | |
attr_reader :randomized | |
def assign_properties(properties = {}) | |
@type = properties[:type] | |
@sort = properties[:sort] | |
@tags = properties[:tags] | |
@user = properties[:user] | |
end | |
def most_recently_updated | |
self.sort {| a, b | a.updated_at <=> b.updated_at } | |
end | |
def randomize! | |
self.sort_by! { rand } | |
@randomized = true | |
end | |
def cache_key | |
raise NoEmoteListTypeSpecifiedError if @type.nil? | |
raise NoEmoteListSortSpecifiedError if @sort.nil? | |
key = "emote_list/#{@type}/#{sort}/" | |
key += cachify_key_tags | |
key += cachify_key_user | |
key += self.most_recently_updated.updated_at | |
end | |
private | |
def cachify_key_tags | |
return '' if @tags.nil? | |
return (Digest::SHA1.hexdigest @tags) + "/" | |
end | |
def cachify_key_user | |
return '' if @user.nil? | |
return @user.id.to_s + "/" | |
end | |
end | |
class NoEmoteListTypeSpecifiedError < StandardError | |
end | |
class NoEmoteListSortSpecifiedError < StandardError | |
end | |
class CantCacheRandomizedEmoteList < StandardError | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment