Class: RTanque::NormalizedAttr::AttrContainer
- Inherits:
-
Object
- Object
- RTanque::NormalizedAttr::AttrContainer
- Defined in:
- lib/rtanque/normalized_attr.rb
Instance Method Summary collapse
- #delta(current_value, new_value) ⇒ Object
- #enforce_delta(attached_instance, current_value, new_value) ⇒ Object
- #enforce_range(value) ⇒ Object
-
#initialize(range, max_delta = MAX_DELTA) ⇒ AttrContainer
constructor
A new instance of AttrContainer.
- #max ⇒ Object
- #max_delta(attached_instance) ⇒ Object
- #min ⇒ Object
- #normalize(attached_instance, current_value, new_value) ⇒ Object
Constructor Details
#initialize(range, max_delta = MAX_DELTA) ⇒ AttrContainer
Returns a new instance of AttrContainer.
20 21 22 23 |
# File 'lib/rtanque/normalized_attr.rb', line 20 def initialize(range, max_delta = MAX_DELTA) @range = range @max_delta = max_delta end |
Instance Method Details
#delta(current_value, new_value) ⇒ Object
51 52 53 54 55 56 57 58 |
# File 'lib/rtanque/normalized_attr.rb', line 51 def delta(current_value, new_value) if current_value # Heading responds to delta current_value.respond_to?(:delta) ? current_value.delta(new_value) : (new_value - current_value) else 0 end end |
#enforce_delta(attached_instance, current_value, new_value) ⇒ Object
41 42 43 44 45 46 47 48 49 |
# File 'lib/rtanque/normalized_attr.rb', line 41 def enforce_delta(attached_instance, current_value, new_value) current_delta = self.delta(current_value, new_value) current_max_delta = self.max_delta(attached_instance) if current_delta.abs > current_max_delta current_delta > 0 ? current_value + current_max_delta : current_value - current_max_delta else new_value end end |
#enforce_range(value) ⇒ Object
60 61 62 63 64 65 66 |
# File 'lib/rtanque/normalized_attr.rb', line 60 def enforce_range(value) if @range.include?(value) value else value > self.max ? self.max : self.min end end |
#max ⇒ Object
29 30 31 |
# File 'lib/rtanque/normalized_attr.rb', line 29 def max @range.last end |
#max_delta(attached_instance) ⇒ Object
37 38 39 |
# File 'lib/rtanque/normalized_attr.rb', line 37 def max_delta(attached_instance) @max_delta.respond_to?(:call) ? @max_delta.call(attached_instance) : @max_delta end |
#min ⇒ Object
25 26 27 |
# File 'lib/rtanque/normalized_attr.rb', line 25 def min @range.first end |
#normalize(attached_instance, current_value, new_value) ⇒ Object
33 34 35 |
# File 'lib/rtanque/normalized_attr.rb', line 33 def normalize(attached_instance, current_value, new_value) self.enforce_range(self.enforce_delta(attached_instance, current_value, new_value)) end |