Class: TTY2::Prompt::Choice

Inherits:
Object
  • Object
show all
Defined in:
lib/tty2/prompt/choice.rb

Overview

An immutable representation of a single choice option from select menu

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, value, **options) ⇒ Choice

Create a Choice instance



95
96
97
98
99
100
101
# File 'lib/tty2/prompt/choice.rb', line 95

def initialize(name, value, **options)
  @name  = name
  @value = value
  @key   = options[:key]
  @disabled = options[:disabled].nil? ? false : options[:disabled]
  freeze
end

Instance Attribute Details

#disabledObject (readonly)

The text to display for disabled choice



90
91
92
# File 'lib/tty2/prompt/choice.rb', line 90

def disabled
  @disabled
end

#keyObject (readonly)

The keyboard key to activate this choice



85
86
87
# File 'lib/tty2/prompt/choice.rb', line 85

def key
  @key
end

#nameObject (readonly)

The label name



80
81
82
# File 'lib/tty2/prompt/choice.rb', line 80

def name
  @name
end

Class Method Details

.convert_array(val) ⇒ Choice

Convert an array into choice

Parameters:

  • (Array<Object>)

Returns:



49
50
51
52
53
54
55
56
57
58
# File 'lib/tty2/prompt/choice.rb', line 49

def self.convert_array(val)
  name, value, options = *val
  if name.is_a?(Hash)
    convert_hash(name)
  elsif val.size == 1
    new(name.to_s, name.to_s)
  else
    new(name.to_s, value, **(options || {}))
  end
end

.convert_hash(val) ⇒ Choice

Convert a hash into choice

Parameters:

  • (Hash<Symbol,Object>)

Returns:



67
68
69
70
71
72
73
74
75
# File 'lib/tty2/prompt/choice.rb', line 67

def self.convert_hash(val)
  if val.key?(:name) && val.key?(:value)
    new(val[:name].to_s, val[:value], **val)
  elsif val.key?(:name)
    new(val[:name].to_s, val[:name].to_s, **val)
  else
    new(val.keys.first.to_s, val.values.first)
  end
end

.from(val) ⇒ Choice

Create choice from value

Parameters:

  • val (Object)

    the value to be converted

Returns:

Raises:

  • (ArgumentError)


29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/tty2/prompt/choice.rb', line 29

def self.from(val)
  case val
  when Choice
    val
  when Array
    convert_array(val)
  when Hash
    convert_hash(val)
  else
    new(val, val)
  end
end

Instance Method Details

#==(other) ⇒ Boolean

Object equality comparison

Returns:

  • (Boolean)


129
130
131
132
133
134
135
# File 'lib/tty2/prompt/choice.rb', line 129

def ==(other)
  return false unless other.is_a?(self.class)

  name == other.name &&
    value == other.value &&
    key == other.key
end

#disabled?Boolean

Check if this choice is disabled

Returns:

  • (Boolean)


108
109
110
# File 'lib/tty2/prompt/choice.rb', line 108

def disabled?
  !!@disabled
end

#to_sString

Object string representation

Returns:

  • (String)


142
143
144
# File 'lib/tty2/prompt/choice.rb', line 142

def to_s
  name.to_s
end

#valueObject

Read value and evaluate



115
116
117
118
119
120
121
122
# File 'lib/tty2/prompt/choice.rb', line 115

def value
  case @value
  when Proc
    @value.call
  else
    @value
  end
end