Class: Mergit::Processor

Inherits:
Object
  • Object
show all
Defined in:
lib/mergit/processor.rb

Overview

The class that actually does the merge processing.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(search_path, replacements, options) ⇒ Processor

Returns a new instance of Processor.

Parameters:

  • search_path (Array<Pathname, String>)

    The list of directories to search.

  • replacements (Hash)

    A list of keywords to replace.

  • options (Hash)

    Either :filename or :string should be set.



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/mergit/processor.rb', line 19

def initialize search_path, replacements, options
  @search_path = search_path.map{|p| Pathname.new p}.freeze
  @replacements = replacements.freeze
  @visited_files = []

  @output = StringIO.new
  begin
    if options.key?(:filename)
      scan_file(Pathname.new(options[:filename]).realpath)
    elsif options.key?(:string)
      scan(options[:string])
    end
  ensure
    @output.close unless options[:do_not_close]
  end
end

Instance Attribute Details

#replacementsHash (readonly)

Returns A frozen hash with the rules for replacements.

Returns:

  • (Hash)

    A frozen hash with the rules for replacements.



14
15
16
# File 'lib/mergit/processor.rb', line 14

def replacements
  @replacements
end

#search_pathArray<Pathname> (readonly)

Returns A frozen array of Pathnames.

Returns:

  • (Array<Pathname>)

    A frozen array of Pathnames



11
12
13
# File 'lib/mergit/processor.rb', line 11

def search_path
  @search_path
end

Instance Method Details

#emit(string) ⇒ Nil

Sends a string to the #output

Parameters:

  • string (String)

    The string to send to #output.

Returns:

  • (Nil)


143
144
145
# File 'lib/mergit/processor.rb', line 143

def emit string
  @output.puts string
end

#find_requirement(lib_name) ⇒ Nil, Pathname

Finds a library using the #search_path

Parameters:

  • lib_name (String)

    The name of the library to look for.

Returns:

  • (Nil, Pathname)

    Returns nil if it isn't found or a Pathname if it is found.



40
41
42
43
44
45
46
# File 'lib/mergit/processor.rb', line 40

def find_requirement lib_name
  @search_path.each do |directory|
    possible_path = directory + "#{lib_name}.rb"
    return possible_path.realpath if possible_path.file?
  end
  nil
end

#find_requirement!(lib_name) ⇒ Pathname

Finds a library using the #search_path

This is identical to #find_requirement except it raises RequirementNotFound if it fails to find the library.

Parameters:

  • lib_name (String)

    The name of the library to look for.

Returns:

  • (Pathname)

    Returns the Pathname of the library.

Raises:

See Also:



58
59
60
61
62
# File 'lib/mergit/processor.rb', line 58

def find_requirement! lib_name
  find_requirement(lib_name).tap do |retval|
    raise Mergit::RequirementNotFound.new("Unabled to find require'd file: #{lib_name}") if retval.nil?
  end
end

#outputString

The resulting processed output.

Returns:

  • (String)


134
135
136
137
# File 'lib/mergit/processor.rb', line 134

def output
  @output.close unless @output.closed?
  @final_output ||= @output.string
end

#scan(string) ⇒ Nil

Scans a string

It splits a string up into individual line via #string_split and passes them to #scan_line.

Parameters:

  • string (String)

    The string to parse.

Returns:

  • (Nil)


119
120
121
# File 'lib/mergit/processor.rb', line 119

def scan string
  string_split(string).each { |line| scan_line line }
end

#scan_file(filename) ⇒ Nil

Scans an entire file

It passes each line of the file to #scan_line for parsing.

Parameters:

  • filename (Pathname)

    The file to scan.

Returns:

  • (Nil)


96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
# File 'lib/mergit/processor.rb', line 96

def scan_file filename
  relative_filename = if filename.relative?
                        filename
                      else
                        filename.relative_path_from(Pathname.pwd)
                      end
  if @visited_files.include? relative_filename
    return
  else
    @visited_files << relative_filename
  end
  emit "### MERGIT: Start of '#{relative_filename}'"
  filename.readlines.each { |line| scan_line line }
  emit "### MERGIT: End of '#{relative_filename}'"
end

#scan_line(line) ⇒ Nil

Scans a single line of the file.

It looks for things that need to be changed, and #emits the resulting (changed) line.

Parameters:

  • line (String)

    The line to parse

Returns:

  • (Nil)


71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
# File 'lib/mergit/processor.rb', line 71

def scan_line line
  line.chomp!
  if line =~ /#\s*MERGIT:\s*skip\s*$/
    nil # do nothing
  elsif line =~ /^\s*require\s+'([^']+)'\s*$/ or line =~ /^\s*require\s+"([^"]+)"\s*$/
    requirement = find_requirement($1)
    if requirement.nil?
      emit line
    else
      scan_file requirement
    end
  else
    replacements.each_key do |string_to_replace|
      line.gsub!(string_to_replace, replacements[string_to_replace])
    end
    emit line
  end
end

#string_split(string) ⇒ Array<String>

Split a string into lines.

Parameters:

  • string (String)

    The string to split into lines.

Returns:

  • (Array<String>)

    The split up string.



127
128
129
# File 'lib/mergit/processor.rb', line 127

def string_split string
  string.split(/\n|\r\n/)
end