Class: MotionBlender::Analyzer::Require

Inherits:
Object
  • Object
show all
Includes:
ActiveSupport::Callbacks
Defined in:
lib/motion_blender/analyzer/require.rb

Constant Summary collapse

TOKENS =
i(motion_require require_relative require)

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(loader, method, arg) ⇒ Require

Returns a new instance of Require.



14
15
16
17
18
# File 'lib/motion_blender/analyzer/require.rb', line 14

def initialize loader, method, arg
  @loader = loader
  @method = method
  @arg = arg
end

Instance Attribute Details

#argObject

Returns the value of attribute arg.



12
13
14
# File 'lib/motion_blender/analyzer/require.rb', line 12

def arg
  @arg
end

#loaderObject

Returns the value of attribute loader.



12
13
14
# File 'lib/motion_blender/analyzer/require.rb', line 12

def loader
  @loader
end

#methodObject

Returns the value of attribute method.



12
13
14
# File 'lib/motion_blender/analyzer/require.rb', line 12

def method
  @method
end

#traceObject

Returns the value of attribute trace.



12
13
14
# File 'lib/motion_blender/analyzer/require.rb', line 12

def trace
  @trace
end

Class Method Details

.acceptable?(method) ⇒ Boolean

Returns:

  • (Boolean)


70
71
72
# File 'lib/motion_blender/analyzer/require.rb', line 70

def self.acceptable? method
  TOKENS.include?(method)
end

Instance Method Details

#candidatesObject



30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/motion_blender/analyzer/require.rb', line 30

def candidates
  path =
    if i(motion_require require_relative).include? method
      Pathname.new(loader).dirname.join(arg)
    else
      Pathname.new(arg)
    end
  dirs = path.relative? && load_path || ['']
  exts = path.extname.empty? ? ['', '.rb'] : ['']
  Enumerator.new do |y|
    dirs.product(exts).each do |dir, ext|
      y << Pathname.new(dir).join("#{path}#{ext}")
    end
  end
end

#excluded?Boolean

Returns:

  • (Boolean)


64
65
66
67
68
# File 'lib/motion_blender/analyzer/require.rb', line 64

def excluded?
  MotionBlender.config.builtin_features.include?(arg) ||
    MotionBlender.config.excepted_files.include?(arg) ||
    MotionBlender.config.excepted_files.include?(file)
end

#explicit_relative(path) ⇒ Object



46
47
48
# File 'lib/motion_blender/analyzer/require.rb', line 46

def explicit_relative path
  path.to_s.sub(%r{^(?![\./])}, './')
end

#fileObject



20
21
22
# File 'lib/motion_blender/analyzer/require.rb', line 20

def file
  @file ||= resolve_path
end

#load_pathObject



54
55
56
57
58
# File 'lib/motion_blender/analyzer/require.rb', line 54

def load_path
  if uses_load_path?
    MotionBlender.config.motion_dirs + $LOAD_PATH
  end
end

#match?(arg_or_file) ⇒ Boolean

Returns:

  • (Boolean)


60
61
62
# File 'lib/motion_blender/analyzer/require.rb', line 60

def match? arg_or_file
  arg == arg_or_file || file == arg_or_file
end

#resolve_pathObject



24
25
26
27
28
# File 'lib/motion_blender/analyzer/require.rb', line 24

def resolve_path
  path = candidates.find(&:file?)
  fail LoadError, "not found `#{arg}'" unless path
  explicit_relative path
end

#uses_load_path?Boolean

Returns:

  • (Boolean)


50
51
52
# File 'lib/motion_blender/analyzer/require.rb', line 50

def uses_load_path?
  method == :require
end