Class: Adventure::Starter

Inherits:
Object
  • Object
show all
Includes:
Adventure, TTY::Exit, TTY::Option
Defined in:
lib/adventure.rb

Overview

CLI starter.

Constant Summary collapse

ADVENTURE_CLASSES =

List of all entity classes.

[].freeze

Constants included from Adventure

AUTHOR, AUTHOR_EMAIL, AUTHOR_URI, DESCRIPTION, LICENSE, NAME, SLUG, VERSION, YEAR

Instance Method Summary collapse

Instance Method Details

#compiled_paramsObject (private)

Compile TTY::Option parameters into a single term for action checking.



87
88
89
90
91
92
93
94
95
# File 'lib/adventure.rb', line 87

def compiled_params
  p = nil
  p = 'version' if params[:version]
  p = 'license' if params[:license]
  p = 'help' if params[:help]
  p = 'gamefile' unless params[:gamefile].nil? || params[:gamefile].empty?

  p
end

#gamefile_valid?(file) ⇒ Boolean (private)

Check if the gamefile has minimal contents expected.

Parameters:

  • file (String)

    The file name given through CLI.

Returns:

  • (Boolean)


100
101
102
103
104
105
106
# File 'lib/adventure.rb', line 100

def gamefile_valid?(file)
  game = YAML.safe_load_file(file, permitted_classes: ADVENTURE_CLASSES)
  return false unless game.key?('current_room')
  return false unless game.key?('level')

  true
end

#runObject

Begin CLI checks and calls the adequate methods.



63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
# File 'lib/adventure.rb', line 63

def run
  if params.errors.any?
    exit_with(:usage_error, params.errors.summary)
  else
    p = compiled_params
    case p
    when 'help' then puts help
    when 'license' then puts Adventure::LICENSE
    when 'version' then puts Adventure::VERSION
    when 'gamefile'
      file = params[:gamefile]
      exit_with(:not_found, 'Given game file doesn\'t exist') unless File.file?(file)
      exit_with(:invalid_argument, 'Given game file isn\'t valid') unless gamefile_valid?(file)
      # TODO: Start main loop here
      puts p
    else
      exit_with(:invalid_argument, 'Unexpected arguments')
    end
  end
end