Class: Pretentious::Deconstructor
- Inherits:
-
Object
- Object
- Pretentious::Deconstructor
show all
- Defined in:
- lib/pretentious/deconstructor.rb
Defined Under Namespace
Classes: Reference, UnResolved
Class Method Summary
collapse
Instance Method Summary
collapse
-
#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(indentation_level = 0, variable_map = {}, declared_names = {}, method_call_collection = [], *target_objects) ⇒ Object
-
#dfs(tree) ⇒ Object
-
#dfs_array(arr, refs) ⇒ Object
-
#dfs_hash(hash, refs) ⇒ Object
-
#get_test_class(target_object) ⇒ Object
-
#inline ⇒ Object
-
#proc_to_ruby(proc, let_variables, declared, indentation = '') ⇒ Object
-
#update_ref_counts(params_arr, method_call) ⇒ Object
Class Method Details
.block_param_names(proc) ⇒ Object
224
225
226
227
228
229
230
231
232
233
|
# File 'lib/pretentious/deconstructor.rb', line 224
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
235
236
237
238
239
240
241
242
|
# File 'lib/pretentious/deconstructor.rb', line 235
def self.block_params_generator(proc, separator = '|')
if (proc.target_proc.parameters.size > 0)
return "#{separator}#{block_param_names(proc).join(', ')}#{separator}"
end
return ''
end
|
.is_primitive?(value) ⇒ Boolean
219
220
221
222
|
# File 'lib/pretentious/deconstructor.rb', line 219
def self.is_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
|
.pick_name(variable_map, object_id, declared_names = {}, value = :no_value_passed) ⇒ Object
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
|
# File 'lib/pretentious/deconstructor.rb', line 306
def self.pick_name(variable_map, object_id, declared_names = {}, value = :no_value_passed)
var_name = "var_#{object_id}"
object_id_to_declared_names = {}
declared_names.each { |k,v|
object_id_to_declared_names[v[:object_id]] = k if v
} if declared_names
return object_id_to_declared_names[object_id] if (object_id_to_declared_names.include? object_id)
if (!variable_map.nil? && variable_map.include?(object_id))
candidate_name = variable_map[object_id].to_s
if !declared_names.include?(candidate_name)
var_name = candidate_name
declared_names[candidate_name] = {count: 1, object_id: object_id}
else
if (declared_names[candidate_name][:object_id] == object_id)
var_name = candidate_name
else
new_name = "#{candidate_name}_#{declared_names[candidate_name][:count]}"
var_name = "#{new_name}"
declared_names[candidate_name][:count]+=1
declared_names[new_name] = {count: 1, object_id: object_id}
end
end
else
if value != :no_value_passed
return Pretentious::value_ize(value, let_variables, declared_names)
end
end
var_name
end
|
.proc_body(proc, let_variables, declared, indentation = '') ⇒ Object
252
253
254
255
256
257
258
|
# File 'lib/pretentious/deconstructor.rb', line 252
def self.proc_body(proc, let_variables, declared,indentation = '')
if (proc.return_value.size == 1)
"#{indentation * 2}#{Pretentious::value_ize(proc.return_value[0], let_variables, declared)}\n"
else
"#{indentation * 2}\# Variable return values ... can't figure out what goes in here...\n"
end
end
|
Instance Method Details
#build_tree(target_object) ⇒ Object
creates a tree on how the object was created
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
171
172
173
174
|
# File 'lib/pretentious/deconstructor.rb', line 138
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
unless args.nil?
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?
else
if (self.class.is_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
end
else
tree[:composition] = target_object
end
tree
end
|
#deconstruct(method_call_collection, *target_objects) ⇒ Object
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
|
# File 'lib/pretentious/deconstructor.rb', line 177
def deconstruct(method_call_collection, *target_objects)
@declaration_order = []
@dependencies = {}
target_objects.each { |target_object|
tree = build_tree target_object
dfs(tree)
}
method_call_collection.each do |m|
update_ref_counts(m[:params], m)
end
inline
{declaration: @declaration_order, dependency: @dependencies}
end
|
#deconstruct_array(array) ⇒ Object
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
|
# File 'lib/pretentious/deconstructor.rb', line 260
def deconstruct_array(array)
composition = []
array.each { |v|
if (Pretentious::Deconstructor.is_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
}
composition
end
|
#deconstruct_hash(hash) ⇒ Object
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
|
# File 'lib/pretentious/deconstructor.rb', line 276
def deconstruct_hash(hash)
composition = {}
hash.each { |k, v|
if (Pretentious::Deconstructor.is_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
}
composition
end
|
#deconstruct_proc(proc) ⇒ Object
292
293
294
295
296
297
298
299
|
# File 'lib/pretentious/deconstructor.rb', line 292
def deconstruct_proc(proc)
if (proc.return_value.size == 1)
return build_tree(proc.return_value[0]) unless proc.return_value[0].nil?
return nil
else
nil
end
end
|
#deconstruct_to_ruby(indentation_level = 0, variable_map = {}, declared_names = {}, method_call_collection = [], *target_objects) ⇒ Object
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
|
# File 'lib/pretentious/deconstructor.rb', line 196
def deconstruct_to_ruby(indentation_level = 0, variable_map = {}, declared_names = {}, method_call_collection = [], *target_objects)
output_buffer = ""
indentation = ""
indentation_level.times {
indentation << ' '
}
target_objects.each { |target_object|
variable_map.merge!(target_object._variable_map) if target_object.methods.include?(:_variable_map) && !target_object._variable_map.nil?
}
declarations, dependencies = deconstruct method_call_collection, *target_objects
declarations[:declaration].each do |d|
unless d[:used_by] == :inline
var_name = Pretentious::Deconstructor.pick_name(variable_map, d[:id], declared_names)
output_buffer << "#{indentation}#{var_name} = #{construct(d, variable_map, declared_names, indentation)}\n"
end
end
output_buffer
end
|
#dfs(tree) ⇒ Object
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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
|
# File 'lib/pretentious/deconstructor.rb', line 57
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
if (tree[:block])
ref << dfs(tree[:block])
end
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
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
|
# File 'lib/pretentious/deconstructor.rb', line 20
def dfs_array(arr, refs)
value = []
arr.each { |v|
if Pretentious::Deconstructor.is_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
}
value
end
|
#dfs_hash(hash, refs) ⇒ Object
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
|
# File 'lib/pretentious/deconstructor.rb', line 38
def dfs_hash(hash, refs)
value = {}
hash.each { |k, v|
if Pretentious::Deconstructor.is_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
}
value
end
|
#get_test_class(target_object) ⇒ Object
301
302
303
|
# File 'lib/pretentious/deconstructor.rb', line 301
def get_test_class(target_object)
target_object.respond_to?(:test_class) ? target_object.test_class : target_object.class
end
|
#inline ⇒ Object
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
|
# File 'lib/pretentious/deconstructor.rb', line 120
def inline
@dependencies.each { |id, definition|
if (definition[:used_by].size == 1)
if (definition.include?(:value) && self.class.is_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
}
end
|
#proc_to_ruby(proc, let_variables, declared, indentation = '') ⇒ Object
244
245
246
247
248
249
250
|
# File 'lib/pretentious/deconstructor.rb', line 244
def proc_to_ruby(proc, let_variables, declared, indentation = '')
output_buffer = ""
output_buffer << "Proc.new { #{self.class.block_params_generator(proc)}\n"
output_buffer << self.class.proc_body(proc, let_variables, declared, indentation)
output_buffer << "#{indentation}}\n"
output_buffer
end
|
#update_ref_counts(params_arr, method_call) ⇒ Object
112
113
114
115
116
117
118
|
# File 'lib/pretentious/deconstructor.rb', line 112
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
|