Class: Separatum::Importers::ActiveRecord

Inherits:
Object
  • Object
show all
Defined in:
lib/separatum/importers/active_record.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(**params) ⇒ ActiveRecord

Returns a new instance of ActiveRecord.



8
9
10
11
12
13
14
15
16
17
18
19
20
21
# File 'lib/separatum/importers/active_record.rb', line 8

def initialize(**params)
  @max_depth = params[:max_depth] || 3
  @edge_classes = params[:edge_classes] || []
  @denied_classes = params[:denied_classes] || []
  @denied_class_transitions = params[:denied_class_transitions] || []

  @svg_file_name = params[:svg_file_name]
  @dot_file_name = params[:dot_file_name]

  @klass_transitions = Set[]
  @object_transitions = Set[]
  @objects = Set[]
  @time_machine = Time.now
end

Instance Attribute Details

#klass_transitionsObject (readonly)

Returns the value of attribute klass_transitions.



6
7
8
# File 'lib/separatum/importers/active_record.rb', line 6

def klass_transitions
  @klass_transitions
end

#object_transitionsObject (readonly)

Returns the value of attribute object_transitions.



6
7
8
# File 'lib/separatum/importers/active_record.rb', line 6

def object_transitions
  @object_transitions
end

Instance Method Details

#act(object, depth = 1) ⇒ Object



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
# File 'lib/separatum/importers/active_record.rb', line 42

def act(object, depth = 1)
  object.class.reflections.each do |association_name, reflection|
    # Skip Through associations
    next if reflection.is_a?(::ActiveRecord::Reflection::ThroughReflection)

    # Skip empty associations
    next if object.send(association_name).nil?

    # Limit depth
    next if depth >= @max_depth

    case reflection.macro
    when :belongs_to, :has_one
      next_object = object.send(association_name)
      next unless process_link(object, next_object)
      puts "#{depth}#{' ' * depth}[#{reflection.macro}] #{object.class} -> #{next_object.class}"
      act(next_object, depth + 1)

    when :has_many, :has_and_belongs_to_many
      new_objects = object.send(association_name)
      new_objects.each do |next_object|
        next unless process_link(object, next_object)
        puts "#{depth}#{' ' * depth}[#{reflection.macro}] #{object.class} -> #{next_object.class}"
        act(next_object, depth + 1)
      end

    else
      fail("Unknown association `#{reflection.macro.inspect} :#{association_name}' in #{object.class}")
    end
  end
end

#call(*objects) ⇒ Object



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/separatum/importers/active_record.rb', line 23

def call(*objects)
  result = []
  objects.flatten.each do |object|
    @objects = Set[object]
    act(object)
    result += @objects.to_a
  end

  if @svg_file_name || @dot_file_name
    if defined? ::GraphViz
      ::Separatum::GraphViz::Drawer.new(svg_file_name: @svg_file_name, dot_file_name: @dot_file_name).draw(@object_transitions)
    else
      fail("GraphViz const is undefined. Use ruby-graphviz gem")
    end
  end

  ::Separatum::Converters::Object2Hash.new(common_fields: { _time_machine: @time_machine }).(result.flatten.uniq)
end


74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
# File 'lib/separatum/importers/active_record.rb', line 74

def process_link(prev_object, next_object)
  # Skip already added object
  return if @objects.include?(next_object)

  # Skip already processed link
  return if @object_transitions.include?([prev_object, next_object])

  # Stuck on edge classes
  return if @edge_classes.include?(prev_object.class)

  # Will not go to denied classes
  return if @denied_classes.include?(next_object.class)

  # Will not go through denied transitions
  return if @denied_class_transitions.include?([prev_object.class, next_object.class])

  @objects.add(next_object)
  @klass_transitions.add([prev_object.class, next_object.class])
  @object_transitions.add([prev_object, next_object])

  true
end