Sha256: 02d099cab855f5fd72519dfb77fa3072eed170e3345aba4a2fb081af3c1d3637

Contents?: true

Size: 1.54 KB

Versions: 9

Compression:

Stored size: 1.54 KB

Contents

# frozen_string_literal: true

module TableSync::Publishing::ORMAdapter
  module Sequel
    module_function

    def model_naming(object)
      ::TableSync::NamingResolver::Sequel.new(table_name: object.table_name, db: object.db)
    end

    def find(dataset, conditions)
      dataset.find(conditions)
    end

    def attributes(object)
      object.values
    end

    def setup_sync(klass, opts)
      if_predicate     = to_predicate(opts.delete(:if), true)
      unless_predicate = to_predicate(opts.delete(:unless), false)
      debounce_time    = opts.delete(:debounce_time)

      if opts.any?
        raise "Only :if, :skip_debounce and :unless options are currently " \
              "supported for Sequel hooks"
      end

      register_callbacks(klass, if_predicate, unless_predicate, debounce_time)
    end

    def to_predicate(val, default)
      return val.to_proc if val.respond_to?(:to_proc)

      -> (*) { default }
    end

    def register_callbacks(klass, if_predicate, unless_predicate, debounce_time)
      { create: :created, update: :updated, destroy: :destroyed }.each do |event, state|
        klass.send(:define_method, :"after_#{event}") do
          if instance_eval(&if_predicate) && !instance_eval(&unless_predicate)
            db.after_commit do
              TableSync::Publishing::Publisher.new(
                self.class.name,
                values,
                state: state,
                debounce_time: debounce_time,
              ).publish
            end
          end

          super()
        end
      end
    end
  end
end

Version data entries

9 entries across 9 versions & 1 rubygems

Version Path
table_sync-5.1.0 lib/table_sync/publishing/orm_adapter/sequel.rb
table_sync-5.0.0 lib/table_sync/publishing/orm_adapter/sequel.rb
table_sync-4.2.2 lib/table_sync/publishing/orm_adapter/sequel.rb
table_sync-4.2.1 lib/table_sync/publishing/orm_adapter/sequel.rb
table_sync-4.2.0 lib/table_sync/publishing/orm_adapter/sequel.rb
table_sync-4.1.3 lib/table_sync/publishing/orm_adapter/sequel.rb
table_sync-4.1.1 lib/table_sync/publishing/orm_adapter/sequel.rb
table_sync-4.1.0 lib/table_sync/publishing/orm_adapter/sequel.rb
table_sync-4.0.0 lib/table_sync/publishing/orm_adapter/sequel.rb