forked from vsapronov/gopoetry
/
method.go
89 lines (75 loc) · 1.63 KB
/
method.go
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
82
83
84
85
86
87
88
package ruby
type MethodDeclaration struct {
name string
args []Writable
noArgs bool
body Writable
paramPerLine bool
}
func (self *MethodDeclaration) AddParams(params ...Writable) *MethodDeclaration {
self.args = append(self.args, params...)
return self
}
func (self *MethodDeclaration) NoParams() *MethodDeclaration {
self.noArgs = true
return self
}
func (self *MethodDeclaration) ParamPerLine() *MethodDeclaration {
self.paramPerLine = true
return self
}
func (self *MethodDeclaration) Body() *StatementsDeclaration {
body := Statements()
self.body = body
return body
}
func (self *MethodDeclaration) Arg(name string) *ArgDeclaration {
param := Arg(name)
self.AddParams(param)
return param
}
func (self *MethodDeclaration) KeywordArg(name string) *ArgDeclaration {
param := KeywordArg(name)
self.AddParams(param)
return param
}
func Method(name string) *MethodDeclaration {
return &MethodDeclaration{
name: name,
args: []Writable{},
body: nil,
paramPerLine: false,
}
}
func (self *MethodDeclaration) WriteCode(writer CodeWriter) {
writer.Write("def " + self.name)
if !self.noArgs {
writer.Write("(")
if self.paramPerLine {
writer.Indent()
writer.Eol()
}
for i, param := range self.args {
param.WriteCode(writer)
if i < len(self.args)-1 {
writer.Write(",")
}
if self.paramPerLine {
writer.Eol()
} else {
if i < len(self.args)-1 {
writer.Write(" ")
}
}
}
if self.paramPerLine {
writer.UnIndent()
}
writer.Write(")")
}
writer.Begin("")
if self.body != nil {
self.body.WriteCode(writer)
}
writer.End()
}