class Gamefic::Props::MultipleChoice

Props for MultipleChoice scenes.

Attributes

invalid_message[W]

A message to send the player for an invalid choice. A formatting token named %<input>s can be used to inject the user input.

@return [String]

Public Instance Methods

index() click to toggle source

The zero-based index of the selected option.

@return [Integer, nil]

# File lib/gamefic/props/multiple_choice.rb, line 28
def index
  return nil unless input

  @index ||= index_by_number || index_by_text
end
invalid_message() click to toggle source
# File lib/gamefic/props/multiple_choice.rb, line 21
def invalid_message
  @invalid_message ||= '"%<input>s" is not a valid choice.'
end
number() click to toggle source

The one-based index of the selected option.

@return [Integer, nil]

# File lib/gamefic/props/multiple_choice.rb, line 37
def number
  return nil unless index

  index + 1
end
options() click to toggle source

The array of available options.

@return [Array<String>]

# File lib/gamefic/props/multiple_choice.rb, line 17
def options
  @options ||= []
end
selection() click to toggle source

The full text of the selected option.

@return [String, nil]

# File lib/gamefic/props/multiple_choice.rb, line 46
def selection
  return nil unless index

  options[index]
end

Private Instance Methods

index_by_number() click to toggle source
# File lib/gamefic/props/multiple_choice.rb, line 54
def index_by_number
  return input.to_i - 1 if input.match(/^\d+$/) && options[input.to_i - 1]

  nil
end
index_by_text() click to toggle source
# File lib/gamefic/props/multiple_choice.rb, line 60
def index_by_text
  matches = options.map.with_index { |text, idx| next idx if text.downcase.start_with?(input.downcase) }.compact
  matches.first if matches.one?
end