Class: Reditor::LibraryLocator

Inherits:
Object
  • Object
show all
Includes:
BundlerSupport
Defined in:
lib/reditor/library_locator.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from BundlerSupport

#bundler_specs

Constructor Details

#initialize(name) ⇒ LibraryLocator

Returns a new instance of LibraryLocator.



14
15
16
# File 'lib/reditor/library_locator.rb', line 14

def initialize(name)
  @name  = name
end

Instance Attribute Details

#nameObject (readonly)

Returns the value of attribute name.



12
13
14
# File 'lib/reditor/library_locator.rb', line 12

def name
  @name
end

Class Method Details

.detect(name) ⇒ Object



8
9
10
# File 'lib/reditor/library_locator.rb', line 8

def self.detect(name)
  new(name.to_s).detect
end

Instance Method Details

#detectObject



18
19
20
# File 'lib/reditor/library_locator.rb', line 18

def detect
  detect_from_bundler || detect_from_loadpath || detect_from_gem
end

#detect_from_bundlerObject



22
23
24
25
26
# File 'lib/reditor/library_locator.rb', line 22

def detect_from_bundler
  return nil unless spec = bundler_specs.find {|spec| spec.name == name }

  Pathname(spec.full_gem_path)
end

#detect_from_gemObject



38
39
40
41
42
43
44
# File 'lib/reditor/library_locator.rb', line 38

def detect_from_gem
  spec = Gem::Specification.find_by_name(name)

  Pathname(spec.full_gem_path)
rescue Gem::LoadError
  # NOP (Gem couldn't find #{name} gem)
end

#detect_from_loadpathObject



28
29
30
31
32
33
34
35
36
# File 'lib/reditor/library_locator.rb', line 28

def detect_from_loadpath
  basename = "#{name}.rb"

  $LOAD_PATH.map {|path|
    full_path = File.expand_path(path + '/' + basename)

    Pathname(full_path)
  }.detect(&:exist?)
end