class Bake::Recipe

Structured access to an instance method in a bakefile.

Constants

COMMENT

Attributes

instance[R]

The {Base} instance that this recipe is attached to.

name[R]

The name of this recipe.

Public Class Methods

new(instance, name, method = nil) click to toggle source

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

<=>(other) click to toggle source

Sort by location in source file.

# File lib/bake/recipe.rb, line 54
def <=> other
        self.source_location <=> other.source_location
end
arity() click to toggle source

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(*arguments, **options) click to toggle source

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
command() click to toggle source

The command name for this recipe.

# File lib/bake/recipe.rb, line 89
def command
        @command ||= compute_command
end
comments() click to toggle source

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
documentation() click to toggle source

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
method() click to toggle source

The method implementation.

# File lib/bake/recipe.rb, line 59
def method
        @method ||= @instance.method(@name)
end
options?() click to toggle source

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
parameters() click to toggle source

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
prepare(arguments) click to toggle source

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
source_location() click to toggle source

The source location of this recipe.

# File lib/bake/recipe.rb, line 64
def source_location
        self.method.source_location
end
to_s() click to toggle source
# File lib/bake/recipe.rb, line 93
def to_s
        self.command
end
types() click to toggle source

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

compute_command() click to toggle source
# 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
parse(name, value, arguments, types) click to toggle source
# 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
read_comments() click to toggle source
# 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
read_types() click to toggle source
# 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