Class: Bake::Recipe

Inherits:
Object
  • Object
show all
Defined in:
lib/bake/recipe.rb

Overview

Structured access to an instance method in a bakefile.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(instance, name, method = nil) ⇒ Recipe

Initialize the recipe.

Parameters:

  • instance (Base)

    The instance this recipe is attached to.

  • name (String)

    The method name.

  • method (Method | Nil) (defaults to: nil)

    The method if already known.



32
33
34
35
36
37
38
39
40
41
42
# File 'lib/bake/recipe.rb', line 32

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

Instance Attribute Details

#instanceObject (readonly)

The Base instance that this recipe is attached to.



45
46
47
# File 'lib/bake/recipe.rb', line 45

def instance
  @instance
end

#nameObject (readonly)

The name of this recipe.



48
49
50
# File 'lib/bake/recipe.rb', line 48

def name
  @name
end

Instance Method Details

#<=>(other) ⇒ Object

Sort by location in source file.



51
52
53
# File 'lib/bake/recipe.rb', line 51

def <=> other
	self.source_location <=> other.source_location
end

#arityObject

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



95
96
97
98
99
100
101
# File 'lib/bake/recipe.rb', line 95

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

#call(*arguments, **options) ⇒ Object

Call the recipe with the specified arguments and options.



145
146
147
148
149
150
151
152
# File 'lib/bake/recipe.rb', line 145

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

#commandObject

The command name for this recipe.



86
87
88
# File 'lib/bake/recipe.rb', line 86

def command
	@command ||= compute_command
end

#commentsArray(String)

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

Returns:

  • (Array(String))

    The comment lines.



156
157
158
# File 'lib/bake/recipe.rb', line 156

def comments
	@comments ||= read_comments
end

#documentationDocumentation

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

Returns:



162
163
164
# File 'lib/bake/recipe.rb', line 162

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

#methodObject

The method implementation.



56
57
58
# File 'lib/bake/recipe.rb', line 56

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

#options?Boolean

Whether this recipe has optional arguments.

Returns:

  • (Boolean)


77
78
79
80
81
82
83
# File 'lib/bake/recipe.rb', line 77

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

#parametersArray | Nil

The recipe’s formal parameters, if any.

Returns:

  • (Array | Nil)


67
68
69
70
71
72
73
# File 'lib/bake/recipe.rb', line 67

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

#prepare(arguments) ⇒ Object

Process command line arguments into the ordered and optional arguments.

Parameters:

  • arguments (Array(String))

    The command line arguments



107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
# File 'lib/bake/recipe.rb', line 107

def prepare(arguments)
	offset = 0
	ordered = []
	options = {}
	parameters = method.parameters.dup
	types = self.types
	
	while argument = arguments.first
		name, value = argument.split('=', 2)
		
		if name and value
			# Consume it:
			arguments.shift
			
			if type = types[name.to_sym]
				value = type.parse(value)
			end
			
			options[name.to_sym] = value
		elsif ordered.size < self.arity
			_, name = parameters.shift
			value = arguments.shift
			
			if type = types[name]
				value = type.parse(value)
			end
			
			# Consume it:
			ordered << value
		else
			break
		end
	end
	
	return ordered, options
end

#source_locationObject

The source location of this recipe.



61
62
63
# File 'lib/bake/recipe.rb', line 61

def source_location
	self.method.source_location
end

#to_sObject



90
91
92
# File 'lib/bake/recipe.rb', line 90

def to_s
	self.command
end

#typesArray

The documented type signature of the recipe.

Returns:

  • (Array)

    An array of Types instances.



168
169
170
# File 'lib/bake/recipe.rb', line 168

def types
	@types ||= read_types
end