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:



20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/dicey/roller.rb', line 20

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 "    Dice with non-numeric sides need gem \"vector_number\" to be present and available.\n    If this is intended, please install the gem.\n  TEXT\n  raise DiceyError, \"can not roll dice with non-numeric sides!\"\nend\n"