/
method_template.rb
81 lines (68 loc) · 2.13 KB
/
method_template.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
module GirFFI
module Builders
# Encapsulates the general structure of generated methods, consisting of a
# preparation phase where arguments are converted to the form needed by the
# main call of the method, the main call itself, a post-processing phase
# where the return values and any 'out' arguments of the main call are
# converted into a form suitable for returning, and finally the returning
# of said values to the caller.
#
# The different method builders collaborate with MethodTemplate to build
# the desired method.
class MethodTemplate
def initialize builder, argument_builder_collection
@builder = builder
@argument_builder_collection = argument_builder_collection
end
def method_definition
code = "def #{qualified_method_name}"
code << "(#{method_arguments.join(', ')})" if method_arguments.any?
method_lines.each { |line| code << "\n #{line}" }
code << "\nend\n"
end
private
def qualified_method_name
"#{@builder.singleton_method? ? 'self.' : ''}#{@builder.method_name}"
end
def method_arguments
@builder.method_arguments
end
def method_lines
method_preparation +
parameter_preparation +
invocation +
return_value_conversion +
result
end
def method_preparation
@builder.preparation
end
def parameter_preparation
@argument_builder_collection.parameter_preparation
end
def invocation
if result_name_list.empty?
plain_invocation
else
capturing_invocation
end
end
def return_value_conversion
@argument_builder_collection.return_value_conversion
end
def result
@builder.result
end
def result_name_list
@result_name_list ||=
@argument_builder_collection.capture_variable_names.join(', ')
end
def capturing_invocation
["#{result_name_list} = #{@builder.invocation}"]
end
def plain_invocation
[@builder.invocation]
end
end
end
end