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

#arityObject



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

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

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



96
97
98
99
100
101
102
103
# File 'lib/bake/recipe.rb', line 96

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

#commandObject



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

def command
	@command ||= compute_command
end

#descriptionObject



113
114
115
# File 'lib/bake/recipe.rb', line 113

def description
	@description ||= read_description
end

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



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

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

#methodObject



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

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

#options?Boolean

Returns:

  • (Boolean)


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

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

#parametersObject



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

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

#prepare(arguments) ⇒ Object



72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
# File 'lib/bake/recipe.rb', line 72

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



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

def to_s
	self.command
end