Class: TableSync::Config
- Inherits:
-
Object
- Object
- TableSync::Config
- Defined in:
- lib/table_sync/config.rb
Defined Under Namespace
Classes: CallbackRegistry
Instance Attribute Summary collapse
-
#callback_registry ⇒ Object
readonly
Returns the value of attribute callback_registry.
-
#events ⇒ Object
readonly
Returns the value of attribute events.
-
#model ⇒ Object
readonly
Returns the value of attribute model.
Class Method Summary collapse
-
.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.
Instance Method Summary collapse
- #after_commit(on:, &block) ⇒ Object
- #allow_event?(name) ⇒ Boolean
- #before_commit(on:, &block) ⇒ Object
-
#initialize(model:, events: nil) ⇒ Config
constructor
A new instance of Config.
- #on_destroy(&block) ⇒ Object
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 |
# 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 target_keys(model.primary_keys) end |
Instance Attribute Details
#callback_registry ⇒ Object (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 |
#events ⇒ Object (readonly)
Returns the value of attribute events.
5 6 7 |
# File 'lib/table_sync/config.rb', line 5 def events @events end |
#model ⇒ Object (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
29 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 |
# File 'lib/table_sync/config.rb', line 29 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
66 67 68 |
# File 'lib/table_sync/config.rb', line 66 def after_commit(on:, &block) callback_registry.register_callback(block, kind: :after_commit, event: on.to_sym) end |
#allow_event?(name) ⇒ Boolean
57 58 59 60 |
# File 'lib/table_sync/config.rb', line 57 def allow_event?(name) return true if events.nil? events.include?(name) end |
#before_commit(on:, &block) ⇒ Object
62 63 64 |
# File 'lib/table_sync/config.rb', line 62 def before_commit(on:, &block) callback_registry.register_callback(block, kind: :before_commit, event: on.to_sym) end |
#on_destroy(&block) ⇒ Object
70 71 72 |
# File 'lib/table_sync/config.rb', line 70 def on_destroy(&block) block_given? ? @on_destroy = block : @on_destroy end |