Sha256: c63206a7c5b6b6cb2e682930f222c40cd226182c8508222d0f8c41d289e1d21a

Contents?: true

Size: 1.44 KB

Versions: 4

Compression:

Stored size: 1.44 KB

Contents

require 'yaml'

module Faussaire
  class Cosmos
    DATA_PATH = File.expand_path('../../locale/fr.yml', __dir__)

    class << self
      def initialize_data
        @data = YAML.load_file(DATA_PATH)
      end

      ##
      # Produces a random planet name.
      #
      # @return [String]
      #
      # @example
      #   Faussaire::Cosmos.planet #=> "Mars"
      #
      def planet
        fetch('fr.faussaire.cosmos.planet')
      end

      ##
      # Produces a random cosmic event.
      #
      # @return [String]
      #
      # @example
      #   Faussaire::Cosmos.event #=> "Éclipse solaire"
      #
      def event
        fetch('fr.faussaire.cosmos.event')
      end

      ##
      # Produces a random neighborhood in the cosmos (like a galaxy or nebula).
      #
      # @return [String]
      #
      # @example
      #   Faussaire::Cosmos.neighborhood #=> "Voie lactée"
      #
      def neighborhood
        fetch('fr.faussaire.cosmos.neighborhood')
      end

      ##
      # Produces a random space exploration name.
      #
      # @return [String]
      #
      # @example
      #   Faussaire::Cosmos.exploration #=> "Voyager"
      #
      def exploration
        fetch('fr.faussaire.cosmos.exploration')
      end

      private

      def fetch(key)
        return nil if data.nil? || data.dig(*key.split('.')).nil?
        data.dig(*key.split('.')).sample
      end

      def data
        @data ||= initialize_data
      end
    end
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
faussaire-1.0.0 lib/faussaire/cosmos.rb
faussaire-0.1.6 lib/faussaire/cosmos.rb
faussaire-0.1.3 lib/faussaire/cosmos.rb
faussaire-0.1.1 lib/faussaire/cosmos.rb