BakeSourceBakeRecipe

class Recipe

Structured access to an instance method in a bakefile.

Definitions

def initialize(instance, name, method = nil)

Initialize the recipe.

Signature

parameter instance Base

The instance this recipe is attached to.

parameter name String

The method name.

parameter method Method | Nil

The method if already known.

Implementation

def initialize(instance, name, method = nil)
	@instance = instance
	@name = name
	@command = nil
	@comments = nil
	@types = nil
	@documentation = nil
	
	@method = method
	@arity = nil
end

attr :instance

The class Bake::Base instance that this recipe is attached to.

attr :name

The name of this recipe.

def <=> other

Sort by location in source file.

Implementation

def <=> other
	(self.source_location || []) <=> (other.source_location || [])
end

def method

The method implementation.

Implementation

def method
	@method ||= @instance.method(@name)
end

def source_location

The source location of this recipe.

Implementation

def source_location
	self.method.source_location
end

def parameters

The recipe's formal parameters, if any.

Signature

returns Array | Nil

Implementation

def parameters
	parameters = method.parameters
	
	unless parameters.empty?
		return parameters
	end
end

def options?

Whether this recipe has optional arguments.

Signature

returns Boolean

Implementation

def options?
	if parameters = self.parameters
		type, name = parameters.last
		
		return type == :keyrest || type == :keyreq || type == :key
	end
end

def command

The command name for this recipe.

Implementation

def command
	@command ||= compute_command
end

def arity

The method's arity, the required number of positional arguments.

Implementation

def arity
	if @arity.nil?
		@arity = method.parameters.count{|type, name| type == :req}
	end
	
	return @arity
end

def prepare(arguments, last_result = nil)

Process command line arguments into the ordered and optional arguments.

Signature

parameter arguments Array(String)

The command line arguments

Implementation

def prepare(arguments, last_result = nil)
	Arguments.extract(self, arguments, input: last_result)
end

def call(*arguments, **options)

Call the recipe with the specified arguments and options.

Implementation

def call(*arguments, **options)
	if options?
		@instance.send(@name, *arguments, **options)
	else
		# Ignore options...
		@instance.send(@name, *arguments)
	end
end

def comments

Any comments associated with the source code which defined the method.

Signature

returns Array(String)

The comment lines.

Implementation

def comments
	@comments ||= read_comments
end

def documentation

The documentation object which provides structured access to the Bake::Recipe#comments.

Signature

returns Documentation

Implementation

def documentation
	@documentation ||= Documentation.new(self.comments)
end

def types

The documented type signature of the recipe.

Signature

returns Array

An array of module Bake::Types instances.

Implementation

def types
	@types ||= read_types
end