Class: TableSync::Config

Inherits:
Object
  • Object
show all
Defined in:
lib/table_sync/config.rb

Defined Under Namespace

Classes: CallbackRegistry

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(model:, events: nil) ⇒ Config

Returns a new instance of Config.



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
# File 'lib/table_sync/config.rb', line 7

def initialize(model:, events: nil)
  @model = model
  @events = events.nil? ? nil : [events].flatten.map(&:to_sym)

  @callback_registry = CallbackRegistry.new

  only(model.columns)
  mapping_overrides({})
  additional_data({})
  default_values({})
  @rest_key = :rest
  @version_key = :version
  @first_sync_time_key = nil
  @on_destroy = nil
  target_keys(model.primary_keys)
end

Instance Attribute Details

#callback_registryObject (readonly)

Returns the value of attribute callback_registry.



5
6
7
# File 'lib/table_sync/config.rb', line 5

def callback_registry
  @callback_registry
end

#eventsObject (readonly)

Returns the value of attribute events.



5
6
7
# File 'lib/table_sync/config.rb', line 5

def events
  @events
end

#modelObject (readonly)

Returns the value of attribute model.



5
6
7
# File 'lib/table_sync/config.rb', line 5

def model
  @model
end

Class Method Details

.add_option(name, &option_block) ⇒ Object

add_option implements next logic config.option - get value config.option(args) - set static value config.option { … } - set proc as value



28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/table_sync/config.rb', line 28

def self.add_option(name, &option_block)
  ivar = "@#{name}".to_sym

  option_block ||= proc { |value| value }

  define_method(name) do |*args, &block|
    if args.empty? && block.nil?
      instance_variable_get(ivar)
    elsif block
      params = block.parameters.map { |param| param[0] == :keyreq ? param[1] : nil }.compact
      unified_block = proc { |hash = {}| block.call(hash.slice(*params)) }
      instance_variable_set(ivar, unified_block)
    else
      instance_variable_set(ivar, instance_exec(*args, &option_block))
    end
  end
end

Instance Method Details

#after_commit(on:, &block) ⇒ Object



55
56
57
# File 'lib/table_sync/config.rb', line 55

def after_commit(on:, &block)
  callback_registry.register_callback(block, kind: :after_commit, event: on.to_sym)
end

#allow_event?(name) ⇒ Boolean

Returns:

  • (Boolean)


46
47
48
49
# File 'lib/table_sync/config.rb', line 46

def allow_event?(name)
  return true if events.nil?
  events.include?(name)
end

#before_commit(on:, &block) ⇒ Object



51
52
53
# File 'lib/table_sync/config.rb', line 51

def before_commit(on:, &block)
  callback_registry.register_callback(block, kind: :before_commit, event: on.to_sym)
end

#on_destroy(&block) ⇒ Object



59
60
61
# File 'lib/table_sync/config.rb', line 59

def on_destroy(&block)
  block_given? ? @on_destroy = block : @on_destroy
end