Class: DiceBag::Roll

Inherits:
Object
  • Object
show all
Includes:
RollString
Defined in:
lib/dicebag/roll.rb

Overview

This is the ‘main’ class of Dice Bag. This class takes the dice string, parses it, and encapsulates the actual rolling of the dice. If no dice string is given, it defaults to DiceBag.default_roll

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from RollString

#inspect, #to_s

Constructor Details

#initialize(dstr = nil) ⇒ Roll

Returns a new instance of Roll.



13
14
15
16
17
# File 'lib/dicebag/roll.rb', line 13

def initialize(dstr = nil)
  @dstr   = dstr ||= DiceBag.default_roll
  @tree   = DiceBag.parse dstr
  @result = nil
end

Instance Attribute Details

#dstrObject (readonly)

Returns the value of attribute dstr.



8
9
10
# File 'lib/dicebag/roll.rb', line 8

def dstr
  @dstr
end

#treeObject (readonly) Also known as: parsed

Returns the value of attribute tree.



9
10
11
# File 'lib/dicebag/roll.rb', line 9

def tree
  @tree
end

Instance Method Details

#averageObject



54
55
56
# File 'lib/dicebag/roll.rb', line 54

def average
  MinMaxCalc.average self
end

#maximumObject



58
59
60
# File 'lib/dicebag/roll.rb', line 58

def maximum
  MinMaxCalc.maximum self
end

#minimumObject



62
63
64
# File 'lib/dicebag/roll.rb', line 62

def minimum
  MinMaxCalc.minimum self
end

#notesObject



19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/dicebag/roll.rb', line 19

def notes
  arr = []
  fmt = "For %s: %s\n"

  tree.each_value do |part|
    next unless part.is_a?(RollPart) && !part.notes.empty?

    arr.push format(fmt, part, part.notes)
  end

  arr
end

#notes_to_sObject



32
33
34
35
36
# File 'lib/dicebag/roll.rb', line 32

def notes_to_s
  n = notes

  n.empty? ? '' : n.join("\n")
end

#resultObject



38
39
40
41
42
# File 'lib/dicebag/roll.rb', line 38

def result
  roll unless @result

  @result
end

#rollObject



44
45
46
47
48
49
50
51
52
# File 'lib/dicebag/roll.rb', line 44

def roll
  @label    = ''
  @total    = 0
  @sections = []

  handle_tree

  @result = Result.new(@label, @total, @sections)
end