Class: RedParse::Node

Inherits:
Object show all
Defined in:
lib/stratagem/extensions/red_parse.rb

Instance Method Summary collapse

Instance Method Details

#dereferenceObject

iterates the node and figures out the class of the leaf that a call is talking about



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
  # recursion is evil
  path = []
  current_node = self
  # puts "current:#{current_node.inspect}"
  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
  # p path
  previous = nil
  path.reverse!
  path.map! {|element|
    new_val = element
    if (element.downcase != element)
      # class?
      begin
        new_val = Class.class_eval(element)
      rescue
        logger.error "Unable to load class #{element} in #{path.inspect}"
      end
    elsif (element.pluralize == element)
      # collection?
      if (previous)
        if (previous.kind_of?(Class))
          # look up the loaded model of the 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
      # call to the class / object in the previous statement
      if (['find','all','first','last'].include?(element))
        # then expect to receive an instance of the previous type
        new_val = previous
      end
    end
    previous = new_val
    new_val
  }
  path.last
end

#loggerObject



83
84
85
# File 'lib/stratagem/extensions/red_parse.rb', line 83

def logger
  Rails.logger
end

#talking_about?(model) ⇒ Boolean

Returns:

  • (Boolean)


2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
# File 'lib/stratagem/extensions/red_parse.rb', line 2

def talking_about?(model)
  talking_about = false

  if self.receiver.methods_include?(:name)
    if ((self.receiver) && self.receiver.name == model.klass.name)
#        puts "\treceiver name #{self.receiver.name} is equal to #{model.klass.name}"
      talking_about = true
    elsif (self.receiver)
      name = self.receiver.name
      # if it's lowercase then it's possibly referencing an association
      if (name.downcase == name)
        # look through the models to see if one has an association with this name and class
        Stratagem::Model::Application.instance.models.each do |app_model|
          talking_about = app_model.association_match?(name, model)
          break if talking_about
        end
      end
    end
  end
  
  talking_about
end