Sha256: cb7ec92fc763765e22641a1bbb607149ec2c2a99ceee11da586843a9fe6b4bb3

Contents?: true

Size: 1.33 KB

Versions: 7

Compression:

Stored size: 1.33 KB

Contents

# frozen_string_literal: true

module TableSync::Setup
  class Base
    include Tainbox

    EVENTS            = %i[create update destroy].freeze
    INVALID_EVENT     = Class.new(StandardError)
    INVALID_CONDITION = Class.new(StandardError)

    attribute :object_class
    attribute :debounce_time
    attribute :on
    attribute :if_condition
    attribute :unless_condition

    def initialize(attrs)
      super(attrs)

      self.on = Array.wrap(on).map(&:to_sym)

      self.if_condition     ||= proc { true }
      self.unless_condition ||= proc { false }

      raise INVALID_EVENTS    unless valid_events?
      raise INVALID_CONDITION unless valid_conditions?
    end

    def register_callbacks
      applicable_events.each { |event| define_after_commit(event) }
    end

    private

    # VALIDATION

    def valid_events?
      on.all? { |event| event.in?(EVENTS) }
    end

    def valid_conditions?
      if_condition.is_a?(Proc) && unless_condition.is_a?(Proc)
    end

    # EVENTS

    def applicable_events
      on.presence || EVENTS
    end

    # CREATING HOOKS

    # :nocov:
    def define_after_commit(event)
      raise NotImplementedError
    end
    # :nocov:

    def options_exposed_for_block
      {
        if: if_condition,
        unless: unless_condition,
        debounce_time: debounce_time,
      }
    end
  end
end

Version data entries

7 entries across 7 versions & 1 rubygems

Version Path
table_sync-6.5.1 lib/table_sync/setup/base.rb
table_sync-6.5.0 lib/table_sync/setup/base.rb
table_sync-6.4.2 lib/table_sync/setup/base.rb
table_sync-6.4.1 lib/table_sync/setup/base.rb
table_sync-6.4.0 lib/table_sync/setup/base.rb
table_sync-6.3.0 lib/table_sync/setup/base.rb
table_sync-6.1.0 lib/table_sync/setup/base.rb