Class: Brakeman::FileCache

Inherits:
Object
  • Object
show all
Defined in:
lib/brakeman/tracker/file_cache.rb

Instance Method Summary collapse

Constructor Details

#initialize(file_list = nil) ⇒ FileCache

Returns a new instance of FileCache.



3
4
5
6
7
8
9
10
11
# File 'lib/brakeman/tracker/file_cache.rb', line 3

def initialize(file_list = nil)
  @file_list = file_list || {
    controller: {},
    initializer: {},
    lib: {},
    model: {},
    template: {},
  }
end

Instance Method Details

#add_file(astfile, type) ⇒ Object



33
34
35
36
# File 'lib/brakeman/tracker/file_cache.rb', line 33

def add_file(astfile, type)
  raise "Unknown type: #{type}" unless valid_type? type
  @file_list[type][astfile.path] = astfile
end

#cached?(path) ⇒ Boolean

Returns:

  • (Boolean)


42
43
44
45
46
# File 'lib/brakeman/tracker/file_cache.rb', line 42

def cached? path
  @file_list.any? do |name, list|
    list[path]
  end
end

#controllersObject



13
14
15
# File 'lib/brakeman/tracker/file_cache.rb', line 13

def controllers
  @file_list[:controller]
end

#delete(path) ⇒ Object



48
49
50
51
52
# File 'lib/brakeman/tracker/file_cache.rb', line 48

def delete path
  @file_list.each do |name, list|
    list.delete path
  end
end

#diff(other) ⇒ Object



54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/brakeman/tracker/file_cache.rb', line 54

def diff other
  @file_list.each do |name, list|
    other_list = other.send(:"#{name}s")

    if list == other_list
      next
    else
      puts "-- #{name} --"
      puts "Old: #{other_list.keys - list.keys}"
      puts "New: #{list.keys - other_list.keys}"
    end
  end
end

#dupObject



68
69
70
71
72
73
74
75
76
77
78
79
80
81
# File 'lib/brakeman/tracker/file_cache.rb', line 68

def dup
  copy_file_list = @file_list.map do |name, list|
    copy_list = list.map do |path, astfile|
      copy_astfile = astfile.dup
      copy_astfile.ast = copy_astfile.ast.deep_clone

      [path, copy_astfile]
    end.to_h

    [name, copy_list]
  end.to_h

  FileCache.new(copy_file_list)
end

#initializersObject



17
18
19
# File 'lib/brakeman/tracker/file_cache.rb', line 17

def initializers
  @file_list[:initializer]
end

#libsObject



21
22
23
# File 'lib/brakeman/tracker/file_cache.rb', line 21

def libs
  @file_list[:lib]
end

#modelsObject



25
26
27
# File 'lib/brakeman/tracker/file_cache.rb', line 25

def models
  @file_list[:model]
end

#templatesObject



29
30
31
# File 'lib/brakeman/tracker/file_cache.rb', line 29

def templates
  @file_list[:template]
end

#valid_type?(type) ⇒ Boolean

Returns:

  • (Boolean)


38
39
40
# File 'lib/brakeman/tracker/file_cache.rb', line 38

def valid_type?(type)
  @file_list.key? type
end