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
# File 'lib/bake/recipe.rb', line 23

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

Instance Attribute Details

#nameObject (readonly)

Returns the value of attribute name.



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

def name
  @name
end

#scopeObject (readonly)

Returns the value of attribute scope.



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

def scope
  @scope
end

Instance Method Details

#arityObject



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

def arity
	method.arity
end

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



91
92
93
94
95
96
97
98
# File 'lib/bake/recipe.rb', line 91

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

#commandObject



55
56
57
# File 'lib/bake/recipe.rb', line 55

def command
	@command ||= compute_command
end

#descriptionObject



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

def description
	@description ||= read_description
end

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



100
101
102
103
104
105
106
# File 'lib/bake/recipe.rb', line 100

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

#methodObject



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

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

#options?Boolean

Returns:

  • (Boolean)


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

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

#parametersObject



39
40
41
42
43
44
45
# File 'lib/bake/recipe.rb', line 39

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

#prepare(arguments) ⇒ Object



67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
# File 'lib/bake/recipe.rb', line 67

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

#to_sObject



59
60
61
# File 'lib/bake/recipe.rb', line 59

def to_s
	self.command
end