Class: Pretentious::Deconstructor
- Inherits:
-
Object
- Object
- Pretentious::Deconstructor
show all
- Defined in:
- lib/pretentious/deconstructor.rb
Overview
Deconstructor - decompose an object into its parts
Defined Under Namespace
Classes: Reference, UnResolved
Class Method Summary
collapse
Instance Method Summary
collapse
-
#build_output(context, indentation_level, declarations) ⇒ Object
-
#build_tree(target_object) ⇒ Object
creates a tree on how the object was created.
-
#deconstruct(method_call_collection, *target_objects) ⇒ Object
-
#deconstruct_array(array) ⇒ Object
-
#deconstruct_hash(hash) ⇒ Object
-
#deconstruct_proc(proc) ⇒ Object
-
#deconstruct_to_ruby(context, indentation_level = 0, *target_objects) ⇒ Object
-
#dfs(tree) ⇒ Object
-
#dfs_array(arr, refs) ⇒ Object
-
#dfs_hash(hash, refs) ⇒ Object
-
#generate_declarations(context, method_call_collection, *target_objects) ⇒ Object
-
#get_test_class(target_object) ⇒ Object
-
#inline ⇒ Object
-
#proc_to_ruby(context, proc, indentation = '') ⇒ Object
-
#update_ref_counts(params_arr, method_call) ⇒ Object
Class Method Details
.block_param_names(proc) ⇒ Object
222
223
224
225
226
227
228
229
|
# File 'lib/pretentious/deconstructor.rb', line 222
def self.block_param_names(proc)
parameters_to_join = []
parameters = proc.target_proc.parameters
parameters.each { |p| parameters_to_join << p[1].to_s }
parameters_to_join
end
|
.block_params_generator(proc, separator = '|') ⇒ Object
231
232
233
234
235
236
237
|
# File 'lib/pretentious/deconstructor.rb', line 231
def self.block_params_generator(proc, separator = '|')
if proc.target_proc.parameters.size > 0
return "#{separator}#{block_param_names(proc).join(', ')}#{separator}"
end
''
end
|
.primitive?(value) ⇒ Boolean
217
218
219
220
|
# File 'lib/pretentious/deconstructor.rb', line 217
def self.primitive?(value)
value.is_a?(String) || value.is_a?(Fixnum) || value.is_a?(TrueClass) || value.is_a?(FalseClass) ||
value.is_a?(NilClass) || value.is_a?(Symbol) || value.is_a?(Class)
end
|
.proc_body(context, proc, indentation = '') ⇒ Object
247
248
249
250
251
252
253
|
# File 'lib/pretentious/deconstructor.rb', line 247
def self.proc_body(context, proc, indentation = '')
if proc.return_value.size == 1
"#{indentation} #{context.value_of(proc.return_value[0])}\n"
else
"#{indentation} \# Variable return values ... can't figure out what goes in here...\n"
end
end
|
Instance Method Details
#build_output(context, indentation_level, declarations) ⇒ Object
197
198
199
200
201
202
203
204
205
206
207
208
209
210
|
# File 'lib/pretentious/deconstructor.rb', line 197
def build_output(context, indentation_level, declarations)
output_buffer = ''
indentation = ''
indentation_level.times { indentation << ' ' }
declarations[:declaration].select { |d| d[:used_by] != :inline }.each do |d|
if !context.was_declared_previously?(d[:id])
var_name = context.pick_name(d[:id])
output_buffer << "#{indentation}#{var_name} = #{construct(context, d, indentation)}\n"
elsif context.was_declared_previously?(d[:id])
context.register_instance_variable(d[:id])
end
end
output_buffer
end
|
#build_tree(target_object) ⇒ Object
creates a tree on how the object was created
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
|
# File 'lib/pretentious/deconstructor.rb', line 137
def build_tree(target_object)
tree = { class: get_test_class(target_object), id: target_object.object_id, composition: [] }
if target_object.is_a? Array
tree[:composition] = deconstruct_array(target_object)
elsif target_object.is_a? Hash
tree[:composition] = deconstruct_hash(target_object)
elsif target_object.is_a? Pretentious::RecordedProc
tree[:composition] = deconstruct_proc(target_object)
tree[:given_block] = target_object.given_block?
tree[:recorded_proc] = target_object
tree[:id] = target_object.target_proc.object_id
tree[:block_params] = self.class.block_param_names(target_object)
elsif target_object.methods.include? :_get_init_arguments
args = target_object._get_init_arguments
if args.nil?
if self.class.primitive?(target_object)
tree[:composition] = target_object
elsif target_object.class == File
tree[:composition] << build_tree(target_object.path)
else
tree[:composition] = UnResolved.new(target_object)
end
else
tree[:params_types] = args[:params_types]
args[:params].each { |p| tree[:composition] << build_tree(p) }
tree[:block] = build_tree(args[:block]) unless args[:block].nil?
end
else
tree[:composition] = target_object
end
tree
end
|
#deconstruct(method_call_collection, *target_objects) ⇒ Object
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
|
# File 'lib/pretentious/deconstructor.rb', line 172
def deconstruct(method_call_collection, *target_objects)
@declaration_order = []
@dependencies = {}
target_objects.each do |target_object|
tree = build_tree target_object
dfs(tree)
end
method_call_collection.each do |m|
update_ref_counts(m[:params], m)
end
inline
{ declaration: @declaration_order, dependency: @dependencies }
end
|
#deconstruct_array(array) ⇒ Object
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
|
# File 'lib/pretentious/deconstructor.rb', line 255
def deconstruct_array(array)
composition = []
array.each do |v|
if Pretentious::Deconstructor.primitive?(v)
composition << v
elsif v.is_a? Hash
composition << deconstruct_hash(v)
elsif v.is_a? Array
composition << deconstruct_array(v)
else
composition << Reference.new(build_tree(v))
end
end
composition
end
|
#deconstruct_hash(hash) ⇒ Object
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
|
# File 'lib/pretentious/deconstructor.rb', line 271
def deconstruct_hash(hash)
composition = {}
hash.each do |k, v|
if Pretentious::Deconstructor.primitive?(v)
composition[k] = v
elsif v.is_a? Hash
composition[k] = deconstruct_hash(v)
elsif v.is_a? Array
composition[k] = deconstruct_array(v)
else
composition[k] = Reference.new(build_tree(v))
end
end
composition
end
|
#deconstruct_proc(proc) ⇒ Object
287
288
289
290
|
# File 'lib/pretentious/deconstructor.rb', line 287
def deconstruct_proc(proc)
return nil if proc.return_value.size != 1
return build_tree(proc.return_value[0]) unless proc.return_value[0].nil?
end
|
#deconstruct_to_ruby(context, indentation_level = 0, *target_objects) ⇒ Object
212
213
214
215
|
# File 'lib/pretentious/deconstructor.rb', line 212
def deconstruct_to_ruby(context, indentation_level = 0, *target_objects)
declarations, _dependencies = generate_declarations context, [], *target_objects
build_output(context, indentation_level, declarations)
end
|
#dfs(tree) ⇒ Object
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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
|
# File 'lib/pretentious/deconstructor.rb', line 59
def dfs(tree)
if !tree.is_a? Hash
value = tree
definition = { id: value.object_id,
class: tree.class,
value: value,
used_by: [] }
unless @dependencies.include? value.object_id
@dependencies[value.object_id] = definition
@declaration_order << definition
end
value.object_id
else
ref = []
definition = { id: tree[:id],
class: tree[:class],
params_types: tree[:params_types],
used_by: [] }
if tree[:class] == Hash
definition[:value] = dfs_hash(tree[:composition], ref)
elsif tree[:class] == Array
definition[:value] = dfs_array(tree[:composition], ref)
elsif tree[:class] == Pretentious::RecordedProc
definition[:recorded_proc] = tree[:recorded_proc]
if !tree[:composition].nil?
ref << dfs(tree[:composition])
else
dfs(tree[:composition])
end
elsif tree[:composition].is_a? Array
tree[:composition].each { |t| ref << dfs(t) }
else
ref << dfs(tree[:composition])
end
ref << dfs(tree[:block]) if tree[:block]
definition[:ref] = ref
unless @dependencies.include? tree[:id]
@declaration_order << definition
@dependencies[tree[:id]] = definition
ref.each { |r| @dependencies[r][:used_by] << definition }
end
tree[:id]
end
end
|
#dfs_array(arr, refs) ⇒ Object
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
|
# File 'lib/pretentious/deconstructor.rb', line 22
def dfs_array(arr, refs)
value = []
arr.each do |v|
if Pretentious::Deconstructor.primitive?(v)
value << v
elsif v.is_a? Hash
value << dfs_hash(v, refs)
elsif v.is_a? Array
value << dfs_array(v, refs)
elsif v.is_a? Reference
refs << v.tree[:id]
value << Reference.new(dfs(v.tree))
elsif value << v
end
end
value
end
|
#dfs_hash(hash, refs) ⇒ Object
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
|
# File 'lib/pretentious/deconstructor.rb', line 40
def dfs_hash(hash, refs)
value = {}
hash.each do |k, v|
if Pretentious::Deconstructor.primitive?(v)
value[k] = v
elsif v.is_a? Hash
value[k] = dfs_hash(v, refs)
elsif v.is_a? Array
value[k] = dfs_array(v, refs)
elsif v.is_a? Reference
refs << v.tree[:id]
value[k] = Reference.new(dfs(v.tree))
else
value[k] = v
end
end
value
end
|
#generate_declarations(context, method_call_collection, *target_objects) ⇒ Object
190
191
192
193
194
195
|
# File 'lib/pretentious/deconstructor.rb', line 190
def generate_declarations(context, method_call_collection, *target_objects)
target_objects.each do |target_object|
context.merge_variable_map(target_object)
end
deconstruct method_call_collection, *target_objects
end
|
#get_test_class(target_object) ⇒ Object
292
293
294
|
# File 'lib/pretentious/deconstructor.rb', line 292
def get_test_class(target_object)
target_object.respond_to?(:test_class) ? target_object.test_class : target_object.class
end
|
#inline ⇒ Object
122
123
124
125
126
127
128
129
130
131
132
133
134
|
# File 'lib/pretentious/deconstructor.rb', line 122
def inline
@dependencies.each do |id, definition|
next if definition[:used_by].size != 1
next if !definition.include?(:value) || !self.class.primitive?(definition[:value])
ref = definition[:used_by][0]
definition[:used_by] = :inline
references = ref[:ref]
if references
new_ref = references.collect { |c| c == id ? definition : c }
ref[:ref] = new_ref
end
end
end
|
#proc_to_ruby(context, proc, indentation = '') ⇒ Object
239
240
241
242
243
244
245
|
# File 'lib/pretentious/deconstructor.rb', line 239
def proc_to_ruby(context, proc, indentation = '')
output_buffer = ''
output_buffer << "proc { #{self.class.block_params_generator(proc)}\n"
output_buffer << self.class.proc_body(context, proc, indentation)
output_buffer << "#{indentation}}\n"
output_buffer
end
|
#update_ref_counts(params_arr, method_call) ⇒ Object
114
115
116
117
118
119
120
|
# File 'lib/pretentious/deconstructor.rb', line 114
def update_ref_counts(params_arr, method_call)
params_arr.each do |p|
if @dependencies.include? p.object_id
@dependencies[p.object_id][:used_by] << method_call
end
end
end
|