Class: Dicey::Roller

Inherits:
Object
  • Object
show all
Includes:
Mixins::RationalToInteger, Mixins::VectorizeDice
Defined in:
lib/dicey/roller.rb

Overview

Let the dice roll!

This is the implementation of roll mode for the CLI.

Instance Method Summary collapse

Instance Method Details

#call(arguments, format:) ⇒ nil

Parameters:

  • arguments (Array<String>)

    die definitions

  • format (#call)

    formatter for output

Returns:

  • (nil)

Raises:



27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/dicey/roller.rb', line 27

def call(arguments, format:, **)
  raise DiceyError, "no dice!" if arguments.empty?

  dice = arguments.flat_map { |definition| die_foundry.cast(definition) }
  result = roll_dice(dice)

  format.call({ "roll" => rational_to_integer(result) }, AbstractDie.describe(dice))
rescue TypeError
  warn <<~TEXT
    Dice with non-numeric sides need gem "vector_number" to be present and available.
    If this is intended, please install the gem.
  TEXT
  raise DiceyError, "can not roll dice with non-numeric sides!"
end