Class: Utils::ConfigFile::BlockConfig

Inherits:
Object
  • Object
show all
Defined in:
lib/utils/config_file/block_config.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.



81
82
83
# File 'lib/utils/config_file/block_config.rb', line 81

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



70
71
72
# File 'lib/utils/config_file/block_config.rb', line 70

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.

Yields:

  • (block)

    optional block to be passed to the dsl_accessor method



33
34
35
36
37
38
# File 'lib/utils/config_file/block_config.rb', line 33

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.



14
15
16
17
# File 'lib/utils/config_file/block_config.rb', line 14

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

Yields:

  • (default)

    optional block that provides the default value for



54
55
56
57
58
59
# File 'lib/utils/config_file/block_config.rb', line 54

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.



94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
# File 'lib/utils/config_file/block_config.rb', line 94

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