class Friends::Location
Constants
- ALIAS_PREFIX
- SERIALIZATION_PREFIX
Attributes
The number of activities this location is in. This is for internal use only and is set by the Introvert
as needed.
Public Class Methods
@return [Regexp] the string of what we expected during deserialization
# File lib/friends/location.rb, line 22 def self.deserialization_expectation "[Location Name]" end
@return [Regexp] the regex for capturing groups in deserialization
# File lib/friends/location.rb, line 16 def self.deserialization_regex # Note: this regex must be on one line because whitespace is important /(#{SERIALIZATION_PREFIX})?(?<name>[^\(]*[^\(\s])(\s+\(#{ALIAS_PREFIX}(?<alias_str>.+)\))?/ end
@param name [String] the name of the location
# File lib/friends/location.rb, line 27 def initialize(name:, alias_str: nil) @name = name @aliases = alias_str&.split(" #{ALIAS_PREFIX}") || [] end
Public Instance Methods
Add an alias, ignoring duplicates. @param nickname [String] the alias to add
# File lib/friends/location.rb, line 54 def add_alias(nickname) @aliases << nickname @aliases.uniq! end
# File lib/friends/location.rb, line 78 def n_activities defined?(@n_activities) ? @n_activities : 0 end
@return [Array] a list of all regexes to match the name in a string NOTE: Only full names and aliases
# File lib/friends/location.rb, line 71 def regexes_for_name [name, *@aliases].map { |str| Friends::RegexBuilder.regex(str) } end
@param nickname [String] the alias to remove @raise [FriendsError] if the location does not have the given alias
# File lib/friends/location.rb, line 61 def remove_alias(nickname) unless @aliases.include? nickname raise FriendsError, "Alias \"#{nickname}\" not found for \"#{name}\"" end @aliases.delete(nickname) end
@return [String] the file serialization text for the location
# File lib/friends/location.rb, line 36 def serialize Paint.unpaint("#{SERIALIZATION_PREFIX}#{self}") end
@return [String] a string representing the location's name and aliases
# File lib/friends/location.rb, line 41 def to_s unless @aliases.empty? alias_str = " (" + @aliases.map do |nickname| "#{ALIAS_PREFIX}#{Paint[nickname, :bold, :yellow]}" end.join(" ") + ")" end "#{Paint[@name, :bold]}#{alias_str}" end
Private Instance Methods
Default sorting for an array of locations is alphabetical.
# File lib/friends/location.rb, line 85 def <=>(other) name <=> other.name end