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.



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

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.



24
25
26
# File 'lib/table_sync/receiving/config.rb', line 24

def default_values_for_options
  @default_values_for_options
end

Instance Attribute Details

#eventsObject (readonly)

Returns the value of attribute events.



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

def events
  @events
end

#modelObject (readonly)

Returns the value of attribute model.



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

def model
  @model
end

Class Method Details

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

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`



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

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

Instance Method Details

#allow_event?(name) ⇒ Boolean

Returns:

  • (Boolean)


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

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

#invalid_eventsObject



19
20
21
# File 'lib/table_sync/receiving/config.rb', line 19

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