Module: Adjective::Statusable
- Defined in:
- lib/adjective/concerns/statusable.rb
Overview
I plan on having this module handle the messier side of buff/debuff processing It will need to include a way to manage the coupling between Status and the target model. It will need to be able to process and return values that can be easily passed into other methods.
Instance Method Summary collapse
- #apply_status(status) {|_self, status| ... } ⇒ Object
- #clear_expired_statuses ⇒ Object
-
#has_status?(attribute, match) ⇒ Boolean
Actually has three cases 1: has and responds to given method PERIOD.
- #initialize_status_data ⇒ Object
- #tick_all {|_self, @statuses| ... } ⇒ Object
Instance Method Details
#apply_status(status) {|_self, status| ... } ⇒ Object
15 16 17 18 19 20 21 |
# File 'lib/adjective/concerns/statusable.rb', line 15 def apply_status(status, &block) validate_modifier_existence(status) validate_initial_attributes(status.affected_attributes) yield(self, status) if block_given? @statuses.push(status) return @statuses end |
#clear_expired_statuses ⇒ Object
55 56 57 58 59 60 |
# File 'lib/adjective/concerns/statusable.rb', line 55 def clear_expired_statuses unexpired = @statuses.select { |status| status.duration != 0 } diff = @statuses - unexpired @statuses = unexpired return diff end |
#has_status?(attribute, match) ⇒ Boolean
Actually has three cases 1: has and responds to given method PERIOD
25 26 27 28 29 30 31 32 |
# File 'lib/adjective/concerns/statusable.rb', line 25 def has_status?(attribute, match) @statuses.each do |status| if status.respond_to?(attribute) return true if status.send(attribute) == match end end return false end |
#initialize_status_data ⇒ Object
10 11 12 13 |
# File 'lib/adjective/concerns/statusable.rb', line 10 def initialize_status_data @statuses = [] self.class.send(:attr_accessor, :statuses) end |
#tick_all {|_self, @statuses| ... } ⇒ Object
34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 |
# File 'lib/adjective/concerns/statusable.rb', line 34 def tick_all(&block) # Provides baseline functionality to + or - values from a given status, as this is # the most common of implementations. # Strings and other values are simply set. # Yielding to an arbitrary block should curcumvent any issues with extension and post-effect hooks. @statuses.each do |status| validate_modifier_existence(status) status.modifiers.each do |key, value| attribute = key.to_s if (value.is_a?(Integer) || value.is_a?(Float)) eval("self.#{attribute} += #{value}") if self.respond_to?(attribute+"=") else send("#{attribute}=", value) if self.respond_to?(attribute+"=") end end status.tick end yield(self, @statuses) if block_given? return @statuses end |