Module: PgEventstore::Extensions::ActsAsConfigurable::Configurable

Defined in:
lib/pg_eventstore/extensions/acts_as_configurable.rb,
sig/pg_eventstore/extensions/acts_as_configurable.rbs

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#mutexThread::Mutex

Returns the value of attribute mutex.

Returns:

  • (Thread::Mutex)


19
20
21
# File 'lib/pg_eventstore/extensions/acts_as_configurable.rb', line 19

def mutex
  @mutex
end

Instance Method Details

#available_configsArray<Symbol>

Returns:

  • (Array<Symbol>)


42
43
44
# File 'lib/pg_eventstore/extensions/acts_as_configurable.rb', line 42

def available_configs
  @config.keys
end

#config(name = const_get(:DEFAULT_CONFIG)) ⇒ PgEventstore::Config

Parameters:

  • name (Symbol) (defaults to: const_get(:DEFAULT_CONFIG))

Returns:



48
49
50
51
52
53
54
55
56
57
58
# File 'lib/pg_eventstore/extensions/acts_as_configurable.rb', line 48

def config(name = const_get(:DEFAULT_CONFIG))
  return @config[name] if @config[name]

  error_message = "    Could not find \#{name.inspect} config. You can define it like this:\n    \#{self}.configure(name: \#{name.inspect}) do |config|\n      # your config goes here\n    end\n  TEXT\n  raise error_message\nend\n"

#configure(name: const_get(:DEFAULT_CONFIG)) {|arg0| ... } ⇒ Object

Creates a Config if not exists and yields it to the given block.

Parameters:

  • name (Symbol) (defaults to: const_get(:DEFAULT_CONFIG))

    a name to assign to a config

  • name: (Symbol) (defaults to: const_get(:DEFAULT_CONFIG))

Yields:

Yield Parameters:

Yield Returns:

  • (Object)

Returns:

  • (Object)

    a result of the given block



25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/pg_eventstore/extensions/acts_as_configurable.rb', line 25

def configure(name: const_get(:DEFAULT_CONFIG))
  config_class = const_get(:CONFIG_CLASS)
  mutex.synchronize do
    @config[name] = @config[name] ? config_class.new(name:, **@config[name].options_hash) : config_class.new(name:)
    connection_config_was = connection_options(@config[name])

    yield(@config[name]).tap do
      @config[name].freeze
      next if connection_config_was == connection_options(@config[name])

      # Reset the connection if user decided to reconfigure connection's options
      @connection.delete(name)
    end
  end
end

#connection(name = const_get(:DEFAULT_CONFIG)) ⇒ PgEventstore::Connection

Look ups and returns a Connection, based on the given config. If not exists - it creates one. This operation is a thread-safe

Parameters:

  • name (Symbol) (defaults to: const_get(:DEFAULT_CONFIG))

Returns:



64
65
66
67
68
# File 'lib/pg_eventstore/extensions/acts_as_configurable.rb', line 64

def connection(name = const_get(:DEFAULT_CONFIG))
  mutex.synchronize do
    @connection[name] ||= Connection.new(**connection_options(config(name)))
  end
end

#connection_options(config) ⇒ Hash

Returns { uri: String, pool_size: Integer, pool_timeout: Integer }.

Parameters:

Returns:

  • (Hash)

    { uri: String, pool_size: Integer, pool_timeout: Integer }

Raises:

  • (NotImplementedError)


74
75
76
# File 'lib/pg_eventstore/extensions/acts_as_configurable.rb', line 74

def connection_options(config)
  raise NotImplementedError
end

#init_variablesvoid

This method returns an undefined value.



79
80
81
82
83
84
# File 'lib/pg_eventstore/extensions/acts_as_configurable.rb', line 79

def init_variables
  default_config = const_get(:DEFAULT_CONFIG)
  @config = { default_config => const_get(:CONFIG_CLASS).new(name: default_config) }
  @connection = {}
  @mutex = Thread::Mutex.new
end