Module: Gamefic::Keywords
- Included in:
- Describable, String
- Defined in:
- lib/gamefic/keywords.rb
Constant Summary collapse
- SPLIT_REGEXP =
/[\s]+/
Instance Method Summary collapse
-
#keywords ⇒ Array<String>
Get an array of keywords associated with this object.
-
#specified?(description, fuzzy: false) ⇒ Boolean
Determine if this object matches the provided description.
Instance Method Details
#keywords ⇒ Array<String>
Get an array of keywords associated with this object. The default implementation splits the value of self.to_s into an array.
9 10 11 |
# File 'lib/gamefic/keywords.rb', line 9 def keywords self.to_s.downcase.split(SPLIT_REGEXP).uniq end |
#specified?(description, fuzzy: false) ⇒ Boolean
Determine if this object matches the provided description. In a regular match, every word in the description must be a keyword. Fuzzy matches accept words if a keyword starts with it, e.g., “red” would be a fuzzy match for “reddish.”
32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 |
# File 'lib/gamefic/keywords.rb', line 32 def specified? description, fuzzy: false words = description.split(SPLIT_REGEXP) return false if words.empty? matches = 0 available = keywords words.each { |w| if fuzzy available.each { |k| if k.gsub(/[^a-z0-9]/, '').start_with?(w.downcase.gsub(/[^a-z0-9]/, '')) matches +=1 break end } else matches +=1 if available.include?(w.downcase) end } matches == words.length end |