Class: Flux::Capacitor

Inherits:
Object
  • Object
show all
Defined in:
lib/flux_capacitor/capacitor.rb

Direct Known Subclasses

Falsy, Truthy

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

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

#pivotObject (readonly)

Returns the value of attribute pivot.



7
8
9
# File 'lib/flux_capacitor/capacitor.rb', line 7

def pivot
  @pivot
end

#time_dilationObject (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)
  timestamp  = pivot.strftime("%s").to_i - ("ffffffff".to_i(16)/8)
  DateTime.strptime(timestamp.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)
  timestamp  = pivot.strftime("%s").to_i - (MurmurHash3::V32.str_hexdigest(str).to_i(16)/8)
  DateTime.strptime(timestamp.to_s, "%s")
end

Instance Method Details

#limitObject



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

Returns:

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