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
# 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
  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



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

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



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

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

#allow_event?(name) ⇒ Boolean

Returns:

  • (Boolean)


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

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

#before_commit(on:, &block) ⇒ Object



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

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