Class: Mutant::Zombifier::File

Inherits:
Object
  • Object
show all
Includes:
Adamantium::Flat
Defined in:
lib/mutant/zombifier.rb

Overview

File containing source beeing zombified

Constant Summary collapse

CACHE =
{}
RECEIVER_INDEX =
0
SELECTOR_INDEX =
1
ARGUMENT_INDEX =
2..-1.freeze

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.find(logical_name) ⇒ File

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Find file

Parameters:

  • logical_name (String)

Returns:

  • (File)

    if found

Raises:

  • (RuntimeError)

    if file cannot be found



129
130
131
132
133
134
# File 'lib/mutant/zombifier.rb', line 129

def self.find(logical_name)
  return if IGNORE.include?(logical_name)
  CACHE.fetch(logical_name) do
    CACHE[logical_name] = find_uncached(logical_name)
  end
end

.find_uncached(logical_name) ⇒ File?

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Find file without cache

Parameters:

  • logical_name (String)

Returns:

  • (File)

    if found

  • (nil)

    otherwise



148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
# File 'lib/mutant/zombifier.rb', line 148

def self.find_uncached(logical_name)
  file_name =
    if logical_name.end_with?('.rb')
      logical_name
    else
      "#{logical_name}.rb"
    end

  $LOAD_PATH.each do |path|
    path = Pathname.new(path).join(file_name)
    if path.file?
      return new(path)
    end
  end

  $stderr.puts "Cannot find file #{file_name} in $LOAD_PATH"
  nil
end

Instance Method Details

#nodeParser::AST::Node

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Return node

Returns:

  • (Parser::AST::Node)


184
185
186
# File 'lib/mutant/zombifier.rb', line 184

def node
  Parser::CurrentRuby.parse(::File.read(source_path))
end

#required_pathsEnumerable<String>

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Return required paths

Returns:

  • (Enumerable<String>)


199
200
201
202
203
204
205
206
207
208
209
210
211
# File 'lib/mutant/zombifier.rb', line 199

def required_paths
  require_nodes.map do |node|
    arguments = node.children[ARGUMENT_INDEX]
    unless arguments.length == 1
      raise "Require node with not exactly one argument: #{node}"
    end
    argument = arguments.first
    unless argument.type == :str
      raise "Require argument is not a literal string: #{argument}"
    end
    argument.children.first
  end
end

#subjectSubject

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Return subject

Returns:



173
174
175
# File 'lib/mutant/zombifier.rb', line 173

def subject
  Subject.new(self)
end

#zombifyself

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Zombify contents of file

Returns:

  • (self)


106
107
108
109
110
111
112
113
114
# File 'lib/mutant/zombifier.rb', line 106

def zombify
  subject.zombify
  required_paths.each do |path|
    file = File.find(path)
    next unless file
    file.zombify
  end
  self
end