Class: Relation

Inherits:
Explorer show all
Defined in:
lib/exploration/relation.rb

Instance Attribute Summary

Attributes inherited from Explorer

#resolve_strategy

Instance Method Summary collapse

Instance Method Details

#each_by_type(types, relation, sexp, context = nil, &block) ⇒ Object

TODO these two methods are only used by aggregation and dependency, find a better place for them



6
7
8
9
10
11
12
13
14
15
# File 'lib/exploration/relation.rb', line 6

def each_by_type types, relation, sexp, context=nil, &block
  already_explored = []
  sexp.deep_each do |sub_sexp|
    if types.include? sub_sexp.sexp_type
      # by exploring :colon2 first, we won't pick up any :const that was inside a :colon2
      explore_entity_sexp :colon2, sexp, relation, context, already_explored, &block
      explore_entity_sexp :const, sexp, relation, context, already_explored, &block
    end
  end
end

#explore_entity_sexp(type, sexp, relationship, context, already_explored, &block) ⇒ Object



17
18
19
20
21
22
23
24
25
# File 'lib/exploration/relation.rb', line 17

def explore_entity_sexp type, sexp, relationship, context, already_explored, &block
  sexp.each_of_type(type) do |node|
    if !already_explored.include?(node)
      name, namespace, explored = get_name_and_namespace node
      block.call context, relationship, { name: name, type: :class, namespace: namespace }
      already_explored.concat explored
    end
  end
end

#get_name_and_namespace(sexp) ⇒ Object

Extracts name and namespace of the Sexp object.

params
  • sexp is a Sexp object

precondition
  • sexp is of the form: s(:colon2, s(:colon2 …, s(:const, FirstNamespace), SecondNamespace, …, ClassName).

  • :colon2 sexp’s are optional but there must be a :const

return
  • name as String

  • namespace as Array of String objects

  • explored Sexp objects as an Array



37
38
39
40
41
42
43
44
45
46
# File 'lib/exploration/relation.rb', line 37

def get_name_and_namespace sexp
  name = if sexp.first == :colon2
           sexp.rest.rest.first.to_s
         else
           sexp.rest.first.to_s
         end
  namespace, explored = get_namespace sexp
  explored << sexp
  return name, namespace, explored
end

#get_namespace(sexp) ⇒ Object

Extracts the namespace from the Sexp object.

precondition
  • sexp is of the form: s(:colon2, s(:colon2 …, s(:const, FirstNamespace), SecondNamespace, …, ClassName).

  • :colon2 sexp’s are optional but there must be a :const

params
  • sexp is a Sexp object

return
  • Array of String objects

  • Array of Sexp objects that where encountered

example
  • s(:colon2, s(:colon2, s(:const, :Baz), :Foo), :Bar) ==> [‘Baz’, ‘Foo’]



59
60
61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/exploration/relation.rb', line 59

def get_namespace sexp
  current = sexp
  type = current.first
  namespace = []
  subs = []
  while type == :colon2
    namespace.unshift current.rest.rest.first.to_s
    current = current.rest.first
    subs << current
    type = current.first
  end
  namespace.unshift current.rest.first.to_s
  return namespace[0...namespace.length-1], subs # return all except for the last element
end