Class: SelfDefense

Inherits:
Object
  • Object
show all
Defined in:
lib/self-defense.rb

Overview

file: self-defense.rb

Constant Summary collapse

@@hits =
[Time.now - 1]

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize {|_self| ... } ⇒ SelfDefense

Returns a new instance of SelfDefense.

Yields:

  • (_self)

Yield Parameters:

  • _self (SelfDefense)

    the object that the method was called on



9
10
11
12
13
# File 'lib/self-defense.rb', line 9

def initialize(&block)
  super()
  @unit = {s: proc{|x| x}, m: proc{|x| x*60}, h: proc{|x| x*60*60}}
  yield(self)  
end

Class Method Details

.newObject



15
16
17
18
# File 'lib/self-defense.rb', line 15

def self.new
  super()
  @@hits << Time.now
end

Instance Method Details

#rapid?(recovery_period = 0.8) ⇒ Boolean

Returns:

  • (Boolean)


20
21
22
# File 'lib/self-defense.rb', line 20

def rapid?(recovery_period=0.8)
  (Time.now - @@hits.last) > recovery_period
end

#sustained?(h = {}) ⇒ Boolean

Returns:

  • (Boolean)


24
25
26
27
# File 'lib/self-defense.rb', line 24

def sustained?(h={})    
  duration = @unit[h[:duration].slice!(-1).to_sym].call(h[:duration])
  @@hits.select {|x| @@hits.last - x <= duration.to_i}.length < h[:hit_limit] + 1
end