Class: CabezaDeTermo::JsonSpec::JsonExpressionExplainer

Inherits:
ExpressionWalker show all
Defined in:
lib/cabeza-de-termo/json-spec/walkers/json-expression-explainer.rb

Instance Method Summary collapse

Methods inherited from ExpressionWalker

#supressing

Constructor Details

#initializeJsonExpressionExplainer

Returns a new instance of JsonExpressionExplainer.



6
7
8
# File 'lib/cabeza-de-termo/json-spec/walkers/json-expression-explainer.rb', line 6

def initialize()
	reset
end

Instance Method Details

#append(text) ⇒ Object



164
165
166
# File 'lib/cabeza-de-termo/json-spec/walkers/json-expression-explainer.rb', line 164

def append(text)
	@text += text
end

#append_in_new_line(text) ⇒ Object



155
156
157
158
# File 'lib/cabeza-de-termo/json-spec/walkers/json-expression-explainer.rb', line 155

def append_in_new_line(text)
	cr
	append_with_tabs(text)
end

#append_tabsObject



160
161
162
# File 'lib/cabeza-de-termo/json-spec/walkers/json-expression-explainer.rb', line 160

def append_tabs()
	@tabs.times { |i| tab }
end

#append_with_tabs(text) ⇒ Object

Text



150
151
152
153
# File 'lib/cabeza-de-termo/json-spec/walkers/json-expression-explainer.rb', line 150

def append_with_tabs(text)
	append_tabs
	append(text)
end

#crObject



168
169
170
# File 'lib/cabeza-de-termo/json-spec/walkers/json-expression-explainer.rb', line 168

def cr()
	append("\n")
end

#during_tab(&block) ⇒ Object



176
177
178
179
180
# File 'lib/cabeza-de-termo/json-spec/walkers/json-expression-explainer.rb', line 176

def during_tab(&block)
	@tabs += 1
	block.call
	@tabs -= 1
end

#explain(json_walkable) ⇒ Object



19
20
21
22
23
# File 'lib/cabeza-de-termo/json-spec/walkers/json-expression-explainer.rb', line 19

def explain(json_walkable)
	reset
	walk_on(json_walkable)
	text
end

#resetObject



10
11
12
13
# File 'lib/cabeza-de-termo/json-spec/walkers/json-expression-explainer.rb', line 10

def reset
	@text = ''
	@tabs = 0
end

#tabObject



172
173
174
# File 'lib/cabeza-de-termo/json-spec/walkers/json-expression-explainer.rb', line 172

def tab()
	append("\t")
end

#textObject



15
16
17
# File 'lib/cabeza-de-termo/json-spec/walkers/json-expression-explainer.rb', line 15

def text
	@text
end

#walk_each_defined_field_of(json_object) ⇒ Object



52
53
54
55
56
57
# File 'lib/cabeza-de-termo/json-spec/walkers/json-expression-explainer.rb', line 52

def walk_each_defined_field_of(json_object)
	json_object.fields.each do |field|
		cr
		walk_on(field)
	end
end

#walk_expectation(expectation) ⇒ Object



144
145
146
# File 'lib/cabeza-de-termo/json-spec/walkers/json-expression-explainer.rb', line 144

def walk_expectation(expectation)
	append(' .' + expectation.explain)
end

#walk_expectation_runner(expectation_runner) ⇒ Object



140
141
142
# File 'lib/cabeza-de-termo/json-spec/walkers/json-expression-explainer.rb', line 140

def walk_expectation_runner(expectation_runner)
	expectation_runner.explain_with(self)
end

#walk_json_any_of(any_of) ⇒ Object



119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
# File 'lib/cabeza-de-termo/json-spec/walkers/json-expression-explainer.rb', line 119

def walk_json_any_of(any_of)
	append_with_tabs('any of')

	isFirst = true

	any_of.possible_expressions.each do |expression|
		append_in_new_line('or') unless isFirst

		during_tab do
			cr
			walk_on(expression)				
		end

		isFirst = false
	end
