Build::EnvironmentSourceBuildEnvironmentConstructor

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 environment Environment

The environment to populate.

parameter proxy Object | 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 name Symbol

The method name to check.

parameter include_private Boolean

Whether to include private methods.

returns Boolean

true if 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 name Symbol

The key name or proxy method name.

parameter args Array

Positional arguments: a single value, multiple values, or none (with a block).

parameter options Hash

Keyword arguments forwarded to the proxy.

parameter block Proc

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

true if 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 key Symbol

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 options Hash

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 name Symbol

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 name Symbol

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 name Symbol

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 klass Class

The class to associate with the key.

parameter name Symbol

The key to define.

parameter block Proc

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