Class: Literal::Attributable::Formatter

Inherits:
Formatter show all
Includes:
AbstractNodeMethods
Defined in:
lib/literal/attributable/formatter.rb

Defined Under Namespace

Modules: AbstractNodeMethods

Constant Summary

Constants inherited from Formatter

Formatter::INDENTATION_CHARACTER

Instance Method Summary collapse

Methods included from Modifiers::Abstract

#abstract, #abstract!, #abstract?, #abstract_methods, #included

Methods inherited from Formatter

#comment, #indent, #initialize, #newline, #text

Methods inherited from Visitor

#visit, #visit_each

Constructor Details

This class inherits a constructor from Literal::Formatter

Instance Method Details

#Access(node) ⇒ Object



15
16
17
18
19
20
# File 'lib/literal/attributable/formatter.rb', line 15

def Access(node)
	visit node.collection
	text "["
	visit node.key
	text "]"
end

#Assignment(node) ⇒ Object



22
23
24
25
26
# File 'lib/literal/attributable/formatter.rb', line 22

def Assignment(node)
	visit node.left
	text " = "
	visit node.right
end

#AssignSchema(node) ⇒ Object



28
29
30
# File 'lib/literal/attributable/formatter.rb', line 28

def AssignSchema(node)
	text "@literal_attributes = self.class.literal_attributes"
end

#AttributeCoercion(node) ⇒ Object



32
33
34
# File 'lib/literal/attributable/formatter.rb', line 32

def AttributeCoercion(node)
	text "#{node.attribute.name} = @literal_attributes[:#{node.attribute.name}].coerce(#{node.attribute.name}, context: self)"
end

#BlockParam(node) ⇒ Object



36
37
38
# File 'lib/literal/attributable/formatter.rb', line 36

def BlockParam(node)
	text "&#{node.attribute.name}"
end

#Def(node) ⇒ Object



40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/literal/attributable/formatter.rb', line 40

def Def(node)
	text "#{node.visibility} " if node.visibility
	text "def #{node.name}"
	if node.params&.any?
		text "("
		visit_each(node.params) { text ", " }
		text ")"
	end

	indent do
		visit_each(node.body) { newline; newline }
	end

	newline
	text "end"
	newline
end

#DefaultAssignment(node) ⇒ Object



58
59
60
61
62
63
64
65
66
67
# File 'lib/literal/attributable/formatter.rb', line 58

def DefaultAssignment(node)
	text "if Literal::Null == #{node.attribute.escaped}"

	indent do
		text "#{node.attribute.escaped} = @literal_attributes[:#{node.attribute.name}].default_value"
	end

	newline
	text "end"
end

#HashLiteral(node) ⇒ Object



69
70
71
72
73
74
75
76
77
78
79
# File 'lib/literal/attributable/formatter.rb', line 69

def HashLiteral(node)
	text "{"

	indent do
		visit_each(node.mappings) { text ","; newline }
	end

	newline

	text "}"
end

#InitializerCallback(node) ⇒ Object



81
82
83
84
# File 'lib/literal/attributable/formatter.rb', line 81

def InitializerCallback(node)
	comment "Callback for your own setup logic"
	text "after_initialization if respond_to?(:after_initialization)"
end

#KeywordEscape(node) ⇒ Object



86
87
88
# File 'lib/literal/attributable/formatter.rb', line 86

def KeywordEscape(node)
	text "#{node.attribute.escaped} = binding.local_variable_get(:#{node.attribute.name})"
end

#KeywordParam(node) ⇒ Object



90
91
92
93
94
95
96
97
98
# File 'lib/literal/attributable/formatter.rb', line 90

def KeywordParam(node)
	if node.attribute.default?
		text "#{node.attribute.name}: Literal::Null"
	elsif node.attribute.type === nil
		text "#{node.attribute.name}: nil"
	else
		text "#{node.attribute.name}:"
	end
end

#KeywordSplat(node) ⇒ Object



100
101
102
# File 'lib/literal/attributable/formatter.rb', line 100

def KeywordSplat(node)
	text "**#{node.attribute.name}"
end

#Mapping(node) ⇒ Object



104
105
106
107
108
# File 'lib/literal/attributable/formatter.rb', line 104

def Mapping(node)
	visit node.left
	text " => "
	visit node.right
end

#PositionalParam(node) ⇒ Object



110
111
112
113
114
115
116
# File 'lib/literal/attributable/formatter.rb', line 110

def PositionalParam(node)
	if node.default
		text "#{node.name} = #{node.default}"
	else
		text node.name
	end
end

#PositionalSplat(node) ⇒ Object



118
119
120
# File 'lib/literal/attributable/formatter.rb', line 118

def PositionalSplat(node)
	text "*#{node.attribute.name}"
end

#Ref(node) ⇒ Object



122
123
124
# File 'lib/literal/attributable/formatter.rb', line 122

def Ref(node)
	text node.name
end

#Section(node) ⇒ Object



126
127
128
129
# File 'lib/literal/attributable/formatter.rb', line 126

def Section(node)
	comment node.name if node.name
	visit_each(node.body) { newline; newline } if node.body
end

#Symbol(node) ⇒ Object



131
132
133
# File 'lib/literal/attributable/formatter.rb', line 131

def Symbol(node)
	text ":#{node.name}"
end

#TypeCheck(node) ⇒ Object



135
136
137
138
139
140
141
142
143
144
# File 'lib/literal/attributable/formatter.rb', line 135

def TypeCheck(node)
	text "unless @literal_attributes[:#{node.attribute_name}].type === #{node.variable_name}"

	indent do
		text "raise Literal::TypeError.expected(#{node.variable_name}, to_be_a: @literal_attributes[:#{node.attribute_name}].type)"
	end

	newline
	text "end"
end