lib/table_sync/publisher.rb in table_sync-1.7.0 vs lib/table_sync/publisher.rb in table_sync-1.8.0

- old
+ new

@@ -2,30 +2,32 @@ class TableSync::Publisher < TableSync::BasePublisher DEBOUNCE_TIME = 1.minute # 'original_attributes' are not published, they are used to resolve the routing key - def initialize(object_class, original_attributes, destroyed: nil, confirm: true, state: :updated) + def initialize(object_class, original_attributes, **opts) @object_class = object_class.constantize @original_attributes = filter_safe_for_serialization(original_attributes.deep_symbolize_keys) - @confirm = confirm + @confirm = opts.fetch(:confirm, true) + @debounce_time = opts[:debounce_time]&.seconds || DEBOUNCE_TIME - if destroyed.nil? - @state = validate_state(state) + if opts[:destroyed].nil? + @state = opts.fetch(:state, :updated).to_sym + validate_state else # TODO Legacy job support, remove - @state = destroyed ? :destroyed : :updated + @state = opts[:destroyed] ? :destroyed : :updated end end def publish - return enqueue_job if destroyed? + return enqueue_job if destroyed? || debounce_time.zero? - sync_time = Rails.cache.read(cache_key) || current_time - DEBOUNCE_TIME - 1.second + sync_time = Rails.cache.read(cache_key) || current_time - debounce_time - 1.second return if sync_time > current_time - next_sync_time = sync_time + DEBOUNCE_TIME + next_sync_time = sync_time + debounce_time next_sync_time <= current_time ? enqueue_job : enqueue_job(next_sync_time) end def publish_now # Update request and object does not exist -> skip publishing @@ -36,10 +38,11 @@ private attr_reader :original_attributes attr_reader :state + attr_reader :debounce_time def attrs_for_callables original_attributes end @@ -111,13 +114,9 @@ def created? state == :created end - def validate_state(state) - if %i[created updated destroyed].include?(state&.to_sym) - state.to_sym - else - raise "Unknown state: #{state.inspect}" - end + def validate_state + raise "Unknown state: #{state.inspect}" unless %i[created updated destroyed].include?(state) end end