class Friends::Location

Constants

ALIAS_PREFIX
SERIALIZATION_PREFIX

Attributes

aliases[R]
n_activities[W]

The number of activities this location is in. This is for internal use only and is set by the Introvert as needed.

name[RW]

Public Class Methods

deserialization_expectation() click to toggle source

@return [Regexp] the string of what we expected during deserialization

# File lib/friends/location.rb, line 22
def self.deserialization_expectation
  "[Location Name]"
end
deserialization_regex() click to toggle source

@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
new(name:, alias_str: nil) click to toggle source

@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_alias(nickname) click to toggle source

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
n_activities() click to toggle source
# File lib/friends/location.rb, line 78
def n_activities
  defined?(@n_activities) ? @n_activities : 0
end
regexes_for_name() click to toggle source

@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
remove_alias(nickname) click to toggle source

@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
serialize() click to toggle source

@return [String] the file serialization text for the location

# File lib/friends/location.rb, line 36
def serialize
  Paint.unpaint("#{SERIALIZATION_PREFIX}#{self}")
end
to_s() click to toggle source

@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

<=>(other) click to toggle source

Default sorting for an array of locations is alphabetical.

# File lib/friends/location.rb, line 85
def <=>(other)
  name <=> other.name
end