Class: Bake::Recipe
- Inherits:
-
Object
- Object
- Bake::Recipe
- Defined in:
- lib/bake/recipe.rb
Overview
Structured access to an instance method in a bakefile.
Instance Attribute Summary collapse
-
#instance ⇒ Object
readonly
The Base instance that this recipe is attached to.
-
#name ⇒ Object
readonly
The name of this recipe.
Instance Method Summary collapse
-
#<=>(other) ⇒ Object
Sort by location in source file.
-
#arity ⇒ Object
The method’s arity, the required number of positional arguments.
-
#call(*arguments, **options) ⇒ Object
Call the recipe with the specified arguments and options.
-
#command ⇒ Object
The command name for this recipe.
-
#comments ⇒ Object
Any comments associated with the source code which defined the method.
-
#documentation ⇒ Object
The documentation object which provides structured access to the #comments.
-
#initialize(instance, name, method = nil) ⇒ Recipe
constructor
Initialize the recipe.
-
#method ⇒ Object
The method implementation.
-
#options? ⇒ Boolean
Whether this recipe has optional arguments.
-
#parameters ⇒ Object
The recipe’s formal parameters, if any.
-
#prepare(arguments, last_result = nil) ⇒ Object
Process command line arguments into the ordered and optional arguments.
- #required_options ⇒ Object
-
#source_location ⇒ Object
The source location of this recipe.
- #to_s ⇒ Object
-
#types ⇒ Object
The documented type signature of the recipe.
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 @types = nil @documentation = nil @method = method @arity = nil end |
Instance Attribute Details
#instance ⇒ Object (readonly)
The Base instance that this recipe is attached to.
31 32 33 |
# File 'lib/bake/recipe.rb', line 31 def instance @instance end |
#name ⇒ Object (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 |
#arity ⇒ Object
The method’s arity, the required number of positional arguments.
91 92 93 94 95 96 97 |
# File 'lib/bake/recipe.rb', line 91 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.
108 109 110 111 112 113 114 115 |
# File 'lib/bake/recipe.rb', line 108 def call(*arguments, **) if @instance.send(@name, *arguments, **) else # Ignore options... @instance.send(@name, *arguments) end end |
#command ⇒ Object
The command name for this recipe.
82 83 84 |
# File 'lib/bake/recipe.rb', line 82 def command @command ||= compute_command end |
#comments ⇒ Object
Any comments associated with the source code which defined the method.
119 120 121 |
# File 'lib/bake/recipe.rb', line 119 def comments @comments ||= read_comments end |
#documentation ⇒ Object
The documentation object which provides structured access to the #comments.
125 126 127 |
# File 'lib/bake/recipe.rb', line 125 def documentation @documentation ||= Documentation.new(self.comments) end |
#method ⇒ Object
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 if parameters = self.parameters type, name = parameters.last return type == :keyrest || type == :keyreq || type == :key end end |
#parameters ⇒ Object
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.
103 104 105 |
# File 'lib/bake/recipe.rb', line 103 def prepare(arguments, last_result = nil) Arguments.extract(self, arguments, input: last_result) end |
#required_options ⇒ Object
71 72 73 74 75 76 77 78 79 |
# File 'lib/bake/recipe.rb', line 71 def if parameters = self.parameters parameters.map do |(type, name)| if type == :keyreq name end end.compact end end |
#source_location ⇒ Object
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_s ⇒ Object
86 87 88 |
# File 'lib/bake/recipe.rb', line 86 def to_s self.command end |
#types ⇒ Object
The documented type signature of the recipe.
131 132 133 |
# File 'lib/bake/recipe.rb', line 131 def types @types ||= read_types end |