Class: TableSync::Publishing::Helpers::Debounce

Inherits:
Object
  • Object
show all
Includes:
Memery
Defined in:
lib/table_sync/publishing/helpers/debounce.rb

Constant Summary collapse

DEFAULT_TIME =
60

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(object_class:, needle:, event:, debounce_time: nil) ⇒ Debounce

Returns a new instance of Debounce.



58
59
60
61
62
63
# File 'lib/table_sync/publishing/helpers/debounce.rb', line 58

def initialize(object_class:, needle:, event:, debounce_time: nil)
  @event         = event
  @debounce_time = debounce_time || DEFAULT_TIME
  @object_class  = object_class
  @needle        = needle
end

Instance Attribute Details

#debounce_timeObject (readonly)

Returns the value of attribute debounce_time.



56
57
58
# File 'lib/table_sync/publishing/helpers/debounce.rb', line 56

def debounce_time
  @debounce_time
end

#eventObject (readonly)

Returns the value of attribute event.



56
57
58
# File 'lib/table_sync/publishing/helpers/debounce.rb', line 56

def event
  @event
end

#needleObject (readonly)

Returns the value of attribute needle.



56
57
58
# File 'lib/table_sync/publishing/helpers/debounce.rb', line 56

def needle
  @needle
end

#object_classObject (readonly)

Returns the value of attribute object_class.



56
57
58
# File 'lib/table_sync/publishing/helpers/debounce.rb', line 56

def object_class
  @object_class
end

Instance Method Details

#cache_keyObject



134
135
136
# File 'lib/table_sync/publishing/helpers/debounce.rb', line 134

def cache_key
  "#{object_class}/#{needle.values.join}_table_sync_time".delete(" ")
end

#cache_next_sync_timeObject



126
127
128
129
130
131
132
# File 'lib/table_sync/publishing/helpers/debounce.rb', line 126

def cache_next_sync_time
  expires_at = next_sync_time + debounce_time.seconds
  expires_in = expires_at - Time.current
  return if expires_in.negative?

  Rails.cache.write(cache_key, next_sync_time, expires_in:)
end

#debounce_time_not_passed?Boolean

Returns:

  • (Boolean)


93
94
95
# File 'lib/table_sync/publishing/helpers/debounce.rb', line 93

def debounce_time_not_passed?
  cached_sync_time + debounce_time.seconds > current_time
end

#debounce_time_passed?Boolean

Returns:

  • (Boolean)


89
90
91
# File 'lib/table_sync/publishing/helpers/debounce.rb', line 89

def debounce_time_passed?
  cached_sync_time + debounce_time.seconds <= current_time
end

#debounced_sync_timeObject

MISC



112
113
114
# File 'lib/table_sync/publishing/helpers/debounce.rb', line 112

def debounced_sync_time
  cached_sync_time + debounce_time.seconds
end

#destroy_event?Boolean

Returns:

  • (Boolean)


102
103
104
# File 'lib/table_sync/publishing/helpers/debounce.rb', line 102

def destroy_event?
  event == :destroy
end

#no_sync_before?Boolean

CASE 1

Returns:

  • (Boolean)


80
81
82
# File 'lib/table_sync/publishing/helpers/debounce.rb', line 80

def no_sync_before?
  cached_sync_time.nil?
end

#skip?Boolean

Returns:

  • (Boolean)


65
66
67
# File 'lib/table_sync/publishing/helpers/debounce.rb', line 65

def skip?
  sync_in_the_future? && upsert_event? # case 3.1
end

#sync_in_the_future?Boolean

CASE 3

Returns:

  • (Boolean)


98
99
100
# File 'lib/table_sync/publishing/helpers/debounce.rb', line 98

def sync_in_the_future?
  !!cached_sync_time && (cached_sync_time > current_time)
end

#sync_in_the_past?Boolean

CASE 2

Returns:

  • (Boolean)


85
86
87
# File 'lib/table_sync/publishing/helpers/debounce.rb', line 85

def sync_in_the_past?
  cached_sync_time <= current_time
end

#upsert_event?Boolean

Returns:

  • (Boolean)


106
107
108
# File 'lib/table_sync/publishing/helpers/debounce.rb', line 106

def upsert_event?
  !destroy_event?
end