Class: PartialFinder::LinkSet

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Defined in:
lib/partial_finder/link_set.rb

Overview

LinkSet is a simple collection of { render_child => render_parent } file paths. Given a partial path and a file root to search, it will recursively search for and collect render links for that partial. This is the base structure that is used to generate and print full render chains.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(partial_path, search_root, debug_mode: false) ⇒ LinkSet

Accepts a file path to a partial and a file path used as the search root. The search root can be expanded or shrunk as needed but should stay within the Rails root. It can be flexible since the size of the search directory can drastically effect the performance of grep. It is recommended to use rails_root/app.

Raises:



19
20
21
22
23
24
25
26
27
# File 'lib/partial_finder/link_set.rb', line 19

def initialize(partial_path, search_root, debug_mode: false)
  raise NonPartialArgument.new(partial_path) unless Formatter.is_partial?(partial_path)

  @debug_mode = debug_mode
  @path = partial_path
  @search_root = search_root
  @values = []
  collect_links(path)
end

Instance Attribute Details

#debug_modeObject (readonly)

Returns the value of attribute debug_mode.



10
11
12
# File 'lib/partial_finder/link_set.rb', line 10

def debug_mode
  @debug_mode
end

#pathObject (readonly)

Returns the value of attribute path.



10
11
12
# File 'lib/partial_finder/link_set.rb', line 10

def path
  @path
end

#search_rootObject (readonly)

Returns the value of attribute search_root.



10
11
12
# File 'lib/partial_finder/link_set.rb', line 10

def search_root
  @search_root
end

#valuesObject (readonly)

Returns the value of attribute values.



10
11
12
# File 'lib/partial_finder/link_set.rb', line 10

def values
  @values
end

Class Method Details

.files_that_reference(path, search_root, debug_mode: false) ⇒ Object

Returns a list of files that reference the given partial. Non-partials are not searched for as render chains terminate in non-partials (ie, if a view or controller has been found, the render chain can halt).



33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/partial_finder/link_set.rb', line 33

def self.files_that_reference(path, search_root, debug_mode: false)
  if Formatter.is_partial? path
    # Scans for instances of the partial being explicitly rendered.
    # For example, given the path app/views/orders/_foo.html.erb, the
    # resulting string used by grep would be:
    # "partial: [\"']orders/foo[\"']"
    # This accounts for use of " and ' in the reference.
    #
    # TODO: Make this single line? Character escaping this properly isn't fun
    term = <<~STR.remove("\n")
"partial: [\\"']#{Formatter.path_to_ref(path)}[\\"']"
STR

    puts "Running grep: 'cd #{search_root} && grep -rl #{term}'" if debug_mode
    `cd #{search_root} && grep -rl #{term}`
      .split("\n")
      .map{ |a| Formatter.fix_path(a) }
  else
    []
  end
end