Class: Keisanjaku::OperationSelector

Inherits:
Object
  • Object
show all
Defined in:
lib/keisanjaku/drill.rb

Constant Summary collapse

LABELS =
{
  multiply: "乗算",
  divide: "除算",
  square: "二乗",
  cube: "三乗",
  sqrt: "平方根",
  log: "対数",
  sin: "正弦",
  tan: "正接"
}.freeze

Class Method Summary collapse

Class Method Details



102
103
104
# File 'lib/keisanjaku/drill.rb', line 102

def self.menu
  LABELS.map { |key, label| "#{key}=#{label}" }.join("  ")
end


106
107
108
109
110
111
112
113
114
115
# File 'lib/keisanjaku/drill.rb', line 106

def self.menu_lines(width)
  LABELS.map { |key, label| "#{key}=#{label}" }.each_with_object([]) do |item, lines|
    candidate = lines.empty? || lines.last.empty? ? item : "#{lines.last}  #{item}"
    if lines.empty? || ANSI.visible_width(candidate) > width
      lines << item
    else
      lines[-1] = candidate
    end
  end
end

.parse(selection) ⇒ Object



91
92
93
94
95
96
97
98
99
100
# File 'lib/keisanjaku/drill.rb', line 91

def self.parse(selection)
  text = selection.to_s.strip
  return QuestionGenerator::OPERATIONS if text.empty? || text == "all"

  operations = text.split(/[,\s]+/).filter_map do |token|
    operation = token.to_sym
    operation if QuestionGenerator::OPERATIONS.include?(operation)
  end
  operations.empty? ? QuestionGenerator::OPERATIONS : operations.uniq
end