class TableSync::Receiving::Config

Attributes

default_values_for_options[R]
events[R]
model[R]

Public Class Methods

add_option(name, value_setter_wrapper:, value_as_proc_setter_wrapper:, default:) click to toggle source

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

In `Receiving::Handler` or `Receiving::EventActions` this options are requested through `Receiving::ConfigDecorator#method_missing` which always executes `config.option`

# File lib/table_sync/receiving/config.rb, line 32
def add_option(name, value_setter_wrapper:, value_as_proc_setter_wrapper:, default:)
  ivar = "@#{name}".to_sym

  @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
new(model:, events: AVAILABLE_EVENTS) click to toggle source
# File lib/table_sync/receiving/config.rb, line 7
def initialize(model:, events: AVAILABLE_EVENTS)
  @model = model

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

  unless @events.all? { |event| AVAILABLE_EVENTS.include?(event) }
    raise TableSync::UndefinedEvent.new(events)
  end

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

Public Instance Methods

allow_event?(name) click to toggle source
# File lib/table_sync/receiving/config.rb, line 58
def allow_event?(name)
  events.include?(name)
end