Class: Friends::Location
- Inherits:
-
Object
- Object
- Friends::Location
- Extended by:
- Serializable
- Defined in:
- lib/friends/location.rb
Constant Summary collapse
- SERIALIZATION_PREFIX =
"- "
- ALIAS_PREFIX =
"a.k.a. "
Instance Attribute Summary collapse
-
#aliases ⇒ Object
readonly
Returns the value of attribute aliases.
- #n_activities ⇒ Object
-
#name ⇒ Object
Returns the value of attribute name.
Class Method Summary collapse
-
.deserialization_expectation ⇒ Regexp
The string of what we expected during deserialization.
-
.deserialization_regex ⇒ Regexp
The regex for capturing groups in deserialization.
Instance Method Summary collapse
-
#add_alias(nickname) ⇒ Object
Add an alias, ignoring duplicates.
-
#initialize(name:, alias_str: nil) ⇒ Location
constructor
A new instance of Location.
-
#regexes_for_name ⇒ Array
NOTE: Only full names and aliases.
- #remove_alias(nickname) ⇒ Object
-
#serialize ⇒ String
The file serialization text for the location.
-
#to_s ⇒ String
A string representing the location’s name and aliases.
Methods included from Serializable
Constructor Details
#initialize(name:, alias_str: nil) ⇒ Location
Returns a new instance of 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
#aliases ⇒ Object (readonly)
Returns the value of attribute aliases.
33 34 35 |
# File 'lib/friends/location.rb', line 33 def aliases @aliases end |
#n_activities ⇒ Object
78 79 80 |
# File 'lib/friends/location.rb', line 78 def n_activities defined?(@n_activities) ? @n_activities : 0 end |
#name ⇒ Object
Returns the value of attribute name.
32 33 34 |
# File 'lib/friends/location.rb', line 32 def name @name end |
Class Method Details
.deserialization_expectation ⇒ Regexp
Returns 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_regex ⇒ Regexp
Returns 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.
54 55 56 57 |
# File 'lib/friends/location.rb', line 54 def add_alias(nickname) @aliases << nickname @aliases.uniq! end |
#regexes_for_name ⇒ Array
NOTE: Only full names and aliases
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
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 |
#serialize ⇒ String
Returns 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_s ⇒ String
Returns 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 |