Class: Utils::ConfigFile::BlockConfig

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

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.



131
132
133
# File 'lib/utils/config_file.rb', line 131

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



120
121
122
# File 'lib/utils/config_file.rb', line 120

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



104
105
106
107
108
109
# File 'lib/utils/config_file.rb', line 104

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.



85
86
87
88
# File 'lib/utils/config_file.rb', line 85

def inherited(modul)
  modul.extend DSLKit::DSLAccessor
  super
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.



144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
# File 'lib/utils/config_file.rb', line 144

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