Class: Rochambeau::Option

Inherits:
Object
  • Object
show all
Extended by:
T::Sig
Defined in:
lib/rochambeau/option.rb

Constant Summary collapse

ROCK =
T.let(new('r', 'rock'), Option)
PAPER =
T.let(new('p', 'paper'), Option)
SCISSORS =
T.let(new('s', 'scissors'), Option)
LIZARD =
T.let(new('l', 'lizard'), Option)
SPOCK =
T.let(new('v', 'spock'), Option)
ALL =
T.let(
  [ROCK, PAPER, SCISSORS, LIZARD, SPOCK],
  T::Array[Option]
)

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(initial, name) ⇒ Option

Returns a new instance of Option.



14
15
16
17
# File 'lib/rochambeau/option.rb', line 14

def initialize(initial, name)
  @initial = initial
  @name = name
end

Instance Attribute Details

#initialObject (readonly)

Returns the value of attribute initial.



9
10
11
# File 'lib/rochambeau/option.rb', line 9

def initial
  @initial
end

#nameObject (readonly)

Returns the value of attribute name.



9
10
11
# File 'lib/rochambeau/option.rb', line 9

def name
  @name
end

Class Method Details

.explain(option1, option2) ⇒ Object



104
105
106
107
108
109
110
# File 'lib/rochambeau/option.rb', line 104

def explain(option1, option2)
  unless OUTCOMES[option1] && T.must(OUTCOMES[option1])[option2]
    raise StandardError, "Unexpected combination: #{option1}, #{option2}"
  end

  T.must(T.must(OUTCOMES[option1])[option2])[:explanation]
end

.from_initial(initial) ⇒ Object



113
114
115
116
117
118
# File 'lib/rochambeau/option.rb', line 113

def from_initial(initial)
  result = ALL.detect { |o| o.initial == initial }
  return result if result

  raise Rochambeau::InvalidOptionError, "Invalid initial '#{initial}'."
end

Instance Method Details

#<=>(other) ⇒ Object



79
80
81
82
83
84
85
# File 'lib/rochambeau/option.rb', line 79

def <=>(other)
  unless OUTCOMES[self] && T.must(OUTCOMES[self])[other]
    raise StandardError, "Could not compare #{self} with #{other}"
  end

  T.must(T.must(OUTCOMES[self])[other])[:result]
end

#labelObject



93
94
95
# File 'lib/rochambeau/option.rb', line 93

def label
  "#{@name.capitalize} (#{@initial})"
end

#to_sObject



88
89
90
# File 'lib/rochambeau/option.rb', line 88

def to_s
  @name
end