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
- #wrap_receiving(&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 24 |
# 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 @wrap_receiving = 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
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 56 |
# File 'lib/table_sync/config.rb', line 30 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
67 68 69 |
# File 'lib/table_sync/config.rb', line 67 def after_commit(on:, &block) callback_registry.register_callback(block, kind: :after_commit, event: on.to_sym) end |
#allow_event?(name) ⇒ Boolean
58 59 60 61 |
# File 'lib/table_sync/config.rb', line 58 def allow_event?(name) return true if events.nil? events.include?(name) end |
#before_commit(on:, &block) ⇒ Object
63 64 65 |
# File 'lib/table_sync/config.rb', line 63 def before_commit(on:, &block) callback_registry.register_callback(block, kind: :before_commit, event: on.to_sym) end |
#on_destroy(&block) ⇒ Object
71 72 73 |
# File 'lib/table_sync/config.rb', line 71 def on_destroy(&block) block_given? ? @on_destroy = block : @on_destroy end |
#wrap_receiving(&block) ⇒ Object
75 76 77 |
# File 'lib/table_sync/config.rb', line 75 def wrap_receiving(&block) block_given? ? @wrap_receiving = block : @wrap_receiving end |