Class: Ductr::Configuration

Inherits:
Object
  • Object
show all
Defined in:
lib/ductr/configuration.rb

Overview

Contains the framework’s configuration, including:

  • ‘DUCTR_ENV` environment variable

  • ‘Ductr.configure` block

  • Project root path

  • YML file configuration

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(env) ⇒ Configuration

Initializing environment to “development” by default, setting project root, parsing YML with the config gem and aliasing semantic logger gem constant to make it usable through the ‘Ductr.configure` block



42
43
44
45
46
47
48
49
50
51
52
# File 'lib/ductr/configuration.rb', line 42

def initialize(env)
  @root = Dir.pwd
  @yml = load_yaml("#{root}/config/#{env}.yml")

  @logging = Log::Logger
  logging.level = :debug

  @active_job = Struct.new(:queue_adapter, :default_queue_name, :queue_name_prefix, :queue_name_delimiter).new
  @store_adapter = ActiveSupport::Cache::FileStore
  @store_parameters = ["tmp/store"]
end

Instance Attribute Details

#active_jobStruct (readonly)

Returns The active job configuration, available options are ‘queue_adapter`, `default_queue_name`, `queue_name_prefix` & `queue_name_delimiter`.

Returns:

  • (Struct)

    The active job configuration, available options are ‘queue_adapter`, `default_queue_name`, `queue_name_prefix` & `queue_name_delimiter`



17
18
19
# File 'lib/ductr/configuration.rb', line 17

def active_job
  @active_job
end

#loggingClass<Ductr::Log::Logger> (readonly)

Returns The logger constant.

Returns:



20
21
22
# File 'lib/ductr/configuration.rb', line 20

def logging
  @logging
end

#rootString (readonly)

Returns The project root.

Returns:

  • (String)

    The project root



23
24
25
# File 'lib/ductr/configuration.rb', line 23

def root
  @root
end

#store_adapterClass<ActiveSupport::Cache::Store>, Symbol (readonly)

Returns:



27
28
29
# File 'lib/ductr/configuration.rb', line 27

def store_adapter
  @store_adapter
end

#store_optionsHash<Symbol, Object> (readonly)

Returns The store adapter config options.

Returns:

  • (Hash<Symbol, Object>)

    The store adapter config options



33
34
35
# File 'lib/ductr/configuration.rb', line 33

def store_options
  @store_options
end

#store_parametersArray (readonly)

Returns The store adapter config args.

Returns:

  • (Array)

    The store adapter config args



30
31
32
# File 'lib/ductr/configuration.rb', line 30

def store_parameters
  @store_parameters
end

#ymlHash (readonly)

Returns The parsed YML configuration.

Returns:

  • (Hash)

    The parsed YML configuration



36
37
38
# File 'lib/ductr/configuration.rb', line 36

def yml
  @yml
end

Instance Method Details

#adapter(name) ⇒ Adapter

Find an adapter based on its name.

Parameters:

  • name (Symbol)

    The name of the adapter to find

Returns:

Raises:



90
91
92
93
94
95
96
# File 'lib/ductr/configuration.rb', line 90

def adapter(name)
  not_found_error = -> { raise AdapterNotFoundError, "The adapter named \"#{name}\" does not exist" }

  adapters.find(not_found_error) do |adapter|
    adapter.name == name
  end
end

#adaptersArray<Adapter>

Memoize configured adapters based on the YAML configuration.

Returns:

  • (Array<Adapter>)

    The configured Adapter instances



73
74
75
76
77
78
79
80
# File 'lib/ductr/configuration.rb', line 73

def adapters
  @adapters ||= yml.adapters.to_h.map do |name, entry|
    adapter_class = Ductr.adapter_registry.find(entry.adapter)
    config = entry.to_h.except(:adapter)

    adapter_class.new(name, **config)
  end
end

#apply_active_job_configvoid

This method returns an undefined value.

Configures active job with the given options.



103
104
105
106
107
108
109
110
111
# File 'lib/ductr/configuration.rb', line 103

def apply_active_job_config
  ActiveJob::Base.logger = logging.new("ActiveJob")

  active_job.each_pair do |opt, value|
    next unless value

    ActiveJob::Base.send("#{opt}=", value)
  end
end

#store(adapter, *parameters, **options) ⇒ void

This method returns an undefined value.

Configures the store instance.

Parameters:

  • adapter (Class<ActiveSupport::Cache::Store>, Symbol)

    The store adapter class

  • *parameters (Array)

    The store adapter configuration



62
63
64
65
66
# File 'lib/ductr/configuration.rb', line 62

def store(adapter, *parameters, **options)
  @store_adapter = adapter
  @store_parameters = parameters
  @store_options = options
end