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 ⇒ Array(String)
Any comments associated with the source code which defined the method.
-
#documentation ⇒ Documentation
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 ⇒ Array | Nil
The recipe’s formal parameters, if any.
-
#prepare(arguments) ⇒ Object
Process command line arguments into the ordered and optional arguments.
-
#source_location ⇒ Object
The source location of this recipe.
- #to_s ⇒ Object
-
#types ⇒ Array
The documented type signature of the recipe.
Constructor Details
#initialize(instance, name, method = nil) ⇒ Recipe
Initialize the recipe.
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
#instance ⇒ Object (readonly)
The Base instance that this recipe is attached to.
45 46 47 |
# File 'lib/bake/recipe.rb', line 45 def instance @instance end |
#name ⇒ Object (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 |
#arity ⇒ Object
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, **) if @instance.send(@name, *arguments, **) else # Ignore options... @instance.send(@name, *arguments) end end |
#command ⇒ Object
The command name for this recipe.
86 87 88 |
# File 'lib/bake/recipe.rb', line 86 def command @command ||= compute_command end |
#comments ⇒ Array(String)
Any comments associated with the source code which defined the method.
156 157 158 |
# File 'lib/bake/recipe.rb', line 156 def comments @comments ||= read_comments end |
#documentation ⇒ Documentation
The documentation object which provides structured access to the #comments.
162 163 164 |
# File 'lib/bake/recipe.rb', line 162 def documentation @documentation ||= Documentation.new(self.comments) end |
#method ⇒ Object
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.
77 78 79 80 81 82 83 |
# File 'lib/bake/recipe.rb', line 77 def if parameters = self.parameters type, name = parameters.last return type == :keyrest || type == :keyreq || type == :key end end |
#parameters ⇒ Array | Nil
The recipe’s formal parameters, if any.
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.
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 = [] = {} 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 [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, end |
#source_location ⇒ Object
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_s ⇒ Object
90 91 92 |
# File 'lib/bake/recipe.rb', line 90 def to_s self.command end |
#types ⇒ Array
The documented type signature of the recipe.
168 169 170 |
# File 'lib/bake/recipe.rb', line 168 def types @types ||= read_types end |