-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
4752e76
commit 44deb86
Showing
12 changed files
with
277 additions
and
215 deletions.
There are no files selected for viewing
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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
class Champion < Collection | ||
include ActiveModel::Validations | ||
CHAMPION_NAMES = Rails.cache.read(:champions).values.map { |data| data[:name] } | ||
ACCESSORS = [ | ||
:name, :roles, :stats, :tags, :title, :passive, :spells, :allytips, | ||
:enemytips, :key | ||
].freeze | ||
ACCESSORS.each do |accessor| | ||
attr_accessor accessor | ||
end | ||
|
||
validates :name, presence: true | ||
validates :name, inclusion: { in: CHAMPION_NAMES } | ||
|
||
def initialize(attributes = {}) | ||
@name = attributes[:name].strip | ||
if data = Rails.cache.read(name: @name) || match_collection(@name, CHAMPION_NAMES) | ||
ACCESSORS.each do |key| | ||
instance_variable_set("@#{key}", data[key]) | ||
end | ||
end | ||
end | ||
|
||
def find_by_role(role) | ||
if role.blank? | ||
return @roles.length == 1 ? @roles.first : nil | ||
end | ||
|
||
@roles.detect do |role_data| | ||
role_data[:role] == role | ||
end | ||
end | ||
|
||
def error_message | ||
errors.messages.map do |key, value| | ||
"#{key} #{value.first}" | ||
end.en.conjunction(article: false) | ||
end | ||
end |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
class Collection | ||
SIMILARITY_THRESHOLD = 0.7 | ||
|
||
def match_collection(name, collection) | ||
collection_key = self.class.to_s.downcase.pluralize.to_sym | ||
matcher = Matcher::Matcher.new(name) | ||
|
||
search_key = Hash.new | ||
if match = matcher.find_match(collection, SIMILARITY_THRESHOLD) | ||
search_key[collection_key] = match.result | ||
Rails.cache.read(search_key) | ||
end | ||
end | ||
end |
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
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
Oops, something went wrong.