Class: TableSync::Receiving::Config

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

Class Attribute Summary collapse

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(model:, events: TableSync::Event::VALID_RESOLVED_EVENTS) ⇒ Config

Returns a new instance of Config.



9
10
11
12
13
14
15
16
17
18
19
# File 'lib/table_sync/receiving/config.rb', line 9

def initialize(model:, events: TableSync::Event::VALID_RESOLVED_EVENTS)
  @model = model

  @events = [events].flatten.map(&:to_sym)

  raise TableSync::UndefinedEvent.new(events) if invalid_events.any?

  self.class.default_values_for_options.each do |ivar, default_value_generator|
    instance_variable_set(ivar, default_value_generator.call(self))
  end
end

Class Attribute Details

.default_values_for_optionsObject (readonly)

Returns the value of attribute default_values_for_options.



26
27
28
# File 'lib/table_sync/receiving/config.rb', line 26

def default_values_for_options
  @default_values_for_options
end

Instance Attribute Details

#eventsObject (readonly)

Returns the value of attribute events.



7
8
9
# File 'lib/table_sync/receiving/config.rb', line 7

def events
  @events
end

#modelObject (readonly)

Returns the value of attribute model.



7
8
9
# File 'lib/table_sync/receiving/config.rb', line 7

def model
  @model
end

Class Method Details

.add_hook_option(name, hook_class:) ⇒ Object



58
59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/table_sync/receiving/config.rb', line 58

def add_hook_option(name, hook_class:)
  ivar = :"@#{name}"

  @default_values_for_options ||= {}
  @default_values_for_options[ivar] = proc { [] }

  define_method(name) do |conditions, &handler|
    hooks = instance_variable_get(ivar)
    hooks ||= []

    hooks << hook_class.new(conditions:, handler:)
    instance_variable_set(ivar, hooks)
  end
end

.add_option(name, value_setter_wrapper:, value_as_proc_setter_wrapper:, default:) ⇒ Object

In a configs these options are requested as they are config.option - get value config.option(args) - set static value config.option { … } - set proc as value



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/receiving/config.rb', line 33

def add_option(name, value_setter_wrapper:, value_as_proc_setter_wrapper:, default:)
  ivar = :"@#{name}"

  @default_values_for_options ||= {}
  @default_values_for_options[ivar] = default

  define_method(name) do |*value, &value_as_proc|
    return instance_variable_get(ivar) if value.empty? && value_as_proc.nil?

    value = value.first if value.size == 1

    if value_as_proc.present?
      new_value = TableSync::Utils.proc_keywords_resolver(&value_as_proc)
      setter_wrapper = value_as_proc_setter_wrapper
    else
      new_value = value
      setter_wrapper = value_setter_wrapper
    end

    old_value = instance_variable_get(ivar)
    result_value = instance_exec(name, new_value, old_value, &setter_wrapper)
    instance_variable_set(ivar, result_value)
  end
end

Instance Method Details

#allow_event?(name) ⇒ Boolean

Returns:

  • (Boolean)


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

def allow_event?(name)
  events.include?(name)
end

#invalid_eventsObject



21
22
23
# File 'lib/table_sync/receiving/config.rb', line 21

def invalid_events
  events - TableSync::Event::VALID_RESOLVED_EVENTS
end

#option(name) ⇒ Object



78
79
80
# File 'lib/table_sync/receiving/config.rb', line 78

def option(name)
  instance_variable_get(:"@#{name}")
end