Class: TableSync::Receiving::Config
- Inherits:
-
Object
- Object
- TableSync::Receiving::Config
- Defined in:
- lib/table_sync/receiving/config.rb
Class Attribute Summary collapse
-
.default_values_for_options ⇒ Object
readonly
Returns the value of attribute default_values_for_options.
Instance Attribute Summary collapse
-
#events ⇒ Object
readonly
Returns the value of attribute events.
-
#model ⇒ Object
readonly
Returns the value of attribute model.
Class Method Summary collapse
- .add_hook_option(name, hook_class:) ⇒ Object
-
.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.
Instance Method Summary collapse
- #allow_event?(name) ⇒ Boolean
-
#initialize(model:, events: TableSync::Event::VALID_RESOLVED_EVENTS) ⇒ Config
constructor
A new instance of Config.
- #invalid_events ⇒ Object
- #option(name) ⇒ Object
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..each do |ivar, default_value_generator| instance_variable_set(ivar, default_value_generator.call(self)) end end |
Class Attribute Details
.default_values_for_options ⇒ Object (readonly)
Returns the value of attribute default_values_for_options.
26 27 28 |
# File 'lib/table_sync/receiving/config.rb', line 26 def end |
Instance Attribute Details
#events ⇒ Object (readonly)
Returns the value of attribute events.
7 8 9 |
# File 'lib/table_sync/receiving/config.rb', line 7 def events @events end |
#model ⇒ Object (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}" ||= {} [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}" ||= {} [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
74 75 76 |
# File 'lib/table_sync/receiving/config.rb', line 74 def allow_event?(name) events.include?(name) end |
#invalid_events ⇒ Object
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 |