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

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.

Examples:

SurrogateClass.add_modifer(:strength, 20)

Parameters:

  • attribute (Symbol)
  • value (Integer, Float, String)

Returns:

  • (Hash)


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.

Examples:

SurrogateClass.add_or_update_modifer(:hitpoints, 10)

Parameters:

  • attribute (Symbol)
  • value (Integer, Float, String)

Returns:

  • (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

Examples:

SurrogateClass.has_modifier?(:hitpoints)

Returns:

  • (Boolean)


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

Examples:

class SurrogateClass
  include Adjective::Status
  initialize_status({affected_attributes: { hitpoints: 3}, max_duration: 5})
end

Parameters:

  • opts (Hash) (defaults to: {})

Returns:

  • (Object)


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.

Examples:

SurrogateClass.add_modifer(:strength, 20)

Parameters:

  • attribute (Symbol)
  • value (Integer, Float, String)

Returns:

  • (Hash)


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.

Examples:

SurrogateClass.tick

Parameters:

  • block (Block)

Returns:

  • (Object)


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.

Examples:

SurrogateClass.update_modifier(:hitpoints, 12)

Parameters:

  • attribute (Symbol)
  • value (Integer, Float, String)

Returns:

  • (Hash)


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