Module: Gamefic::Keywords

Included in:
Describable, String
Defined in:
lib/gamefic/keywords.rb

Constant Summary collapse

SPLIT_REGEXP =
/[\s]+/

Instance Method Summary collapse

Instance Method Details

#keywordsArray<String>

Get an array of keywords associated with this object. The default implementation splits the value of self.to_s into an array.

Returns:



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.”

Examples:

dog = "big red dog"
dog.extend Gamefic::Matchable

dog.specified?("red dog")  #=> true
dog.specified?("gray dog") #=> false
dog.specified?("red do")   #=> false

dog.specified?("re do", fuzzy: true)  #=> true
dog.specified?("red og", fuzzy: true) #=> false

Parameters:

  • description (String)

    The description to be compared

  • fuzzy (Boolean) (defaults to: false)

    Use fuzzy matching (default is false)

Returns:

  • (Boolean)


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