Class: TableSync::Publishing::Helpers::Debounce
- Inherits:
-
Object
- Object
- TableSync::Publishing::Helpers::Debounce
- Includes:
- Memery
- Defined in:
- lib/table_sync/publishing/helpers/debounce.rb
Constant Summary collapse
- DEFAULT_TIME =
60
Instance Attribute Summary collapse
-
#debounce_time ⇒ Object
readonly
Returns the value of attribute debounce_time.
-
#event ⇒ Object
readonly
Returns the value of attribute event.
-
#needle ⇒ Object
readonly
Returns the value of attribute needle.
-
#object_class ⇒ Object
readonly
Returns the value of attribute object_class.
Instance Method Summary collapse
- #cache_key ⇒ Object
- #cache_next_sync_time ⇒ Object
- #debounce_time_not_passed? ⇒ Boolean
- #debounce_time_passed? ⇒ Boolean
-
#debounced_sync_time ⇒ Object
MISC.
- #destroy_event? ⇒ Boolean
-
#initialize(object_class:, needle:, event:, debounce_time: nil) ⇒ Debounce
constructor
A new instance of Debounce.
-
#no_sync_before? ⇒ Boolean
CASE 1.
- #skip? ⇒ Boolean
-
#sync_in_the_future? ⇒ Boolean
CASE 3.
-
#sync_in_the_past? ⇒ Boolean
CASE 2.
- #upsert_event? ⇒ Boolean
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_time ⇒ Object (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 |
#event ⇒ Object (readonly)
Returns the value of attribute event.
56 57 58 |
# File 'lib/table_sync/publishing/helpers/debounce.rb', line 56 def event @event end |
#needle ⇒ Object (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_class ⇒ Object (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_key ⇒ Object
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_time ⇒ Object
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
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
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_time ⇒ Object
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
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
80 81 82 |
# File 'lib/table_sync/publishing/helpers/debounce.rb', line 80 def no_sync_before? cached_sync_time.nil? end |
#skip? ⇒ 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
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
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
106 107 108 |
# File 'lib/table_sync/publishing/helpers/debounce.rb', line 106 def upsert_event? !destroy_event? end |