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.
31 32 33 34 35 36 37 38 39 40 41 42 43 |
# File 'lib/games_dice/map_rule.rb', line 31 def initialize(trigger_value, trigger_op, mapped_value = 0, mapped_name = '') unless 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 unless 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.
59 60 61 |
# File 'lib/games_dice/map_rule.rb', line 59 def mapped_name @mapped_name end |
#mapped_value ⇒ Integer (readonly)
Value that a die will use after the value has been mapped.
55 56 57 |
# File 'lib/games_dice/map_rule.rb', line 55 def mapped_value @mapped_value end |
#trigger_op ⇒ Symbol (readonly)
Trigger operation. How the rule is assessed against #trigger_value.
47 48 49 |
# File 'lib/games_dice/map_rule.rb', line 47 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.
51 52 53 |
# File 'lib/games_dice/map_rule.rb', line 51 def trigger_value @trigger_value end |
Instance Method Details
#map_from(test_value) ⇒ Integer?
Assesses the rule against a die result value.
64 65 66 67 68 69 70 |
# File 'lib/games_dice/map_rule.rb', line 64 def map_from(test_value) op_result = @trigger_value.send(@trigger_op, test_value) return nil unless op_result return @mapped_value if op_result == true op_result end |