Sha256: 0d75b2f64a386788c93daebc9392eb0deb47d9c1968825af453813629e763647

Contents?: true

Size: 1.68 KB

Versions: 4

Compression:

Stored size: 1.68 KB

Contents

class Direction
  include Gamefic::Serialize

  attr_writer :adjective, :adverb, :reverse

  # @return [String]
  attr_accessor :name

  def initialize **args
    args.each { |key, value|
      send "#{key}=", value
    }
  end

  # @return [String]
  def adjective
    @adjective || @name
  end

  # @return [String]
  def adverb
    @adverb || @name
  end

  # @return [String]
  def synonyms
    "#{adjective} #{adverb}"
  end

  def reverse
    Direction.find @reverse
  end

  def to_s
    @name
  end

  class << self
    def compass
      @compass ||= {
        north: Direction.new(name: 'north', adjective: 'northern', reverse: :south),
        south: Direction.new(name: 'south', adjective: 'southern', reverse: :north),
        west: Direction.new(name: 'west', adjective: 'western', reverse: :east),
        east: Direction.new(name: 'east', adjective: 'eastern', reverse: :west),
        northwest: Direction.new(name: 'northwest', adjective: 'northwestern', reverse: :southeast),
        southeast: Direction.new(name: 'southeast', adjective: 'southeastern', reverse: :northwest),
        northeast: Direction.new(name: 'northeast', adjective: 'northeastern', reverse: :southwest),
        southwest: Direction.new(name: 'southwest', adjective: 'southwestern', reverse: :northeast),
        up: Direction.new(name: 'up', adjective: 'upwards', reverse: :down),
        down: Direction.new(name: 'down', adjective: 'downwards', reverse: :up)
      }
    end

    # @param dir [Direction, string]
    # @return [Direction, nil]
    def find(dir)
      return dir if dir.is_a?(Direction)
      compass[dir.to_s.downcase.to_sym]
    end
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
gamefic-standard-2.4.0 lib/gamefic-standard/direction.rb
gamefic-standard-2.3.1 lib/gamefic-standard/direction.rb
gamefic-standard-2.3.0 lib/gamefic-standard/direction.rb
gamefic-standard-2.2.0 lib/gamefic-standard/direction.rb