Class: DiceBag::Result

Inherits:
Object
  • Object
show all
Includes:
Comparable
Defined in:
lib/dicebag/result.rb

Overview

This class merely encapsulates the result, providing convience methods to access the results of each section if desired.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(label, total, sections) ⇒ Result

Returns a new instance of Result.



11
12
13
14
15
# File 'lib/dicebag/result.rb', line 11

def initialize(label, total, sections)
  @label    = label
  @total    = total
  @sections = sections
end

Instance Attribute Details

#labelObject (readonly)

Returns the value of attribute label.



7
8
9
# File 'lib/dicebag/result.rb', line 7

def label
  @label
end

#sectionsObject (readonly)

Returns the value of attribute sections.



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

def sections
  @sections
end

#totalObject (readonly)

Returns the value of attribute total.



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

def total
  @total
end

Instance Method Details

#<=>(other) ⇒ Object



31
32
33
# File 'lib/dicebag/result.rb', line 31

def <=>(other)
  @total <=> other.total
end

#each(&block) ⇒ Object



17
18
19
# File 'lib/dicebag/result.rb', line 17

def each(&block)
  sections.each { |section| block.call section }
end

#inspectObject



27
28
29
# File 'lib/dicebag/result.rb', line 27

def inspect
  "<#{self.class.name} #{self}>"
end

#to_sObject



21
22
23
24
25
# File 'lib/dicebag/result.rb', line 21

def to_s
  return "#{label}: #{total}" unless label.empty?

  total.to_s
end