Class: Randsum::Replacer

Inherits:
Object
  • Object
show all
Defined in:
lib/randsum/filters/replacers/replacer.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(target:, with:, roll:) ⇒ Replacer

Returns a new instance of Replacer.



13
14
15
16
17
# File 'lib/randsum/filters/replacers/replacer.rb', line 13

def initialize(target:, with:, roll:)
  @target = ReplacerTarget.for(target, roll: roll)
  @with   = ReplacerValue.for(with, roll: roll)
  @roll   = roll
end

Instance Attribute Details

#rollObject (readonly)

Returns the value of attribute roll.



3
4
5
# File 'lib/randsum/filters/replacers/replacer.rb', line 3

def roll
  @roll
end

#targetObject (readonly)

Returns the value of attribute target.



3
4
5
# File 'lib/randsum/filters/replacers/replacer.rb', line 3

def target
  @target
end

#withObject (readonly)

Returns the value of attribute with.



3
4
5
# File 'lib/randsum/filters/replacers/replacer.rb', line 3

def with
  @with
end

Class Method Details

.for(target:, with:, roll:) ⇒ Object



5
6
7
8
9
10
11
# File 'lib/randsum/filters/replacers/replacer.rb', line 5

def self.for(target:, with:, roll:)
  Randsum::Replacer.new(
    target: target,
    with: with,
    roll: roll,
  )
end

Instance Method Details

#filterObject



19
20
21
22
23
24
25
# File 'lib/randsum/filters/replacers/replacer.rb', line 19

def filter
  return Randsum::Roll.new(
    die: roll.die,
    quantity: roll.quantity,
    result: result
  )
end

#resultObject



27
28
29
# File 'lib/randsum/filters/replacers/replacer.rb', line 27

def result
  (roll.result - target.match) + transformed
end

#transformedObject



31
32
33
# File 'lib/randsum/filters/replacers/replacer.rb', line 31

def transformed
  with.transform(target.match)
end