Sha256: 8f298343b9e11e0b7d947aa4731d7ce1ddc4f3f970e4e7174614c4d0ed219283

Contents?: true

Size: 1.42 KB

Versions: 17

Compression:

Stored size: 1.42 KB

Contents

# frozen_string_literal: true

# Location represents a location in the world.

require "friends/regex_builder"
require "friends/serializable"

module Friends
  class Location
    extend Serializable

    SERIALIZATION_PREFIX = "- ".freeze

    # @return [Regexp] the regex for capturing groups in deserialization
    def self.deserialization_regex
      # Note: this regex must be on one line because whitespace is important
      /(#{SERIALIZATION_PREFIX})?(?<name>.+)/
    end

    # @return [Regexp] the string of what we expected during deserialization
    def self.deserialization_expectation
      "[Location Name]"
    end

    # @param name [String] the name of the location
    def initialize(name:)
      @name = name
    end

    attr_accessor :name

    # @return [String] the file serialization text for the location
    def serialize
      "#{SERIALIZATION_PREFIX}#{@name}"
    end

    # @return [Regexp] the regex used to match this location's name in an
    #   activity description
    def regex_for_name
      Friends::RegexBuilder.regex(@name)
    end

    # The number of activities this location is in. This is for internal use
    # only and is set by the Introvert as needed.
    attr_writer :n_activities
    def n_activities
      defined?(@n_activities) ? @n_activities : 0
    end

    private

    # Default sorting for an array of locations is alphabetical.
    def <=>(other)
      name <=> other.name
    end
  end
end

Version data entries

17 entries across 17 versions & 1 rubygems

Version Path
friends-0.50 lib/friends/location.rb
friends-0.49 lib/friends/location.rb
friends-0.48 lib/friends/location.rb
friends-0.47 lib/friends/location.rb
friends-0.46 lib/friends/location.rb
friends-0.45 lib/friends/location.rb
friends-0.44 lib/friends/location.rb
friends-0.43 lib/friends/location.rb
friends-0.42 lib/friends/location.rb
friends-0.41 lib/friends/location.rb
friends-0.40 lib/friends/location.rb
friends-0.39 lib/friends/location.rb
friends-0.38 lib/friends/location.rb
friends-0.37 lib/friends/location.rb
friends-0.36 lib/friends/location.rb
friends-0.35 lib/friends/location.rb
friends-0.34 lib/friends/location.rb