end

#walk_json_anything(json_anything) ⇒ Object



109
110
111
112
# File 'lib/cabeza-de-termo/json-spec/walkers/json-expression-explainer.rb', line 109

def walk_json_anything(json_anything)
	append_with_tabs('anything')
	walk_on_expectations_of(json_anything)
end

#walk_json_each(json_each_expression) ⇒ Object



91
92
93
94
95
96
97
98
# File 'lib/cabeza-de-termo/json-spec/walkers/json-expression-explainer.rb', line 91

def walk_json_each(json_each_expression)
	return unless json_each_expression.has_each_item_expression?

	cr
	during_tab do
		walk_on(json_each_expression.each_item_expression)
	end
end

#walk_json_each_field(json_each_field) ⇒ Object



59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/cabeza-de-termo/json-spec/walkers/json-expression-explainer.rb', line 59

def walk_json_each_field(json_each_field)
	append_with_tabs('each field')

	during_tab do
		append_in_new_line('name')
		walk_on( json_each_field.name_expression )
	end

	during_tab do
		append_in_new_line('value')
		cr

		during_tab do
			walk_on(json_each_field.value_expression)
		end
	end
end

#walk_json_field(json_field_expression) ⇒ Object



100
101
102
103
104
105
106
107
# File 'lib/cabeza-de-termo/json-spec/walkers/json-expression-explainer.rb', line 100

def walk_json_field(json_field_expression)
	append_with_tabs('"' + json_field_expression.name + '":')

	during_tab do
		cr
		walk_on(json_field_expression.value_expression)
	end
end

#walk_json_field_name(json_field_name) ⇒ Object



77
78
79
# File 'lib/cabeza-de-termo/json-spec/walkers/json-expression-explainer.rb', line 77

def walk_json_field_name(json_field_name)
	walk_on_expectations_of(json_field_name)
end

#walk_json_list(json_list_expression) ⇒ Object



81
82
83
84
85
86
87
88
89
# File 'lib/cabeza-de-termo/json-spec/walkers/json-expression-explainer.rb', line 81

def walk_json_list(json_list_expression)
	append_with_tabs('[')

	walk_on(json_list_expression.each_expression)

	append_in_new_line(']')

	walk_on_expectations_of(json_list_expression)
end

#walk_json_object(json_object) ⇒ Object



33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/cabeza-de-termo/json-spec/walkers/json-expression-explainer.rb', line 33

def walk_json_object(json_object)
	append_with_tabs('{')

	during_tab do
		walk_each_defined_field_of(json_object)
	end

	if json_object.has_each_field?
		during_tab do
			cr
			walk_on(json_object.each_field_expression)
		end
	end

	append_in_new_line('}')

	walk_on_expectations_of(json_object)
end

#walk_json_scalar(json_scalar) ⇒ Object



114
115
116
117
# File 'lib/cabeza-de-termo/json-spec/walkers/json-expression-explainer.rb', line 114

def walk_json_scalar(json_scalar)
	append_with_tabs('scalar')
	walk_on_expectations_of(json_scalar)
end

#walk_json_spec(json_spec) ⇒ Object



29
30
31
# File 'lib/cabeza-de-termo/json-spec/walkers/json-expression-explainer.rb', line 29

def walk_json_spec(json_spec)
	walk_on(json_spec.root_expression)
end

#walk_on(json_walkable) ⇒ Object



25
26
27
# File 'lib/cabeza-de-termo/json-spec/walkers/json-expression-explainer.rb', line 25

def walk_on(json_walkable)
	json_walkable.accept_walker(self)
end

#walk_on_expectations_of(expression) ⇒ Object



136
137
138
# File 'lib/cabeza-de-termo/json-spec/walkers/json-expression-explainer.rb', line 136

def walk_on_expectations_of(expression)
	walk_on(expression.expectations_runner)
end