Class: Utils::ConfigFile::BlockConfig

Inherits:
Object
  • Object
show all
Defined in:
lib/utils/config_file.rb

Overview

Base class for defining configuration blocks with DSL accessors.

This class provides a foundation for creating configuration classes that support dynamic attribute definition through DSL-style accessor methods. It includes functionality for registering configuration settings and generating Ruby code representations of the configuration state.

Class Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(&block) ⇒ BlockConfig

The initialize method sets up the instance by evaluating the provided block in the instance’s context.

This method allows for dynamic configuration of the object by executing the given block within the instance’s scope, enabling flexible initialization patterns.

Parameters:

  • block (Proc)

    the block to be evaluated for instance setup



175
176
177
# File 'lib/utils/config_file.rb', line 175

def initialize(&block)
  block and instance_eval(&block)
end

Class Attribute Details

.config_settingsObject

The config_settings method provides access to the configuration settings.

This method returns the configuration settings stored in the instance variable, allowing for reading and modification of the object’s configuration state.

instance variable

Returns:

  • (Object)

    the current configuration settings stored in the



164
165
166
# File 'lib/utils/config_file.rb', line 164

def config_settings
  @config_settings
end

Class Method Details

.config(name, *r) {|block| ... } ⇒ Object

The config method sets up a configuration accessor with the specified name and options.

This method registers a new configuration setting by adding it to the list of configuration settings and then creates an accessor for it using the dsl_accessor method, allowing for easy retrieval and assignment of configuration values.

Parameters:

  • name (Object)

    the name of the configuration setting

  • r (Array)

    additional arguments passed to the dsl_accessor method

Yields:

  • (block)

    optional block to be passed to the dsl_accessor method

Returns:

  • (Object)

    returns self to allow for method chaining



127
128
129
130
131
132
# File 'lib/utils/config_file.rb', line 127

def config(name, *r, &block)
  self.config_settings ||= []
  config_settings << name.to_sym
  dsl_accessor name, *r, &block
  self
end

.inherited(modul) ⇒ Object

The inherited method extends the module with DSL accessor functionality and calls the superclass implementation.

Parameters:

  • modul (Module)

    the module that inherited this class



108
109
110
111
# File 'lib/utils/config_file.rb', line 108

def inherited(modul)
  modul.extend DSLKit::DSLAccessor
  super
end

.lazy_config(name) {|default| ... } ⇒ Object

The lazy_config method configures a lazy-loaded configuration option with a default value.

This method registers a new configuration setting that will be initialized lazily, meaning the default value or the set value is only computed when the configuration is actually accessed. It adds the setting to the list of configuration settings and creates a lazy accessor for it.

the configuration

Parameters:

  • name (Object)

    the name of the configuration setting to define

Yields:

  • (default)

    optional block that provides the default value for

Returns:

  • (Object)

    returns self to allow for method chaining



148
149
150
151
152
153
# File 'lib/utils/config_file.rb', line 148

def lazy_config(name, &default)
  self.config_settings ||= []
  config_settings << name.to_sym
  dsl_lazy_accessor(name, &default)
  self
end

Instance Method Details

#to_ruby(depth = 0) ⇒ String

The to_ruby method generates a Ruby configuration block representation by recursively processing the object’s configuration settings and their values. It creates a nested structure with proper indentation and formatting suitable for configuration files.

Parameters:

  • depth (Integer) (defaults to: 0)

    the current nesting depth for indentation purposes

Returns:

  • (String)

    a formatted Ruby string representing the configuration block



188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
# File 'lib/utils/config_file.rb', line 188

def to_ruby(depth = 0)
  result = ''
  result << ' ' * 2 * depth <<
    "#{self.class.name[/::([^:]+)\z/, 1].underscore} do\n"
  for name in self.class.config_settings
    value = __send__(name)
    if value.respond_to?(:to_ruby)
      result << ' ' * 2 * (depth + 1) << value.to_ruby(depth + 1)
    else
      result << ' ' * 2 * (depth + 1) <<
        "#{name} #{Array(value).map(&:inspect) * ', '}\n"
    end
  end
  result << ' ' * 2 * depth << "end\n"
end