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

  # initialize default options
  only(model.columns)
  mapping_overrides({})
  additional_data({})
  default_values({})
  @rest_key = :rest
  @version_key = :version
  @first_sync_time_key = nil
  @on_destroy = nil
  @wrap_receiving = 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 the following logic config.option - get value config.option(args) - set static value config.option { … } - set proc as value



30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/table_sync/config.rb', line 30

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

  # default option handler (empty handler)
  # handles values when setting options
  option_block ||= proc { |value| value }

  define_method(name) do |*args, &block|
    # logic for each option
    if args.empty? && block.nil? # case for config.option, just return ivar
      instance_variable_get(ivar)
    elsif block # case for config.option { ... }
      # get named parameters (parameters with :keyreq) from proc-value
      params = block.parameters.map { |param| param[0] == :keyreq ? param[1] : nil }.compact

      # wrapper for proc-value, this wrapper receives all params (model, version, project_id...)
      # and filters them for proc-value
      unified_block = proc { |hash = {}| block.call(hash.slice(*params)) }

      # set wrapped proc-value as ivar value
      instance_variable_set(ivar, unified_block)
    else # case for config.option(args)
      # call option_block with args as params and set ivar to result
      instance_variable_set(ivar, instance_exec(*args, &option_block))
    end
  end
end

Instance Method Details

#after_commit(on:, &block) ⇒ Object



67
68
69
# File 'lib/table_sync/config.rb', line 67

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

#allow_event?(name) ⇒ Boolean

Returns:

  • (Boolean)


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

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

#before_commit(on:, &block) ⇒ Object



63
64
65
# File 'lib/table_sync/config.rb', line 63

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

#on_destroy(&block) ⇒ Object



71
72
73
# File 'lib/table_sync/config.rb', line 71

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

#wrap_receiving(&block) ⇒ Object



75
76
77
# File 'lib/table_sync/config.rb', line 75

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