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.



18
19
20
21
22
23
24
25
26
27
28
# File 'lib/bake/recipe.rb', line 18

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

Instance Attribute Details

#instanceObject (readonly)

The Base instance that this recipe is attached to.



31
32
33
# File 'lib/bake/recipe.rb', line 31

def instance
  @instance
end

#nameObject (readonly)

The name of this recipe.



34
35
36
# File 'lib/bake/recipe.rb', line 34

def name
  @name
end

Instance Method Details

#<=>(other) ⇒ Object

Sort by location in source file.



37
38
39
# File 'lib/bake/recipe.rb', line 37

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

#arityObject

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



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

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

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

Call the recipe with the specified arguments and options. If the recipe does not accept options, they will be ignored.



115
116
117
118
119
120
121
122
# File 'lib/bake/recipe.rb', line 115

def call(*arguments, **options, &block)
  if options.any? and self.options?
    @instance.send(@name, *arguments, **options, &block)
  else
    # Ignore options...
    @instance.send(@name, *arguments, &block)
  end
end

#commandObject

The command name for this recipe.



88
89
90
# File 'lib/bake/recipe.rb', line 88

def command
  @command ||= compute_command
end

#commentsObject

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



126
127
128
# File 'lib/bake/recipe.rb', line 126

def comments
  @comments ||= read_comments
end

#documentationObject

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



132
133
134
# File 'lib/bake/recipe.rb', line 132

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

#methodObject

The method implementation.



42
43
44
# File 'lib/bake/recipe.rb', line 42

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

#options?Boolean

Whether this recipe has optional arguments.



63
64
65
66
67
68
69
# File 'lib/bake/recipe.rb', line 63

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

#output?Boolean

If a recipe produces output, we do not need to invoke the default output command.



73
74
75
# File 'lib/bake/recipe.rb', line 73

def output?
  @instance.output?(self)
end

#parametersObject

The recipe’s formal parameters, if any.



53
54
55
56
57
58
59
# File 'lib/bake/recipe.rb', line 53

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

#prepare(arguments, last_result = nil) ⇒ Object

Process command line arguments into the ordered and optional arguments.



109
110
111
# File 'lib/bake/recipe.rb', line 109

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

#required_optionsObject



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

def required_options
  if parameters = self.parameters
    parameters.map do |(type, name)|
      if type == :keyreq
        name
      end
    end.compact
  end
end

#signatureObject Also known as: types

The documented type signature of the recipe.



138
139
140
# File 'lib/bake/recipe.rb', line 138

def signature
  @signature ||= read_signature
end

#source_locationObject

The source location of this recipe.



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

def source_location
  self.method.source_location
end

#to_sObject



92
93
94
# File 'lib/bake/recipe.rb', line 92

def to_s
  self.command
end