class Utils::ConfigFile::BlockConfig
generating Ruby code representations of the configuration state.
includes functionality for registering configuration settings and
support dynamic attribute definition through DSL-style accessor methods. It
This class provides a foundation for creating configuration classes that
Base class for defining configuration blocks with DSL accessors.
def config(name, *r, &block)
-
(Object)
- returns self to allow for method chaining
Other tags:
- Yield: - optional block to be passed to the dsl_accessor method
Parameters:
-
r
(Array
) -- additional arguments passed to the dsl_accessor method -
name
(Object
) -- the name of the configuration setting
def config(name, *r, &block) self.config_settings ||= [] config_settings << name.to_sym dsl_accessor name, *r, &block self end
def inherited(modul)
-
modul
(Module
) -- the module that inherited this class
def inherited(modul) modul.extend DSLKit::DSLAccessor super end
def initialize(&block)
-
block
(Proc
) -- the block to be evaluated for instance setup
def initialize(&block) block and instance_eval(&block) end
def lazy_config(name, &default)
-
(Object)
- returns self to allow for method chaining
Other tags:
- Yield: - optional block that provides the default value for
Parameters:
-
name
(Object
) -- the name of the configuration setting to define
def lazy_config(name, &default) self.config_settings ||= [] config_settings << name.to_sym dsl_lazy_accessor(name, &default) self end
def to_ruby(depth = 0)
-
(String)
- a formatted Ruby string representing the configuration block
Parameters:
-
depth
(Integer
) -- the current nesting depth for indentation purposes
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