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
  @types = 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.



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, **options)
  if options?
    @instance.send(@name, *arguments, **options)
  else
    # Ignore options...
    @instance.send(@name, *arguments)
  end
end

#commandObject

The command name for this recipe.



82
83
84
# File 'lib/bake/recipe.rb', line 82

def command
  @command ||= compute_command
end

#commentsObject

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

#documentationObject

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

#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.

Returns:

  • (Boolean)


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

def options?
  if parameters = self.parameters
    type, name = parameters.last
    
    return type == :keyrest || type == :keyreq || type == :key
  end
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.



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_optionsObject



71
72
73
74
75
76
77
78
79
# File 'lib/bake/recipe.rb', line 71

def required_options
  if parameters = self.parameters
    parameters.map do |(type, name)|
      if type == :keyreq
        name
      end
    end.compact
  end
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



86
87
88
# File 'lib/bake/recipe.rb', line 86

def to_s
  self.command
end

#typesObject

The documented type signature of the recipe.



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

def types
  @types ||= read_types
end