class Constructor
Represents a DSL proxy used to populate an environment using a block-based interface.
Definitions
def initialize(environment, proxy = nil)
Initialize the constructor with an environment and an optional proxy object.
Signature
-
parameter
environmentEnvironment The environment to populate.
-
parameter
proxyObject | Nil An optional proxy for delegating unknown method calls.
Implementation
def initialize(environment, proxy = nil)
@environment = environment
@proxy = proxy
end
def respond_to?(name, include_private = false)
Check whether the constructor responds to the given method name.
Signature
-
parameter
nameSymbol The method name to check.
-
parameter
include_privateBoolean Whether to include private methods.
-
returns
Boolean trueif the environment includes the key or the proxy responds to the method.
Implementation
def respond_to?(name, include_private = false)
@environment.include?(name) || @proxy&.respond_to?(name, include_private) || super
end
def method_missing(name, *args, **options, &block)
Dynamically set environment keys or delegate to the proxy object.
Signature
-
parameter
nameSymbol The key name or proxy method name.
-
parameter
argsArray Positional arguments: a single value, multiple values, or none (with a block).
-
parameter
optionsHash Keyword arguments forwarded to the proxy.
-
parameter
blockProc A block used as the value when no positional arguments are given.
Implementation
def method_missing(name, *args, **options, &block)
if options.empty?
if args.empty? and block_given?
@environment[name] = block
return name
elsif !args.empty?
if args.count == 1
@environment[name] = args.first
else
@environment[name] = args
end
return name
end
end
if @proxy
# This is a bit of a hack, but I'm not sure if there is a better way.
if options.empty?
@proxy.send(name, *args, &block)
else
@proxy.send(name, *args, **options, &block)
end
else
super
end
end
def respond_to(*args)
Delegate respond_to to the proxy if available.
Signature
-
returns
Boolean trueif the constructor or proxy responds to the given arguments.
Implementation
def respond_to(*args)
super or @proxy&.respond_to(*args)
end
def [](key)
Retrieve the value of a key from the underlying environment.
Signature
-
parameter
keySymbol The key to look up.
-
returns
Object | Nil The value associated with the key.
Implementation
def [] key
@environment[key]
end
def parent
Return the parent of the underlying environment.
Signature
-
returns
Environment | Nil The parent environment.
Implementation
def parent
@environment.parent
end
def hash(**options)
Create an OpenStruct from the given keyword options.
Signature
-
parameter
optionsHash The key-value pairs for the struct.
-
returns
OpenStruct A new struct with the given options.
Implementation
def hash(**options)
OpenStruct.new(options)
end
def default(name)
Mark the current value of a key as the default, wrapping it in a Default struct.
Signature
-
parameter
nameSymbol The key whose value should be treated as a default.
-
returns
Symbol The key name.
Implementation
def default(name)
@environment[name] = Default.new(@environment[name])
return name
end
def replace(name)
Mark the current value of a key for replacement, wrapping it in a Replace struct.
Signature
-
parameter
nameSymbol The key whose value should be replaced.
-
returns
Symbol The key name.
Implementation
def replace(name)
@environment[name] = Replace.new(@environment[name])
return name
end
def append(name)
Convert the current value of a key to an array to allow appending.
Signature
-
parameter
nameSymbol The key whose value should be converted to an array.
-
returns
Symbol The key name.
Implementation
def append(name)
@environment[name] = Array(@environment[name])
return name
end
def define(klass, name, &block)
Associate a class and configuration block with a key as a Define struct.
Signature
-
parameter
klassClass The class to associate with the key.
-
parameter
nameSymbol The key to define.
-
parameter
blockProc A block used to configure the class instance.
-
returns
Symbol The key name.
Implementation
def define(klass, name, &block)
@environment[name] = Define.new(klass, &block)
return name
end