Class: Flux::Capacitor
- Inherits:
-
Object
- Object
- Flux::Capacitor
- Defined in:
- lib/flux_capacitor/capacitor.rb
Instance Attribute Summary collapse
-
#pivot ⇒ Object
readonly
Returns the value of attribute pivot.
-
#time_dilation ⇒ Object
readonly
Returns the value of attribute time_dilation.
Class Method Summary collapse
Instance Method Summary collapse
-
#initialize(start_time, completion_target, oldest_target = Capacitor.oldest_string_time(start_time)) ⇒ Capacitor
constructor
A new instance of Capacitor.
- #limit ⇒ Object
- #string_to_time(str) ⇒ Object
- #travel_to?(destination) ⇒ Boolean
Constructor Details
#initialize(start_time, completion_target, oldest_target = Capacitor.oldest_string_time(start_time)) ⇒ Capacitor
Returns a new instance of Capacitor.
8 9 10 11 12 13 |
# File 'lib/flux_capacitor/capacitor.rb', line 8 def initialize (start_time, completion_target, oldest_target = Capacitor.oldest_string_time(start_time)) @pivot = start_time time_to_complete = seconds_between(completion_target, pivot) dilation = seconds_between(pivot, oldest_target).to_f / time_to_complete.to_f @time_dilation = dilation end |
Instance Attribute Details
#pivot ⇒ Object (readonly)
Returns the value of attribute pivot.
7 8 9 |
# File 'lib/flux_capacitor/capacitor.rb', line 7 def pivot @pivot end |
#time_dilation ⇒ Object (readonly)
Returns the value of attribute time_dilation.
7 8 9 |
# File 'lib/flux_capacitor/capacitor.rb', line 7 def time_dilation @time_dilation end |
Class Method Details
.oldest_string_time(pivot) ⇒ Object
34 35 36 37 |
# File 'lib/flux_capacitor/capacitor.rb', line 34 def self.oldest_string_time(pivot) = pivot.strftime("%s").to_i - ("ffffffff".to_i(16)/8) DateTime.strptime(.to_s, "%s") end |
.string_to_time(pivot, str) ⇒ Object
29 30 31 32 |
# File 'lib/flux_capacitor/capacitor.rb', line 29 def self.string_to_time(pivot, str) = pivot.strftime("%s").to_i - (MurmurHash3::V32.str_hexdigest(str).to_i(16)/8) DateTime.strptime(.to_s, "%s") end |
Instance Method Details
#limit ⇒ Object
15 16 17 18 |
# File 'lib/flux_capacitor/capacitor.rb', line 15 def limit diff = seconds_between(DateTime.now, pivot) datetime_minus_seconds(pivot, (diff * time_dilation).to_i) end |
#string_to_time(str) ⇒ Object
25 26 27 |
# File 'lib/flux_capacitor/capacitor.rb', line 25 def string_to_time(str) Capacitor.string_to_time(pivot, str) end |
#travel_to?(destination) ⇒ Boolean
20 21 22 23 |
# File 'lib/flux_capacitor/capacitor.rb', line 20 def travel_to?(destination) destination = destination.is_a?(DateTime) ? destination : string_to_time(destination) limit < destination end |