class TTY::Prompt::Choice

A single choice option

@api public

Attributes

key[R]
name[R]

The label name

@api public

Public Class Methods

from(val) click to toggle source

Create choice from value

@example

Choice.from(:option_1)
Choice.from([:option_1, 1])

@param [Object] val

the value to be converted

@raise [ArgumentError]

@return [Choice]

@api public

# File lib/tty/prompt/choice.rb, line 39
def self.from(val)
  case val
  when Choice
    val
  when String, Symbol
    new(val, val)
  when Array
    new("#{val.first}", val.last)
  when Hash
    if val.key?(:name)
      new("#{val[:name]}", val[:value], val[:key])
    else
      new("#{val.keys.first}", val.values.first)
    end
  else
    raise ArgumentError, "#{val} cannot be coerced into Choice"
  end
end
new(name, value, key = nil) click to toggle source

Create a Choice instance

@api public

# File lib/tty/prompt/choice.rb, line 19
def initialize(name, value, key = nil)
  @name  = name
  @value = value
  @key   = key
end

Public Instance Methods

==(other) click to toggle source

Object equality comparison

@return [Boolean]

@api public

# File lib/tty/prompt/choice.rb, line 75
def ==(other)
  return false unless other.is_a?(self.class)
  name == other.name && value == other.value
end
to_s() click to toggle source

Object string representation

@return [String]

@api public

# File lib/tty/prompt/choice.rb, line 85
def to_s
  "#{name}"
end
value() click to toggle source

Read value and evaluate

@api public

# File lib/tty/prompt/choice.rb, line 61
def value
  case @value
  when Proc
    @value.call
  else
    @value
  end
end