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(instance, name, method = nil) ⇒ Recipe

Returns a new instance of Recipe.



27
28
29
30
31
32
33
34
35
36
# File 'lib/bake/recipe.rb', line 27

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

Instance Attribute Details

#instanceObject (readonly)

Returns the value of attribute instance.



38
39
40
# File 'lib/bake/recipe.rb', line 38

def instance
  @instance
end

#nameObject (readonly)

Returns the value of attribute name.



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

def name
  @name
end

Instance Method Details

#<=>(other) ⇒ Object



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

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

#arityObject



77
78
79
80
81
82
83
# File 'lib/bake/recipe.rb', line 77

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

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



122
123
124
125
126
127
128
129
# File 'lib/bake/recipe.rb', line 122

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

#commandObject



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

def command
	@command ||= compute_command
end

#descriptionObject



139
140
141
# File 'lib/bake/recipe.rb', line 139

def description
	@description ||= read_description
end

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



131
132
133
134
135
136
137
# File 'lib/bake/recipe.rb', line 131

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

#methodObject



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

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

#options?Boolean

Returns:

  • (Boolean)


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

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

#parametersObject



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) ⇒ Object



85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
# File 'lib/bake/recipe.rb', line 85

def prepare(arguments)
	offset = 0
	ordered = []
	options = {}
	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
			
			options[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, options
end

#source_locationObject



49
50
51
# File 'lib/bake/recipe.rb', line 49

def source_location
	self.method.source_location
end

#to_sObject



73
74
75
# File 'lib/bake/recipe.rb', line 73

def to_s
	self.command
end

#typesObject



143
144
145
# File 'lib/bake/recipe.rb', line 143

def types
	@types ||= read_types
end