Class: GamesDice::MapRule
- Inherits:
-
Object
- Object
- GamesDice::MapRule
- Defined in:
- lib/games_dice/map_rule.rb
Overview
This class models rules that convert numbers shown on a die to values used in a game. A common use for this is to count “successes” - dice that score a certain number or higher.
An object of the class represents a single rule, such as “count a die result of 5 or more as 1 success”.
Instance Attribute Summary collapse
-
#mapped_name ⇒ String
readonly
Name for mapped value, used in explanations.
-
#mapped_value ⇒ Integer
readonly
Value that a die will use after the value has been mapped.
-
#trigger_op ⇒ Symbol
readonly
Trigger operation.
-
#trigger_value ⇒ Integer, ...
readonly
Trigger value.
Instance Method Summary collapse
-
#initialize(trigger_value, trigger_op, mapped_value = 0, mapped_name = '') ⇒ GamesDice::MapRule
constructor
Creates new instance of GamesDice::MapRule.
-
#map_from(test_value) ⇒ Integer?
Assesses the rule against a die result value.
Constructor Details
#initialize(trigger_value, trigger_op, mapped_value = 0, mapped_name = '') ⇒ GamesDice::MapRule
Creates new instance of GamesDice::MapRule. The rule will be assessed as
trigger_value.send( trigger_op, x )
where x is the Integer value shown on a die.
30 31 32 33 34 35 36 37 38 39 40 41 |
# File 'lib/games_dice/map_rule.rb', line 30 def initialize trigger_value, trigger_op, mapped_value=0, mapped_name='' if ! trigger_value.respond_to?( trigger_op ) raise ArgumentError, "trigger_value #{trigger_value.inspect} cannot respond to trigger_op #{trigger_value.inspect}" end @trigger_value = trigger_value @trigger_op = trigger_op raise TypeError if ! mapped_value.is_a? Numeric @mapped_value = Integer(mapped_value) @mapped_name = mapped_name.to_s end |
Instance Attribute Details
#mapped_name ⇒ String (readonly)
Name for mapped value, used in explanations.
57 58 59 |
# File 'lib/games_dice/map_rule.rb', line 57 def mapped_name @mapped_name end |
#mapped_value ⇒ Integer (readonly)
Value that a die will use after the value has been mapped.
53 54 55 |
# File 'lib/games_dice/map_rule.rb', line 53 def mapped_value @mapped_value end |
#trigger_op ⇒ Symbol (readonly)
Trigger operation. How the rule is assessed against #trigger_value.
45 46 47 |
# File 'lib/games_dice/map_rule.rb', line 45 def trigger_op @trigger_op end |
#trigger_value ⇒ Integer, ... (readonly)
Trigger value. An object that will use #trigger_op to assess a die result for a reroll.
49 50 51 |
# File 'lib/games_dice/map_rule.rb', line 49 def trigger_value @trigger_value end |
Instance Method Details
#map_from(test_value) ⇒ Integer?
Assesses the rule against a die result value.
62 63 64 65 66 67 68 69 |
# File 'lib/games_dice/map_rule.rb', line 62 def map_from test_value op_result = @trigger_value.send( @trigger_op, test_value ) return nil unless op_result if op_result == true return @mapped_value end return op_result end |