Class: Friends::Location

Inherits:
Object
  • Object
show all
Extended by:
Serializable
Defined in:
lib/friends/location.rb

Constant Summary collapse

SERIALIZATION_PREFIX =
"- "
ALIAS_PREFIX =
"a.k.a. "

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Serializable

deserialize

Constructor Details

#initialize(name:, alias_str: nil) ⇒ Location

Returns a new instance of Location.

Parameters:

  • name (String)

    the name of the location



27
28
29
30
# File 'lib/friends/location.rb', line 27

def initialize(name:, alias_str: nil)
  @name = name
  @aliases = alias_str&.split(" #{ALIAS_PREFIX}") || []
end

Instance Attribute Details

#aliasesObject (readonly)

Returns the value of attribute aliases.



33
34
35
# File 'lib/friends/location.rb', line 33

def aliases
  @aliases
end

#n_activitiesObject



78
79
80
# File 'lib/friends/location.rb', line 78

def n_activities
  defined?(@n_activities) ? @n_activities : 0
end

#nameObject

Returns the value of attribute name.



32
33
34
# File 'lib/friends/location.rb', line 32

def name
  @name
end

Class Method Details

.deserialization_expectationRegexp

Returns the string of what we expected during deserialization.

Returns:

  • (Regexp)

    the string of what we expected during deserialization



22
23
24
# File 'lib/friends/location.rb', line 22

def self.deserialization_expectation
  "[Location Name]"
end

.deserialization_regexRegexp

Returns the regex for capturing groups in deserialization.

Returns:

  • (Regexp)

    the regex for capturing groups in deserialization



16
17
18
19
# 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

Instance Method Details

#add_alias(nickname) ⇒ Object

Add an alias, ignoring duplicates.

Parameters:

  • nickname (String)

    the alias to add



54
55
56
57
# File 'lib/friends/location.rb', line 54

def add_alias(nickname)
  @aliases << nickname
  @aliases.uniq!
end

#regexes_for_nameArray

NOTE: Only full names and aliases

Returns:

  • (Array)

    a list of all regexes to match the name in a string



71
72
73
# File 'lib/friends/location.rb', line 71

def regexes_for_name
  [name, *@aliases].map { |str| Friends::RegexBuilder.regex(str) }
end

#remove_alias(nickname) ⇒ Object

Parameters:

  • nickname (String)

    the alias to remove

Raises:

  • (FriendsError)

    if the location does not have the given alias



61
62
63
64
65
66
67
# 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

#serializeString

Returns the file serialization text for the location.

Returns:

  • (String)

    the file serialization text for the location



36
37
38
# File 'lib/friends/location.rb', line 36

def serialize
  Paint.unpaint("#{SERIALIZATION_PREFIX}#{self}")
end

#to_sString

Returns a string representing the location’s name and aliases.

Returns:

  • (String)

    a string representing the location’s name and aliases



41
42
43
44
45
46
47
48
49
50
# 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