Class: Utils::ConfigFile::BlockConfig
- 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.
Direct Known Subclasses
Classify, CodeComment, CodeIndexer, Edit, FileFinder, Probe, SshTunnel, SshTunnel::CopyPaste, SyncDir
Class Attribute Summary collapse
-
.config_settings ⇒ Object
The config_settings method provides access to the configuration settings.
Class Method Summary collapse
-
.config(name, *r) {|block| ... } ⇒ Object
The config method sets up a configuration accessor with the specified name and options.
-
.inherited(modul) ⇒ Object
The inherited method extends the module with DSL accessor functionality and calls the superclass implementation.
-
.lazy_config(name) {|default| ... } ⇒ Object
The lazy_config method configures a lazy-loaded configuration option with a default value.
Instance Method Summary collapse
-
#initialize(&block) ⇒ BlockConfig
constructor
The initialize method sets up the instance by evaluating the provided block in the instance’s context.
-
#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.
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.
175 176 177 |
# File 'lib/utils/config_file.rb', line 175 def initialize(&block) block and instance_eval(&block) end |
Class Attribute Details
.config_settings ⇒ Object
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
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.
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.
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
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.
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 |