Module: Adjective::Status
- Includes:
- Temporable
- Defined in:
- lib/adjective/status.rb
Overview
Status is different from something like an attack in that it applies to things that afflict the subject for one or more turns.
Instance Method Summary collapse
-
#add_modifier(attribute, value) ⇒ Hash
Adds to the modifier to @modifiers.
-
#add_or_update_modifier(attribute, value) ⇒ Hash
Adds or updates the modifier hash.
-
#has_modifier?(attribute) ⇒ Boolean
Checks if the status has a modifier present.
-
#initialize_status(opts = {}) ⇒ Object
Initialize module data for Status.
-
#remove_modifier(attribute) ⇒ Hash
Removes the specified modifier from @modifers.
-
#tick(&block) ⇒ Object
Will perform tick functionality, whose default action is to reduce @remaining_duration (from Temporable) by 1.
-
#update_modifier(attribute, value) ⇒ Hash
Updates the modifier in @modifiers.
Methods included from Temporable
#expired?, #expiring?, #extend_by, #initialize_temporality, #max_duration?, #normalize_remaining_duration
Instance Method Details
#add_modifier(attribute, value) ⇒ Hash
Adds to the modifier to @modifiers. Will warn and NOT amend if modifier already exists.
99 100 101 102 103 104 105 106 107 |
# File 'lib/adjective/status.rb', line 99 def add_modifier(attribute, value) if !has_modifier?(attribute) @modifiers.store(attribute, value) assign_affected_attributes else warn("[#{Time.now}]: Attempted to add duplicate modifier: #{attribute}. The new value has NOT been set. (Currently '#{@modifiers[attribute]}'.") end return @modifiers end |
#add_or_update_modifier(attribute, value) ⇒ Hash
Adds or updates the modifier hash.
67 68 69 70 71 72 73 74 75 |
# File 'lib/adjective/status.rb', line 67 def add_or_update_modifier(attribute, value) if has_modifier?(attribute) @modifiers[attribute] = value else @modifiers.store(attribute, value) end assign_affected_attributes return @modifiers end |
#has_modifier?(attribute) ⇒ Boolean
Checks if the status has a modifier present
57 58 59 |
# File 'lib/adjective/status.rb', line 57 def has_modifier?(attribute) @modifiers.key?(attribute) end |
#initialize_status(opts = {}) ⇒ Object
Initialize module data for Status
17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 |
# File 'lib/adjective/status.rb', line 17 def initialize_status(opts = {}) attributes = opts[:affected_attributes] @modifiers = attributes ||= {} @affected_attributes = attributes.map{|entry| entry[0]} # @applied_at Can be used to track simple object intantation if class is created when status is applied. # TODO: If held in memory, opts will need to be given a :timestamp with a value comparable with a Time object. (Custom values should help?) # If the user wishes to sort by a specific attribute in Statusable, then they should pass a block and do so there. (Maybe?) @applied_at = opts[:timestamp] ||= Time.now [:initialized_at, :affected_attributes, :modifiers].each do |attribute| self.class.send(:attr_reader, attribute) end initialize_temporality(opts) normalize_remaining_duration assign_affected_attributes return self end |
#remove_modifier(attribute) ⇒ Hash
Removes the specified modifier from @modifers.
115 116 117 118 119 120 121 122 123 |
# File 'lib/adjective/status.rb', line 115 def remove_modifier(attribute) if has_modifier?(attribute) temp = {}.store(attribute, modifiers[attribute]) @modifiers.delete(attribute) else warn("[#{Time.now}]: Attempted to remove modifier that does not exist: #{attribute}") end return temp end |
#tick(&block) ⇒ Object
Will perform tick functionality, whose default action is to reduce @remaining_duration (from Temporable) by 1. Otherwise, it will accept a block and bypass all default functionality.
43 44 45 46 47 48 49 50 51 |
# File 'lib/adjective/status.rb', line 43 def tick(&block) if block_given? yield(self) else # Default @remaining_duration -= 1 end return self end |
#update_modifier(attribute, value) ⇒ Hash
Updates the modifier in @modifiers. Will warn and NOT amend if modifier does not exist.
83 84 85 86 87 88 89 90 91 |
# File 'lib/adjective/status.rb', line 83 def update_modifier(attribute, value) if has_modifier?(attribute) @modifiers[attribute] = value assign_affected_attributes else warn("[#{Time.now}]: Attempted to update a modifier that wasn't present: #{attribute}. Use #add_modifier or #add_or_update_modifier instead.") end return @modifiers end |