Class: Bake::Recipe

Inherits:
Object
  • Object
show all
Defined in:
lib/bake/recipe.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(scope, name, method = nil) ⇒ Recipe

Returns a new instance of Recipe.



23
24
25
26
27
28
29
30
31
# File 'lib/bake/recipe.rb', line 23

def initialize(scope, name, method = nil)
  @scope = scope
  @name = name
  @command = nil
  @description = nil
  
  @method = method
  @arity = nil
end

Instance Attribute Details

#nameObject (readonly)

Returns the value of attribute name.



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

def name
  @name
end

#scopeObject (readonly)

Returns the value of attribute scope.



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

def scope
  @scope
end

Instance Method Details

#<=>(other) ⇒ Object



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

def <=> other
  self.source_location <=> other.source_location
end

#arityObject



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

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

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



104
105
106
107
108
109
110
111
# File 'lib/bake/recipe.rb', line 104

def call(*arguments, **options)
  if options?
    @scope.send(@name, *arguments, **options)
  else
    # Ignore options...
    @scope.send(@name, *arguments)
  end
end

#commandObject



64
65
66
# File 'lib/bake/recipe.rb', line 64

def command
  @command ||= compute_command
end

#descriptionObject



121
122
123
# File 'lib/bake/recipe.rb', line 121

def description
  @description ||= read_description
end

#explain(context, *arguments, **options) ⇒ Object



113
114
115
116
117
118
119
# File 'lib/bake/recipe.rb', line 113

def explain(context, *arguments, **options)
  if options?
    puts "#{self}(#{arguments.join(", ")}, #{options.inspect})"
  else
    puts "#{self}(#{arguments.join(", ")})"
  end
end

#methodObject



40
41
42
# File 'lib/bake/recipe.rb', line 40

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

#options?Boolean

Returns:

  • (Boolean)


56
57
58
59
60
61
62
# File 'lib/bake/recipe.rb', line 56

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

#parametersObject



48
49
50
51
52
53
54
# File 'lib/bake/recipe.rb', line 48

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

#prepare(arguments) ⇒ Object



80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
# File 'lib/bake/recipe.rb', line 80

def prepare(arguments)
  offset = 0
  ordered = []
  options = {}
  
  while argument = arguments.first
    name, value = argument.split('=', 2)
    
    if name and value
      # Consume it:
      arguments.shift
      
      options[name.to_sym] = value
    elsif ordered.size < self.arity
      # Consume it:
      ordered << arguments.shift
    else
      break
    end
  end
  
  return ordered, options
end

#source_locationObject



44
45
46
# File 'lib/bake/recipe.rb', line 44

def source_location
  self.method.source_location
end

#to_sObject



68
69
70
# File 'lib/bake/recipe.rb', line 68

def to_s
  self.command
end