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
|
# File 'lib/stratagem/extensions/red_parse.rb', line 26
def dereference
path = []
current_node = self
if (current_node.kind_of?(RedParse::VarNode))
path << current_node.name if current_node.methods_include?(:name)
else
while (current_node.methods_include?(:receiver) && (current_node = current_node.receiver) != nil)
path << current_node.name if current_node.methods_include?(:name)
end
end
previous = nil
path.reverse!
path.map! {|element|
new_val = element
if (element.downcase != element)
begin
new_val = Class.class_eval(element)
rescue
logger.error "Unable to load class #{element} in #{path.inspect}"
end
elsif (element.pluralize == element)
if (previous)
if (previous.kind_of?(Class))
model = Stratagem::Model::Application.instance.models.find {|m| m.klass == previous }
if (model)
match = previous.reflect_on_all_associations.find {|assoc| assoc.name.to_sym == element.to_sym }
if (match)
new_val = Class.class_eval(match.class_name)
else
logger.error "I give up, cannot determine what #{element} is in #{path.inspect}"
end
end
else
logger.error "Unknown previous type when trying to dereference a collection #{element} in #{path.inspect}"
end
else
logger.error "Unable to determine prior element for #{element} in #{path.inspect}"
end
elsif previous
if (['find','all','first','last'].include?(element))
new_val = previous
end
end
previous = new_val
new_val
}
path.last
end
|