class Bake::Recipe
Structured access to an instance method in a bakefile.
Constants
- COMMENT
Attributes
The {Base} instance that this recipe is attached to.
The name of this recipe.
Public Class Methods
Initialize the recipe.
@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.
# File lib/bake/recipe.rb, line 35 def initialize(instance, name, method = nil) @instance = instance @name = name @command = nil @comments = nil @types = nil @documentation = nil @method = method @arity = nil end
Public Instance Methods
Sort by location in source file.
# File lib/bake/recipe.rb, line 54 def <=> other self.source_location <=> other.source_location end
The method's arity, the required number of positional arguments.
# File lib/bake/recipe.rb, line 98 def arity if @arity.nil? @arity = method.parameters.count{|type, name| type == :req} end return @arity end
Call the recipe with the specified arguments and options.
# File lib/bake/recipe.rb, line 115 def call(*arguments, **options) if options? @instance.send(@name, *arguments, **options) else # Ignore options... @instance.send(@name, *arguments) end end
The command name for this recipe.
# File lib/bake/recipe.rb, line 89 def command @command ||= compute_command end
Any comments associated with the source code which defined the method. @returns [Array(String)] The comment lines.
# File lib/bake/recipe.rb, line 126 def comments @comments ||= read_comments end
The documentation object which provides structured access to the {comments}. @returns [Documentation]
# File lib/bake/recipe.rb, line 132 def documentation @documentation ||= Documentation.new(self.comments) end
The method implementation.
# File lib/bake/recipe.rb, line 59 def method @method ||= @instance.method(@name) end
Whether this recipe has optional arguments. @returns [Boolean]
# File lib/bake/recipe.rb, line 80 def options? if parameters = self.parameters type, name = parameters.last return type == :keyrest || type == :keyreq || type == :key end end
The recipe's formal parameters, if any. @returns [Array | Nil]
# File lib/bake/recipe.rb, line 70 def parameters parameters = method.parameters unless parameters.empty? return parameters end end
Process command line arguments into the ordered and optional arguments. @parameter arguments [Array(String)] The command line arguments @returns ordered [Array] @returns options [Hash]
# File lib/bake/recipe.rb, line 110 def prepare(arguments) Arguments.extract(self, arguments) end
The source location of this recipe.
# File lib/bake/recipe.rb, line 64 def source_location self.method.source_location end
# File lib/bake/recipe.rb, line 93 def to_s self.command end
The documented type signature of the recipe. @returns [Array] An array of {Types} instances.
# File lib/bake/recipe.rb, line 138 def types @types ||= read_types end
Private Instance Methods
# File lib/bake/recipe.rb, line 157 def compute_command path = @instance.path if path.empty? @name.to_s elsif path.last.to_sym == @name path.join(':') else (path + [@name]).join(':') end end
# File lib/bake/recipe.rb, line 144 def parse(name, value, arguments, types) if count = arguments.index(';') value = arguments.shift(count) arguments.shift end if type = types[name] value = type.parse(value) end return value end
# File lib/bake/recipe.rb, line 171 def read_comments file, line_number = self.method.source_location lines = File.readlines(file) line_index = line_number - 1 description = [] line_index -= 1 # Extract comment preceeding method: while line = lines[line_index] # \Z matches a trailing newline: if match = line.match(COMMENT) description.unshift(match[1]) else break end line_index -= 1 end return description end
# File lib/bake/recipe.rb, line 195 def read_types types = {} self.documentation.parameters do |parameter| types[parameter[:name].to_sym] = Types.parse(parameter[:type]) end return types end