Class: Reditor::LibraryLocator

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

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name) ⇒ LibraryLocator

Returns a new instance of LibraryLocator.



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

def initialize(name)
  @name  = name
end

Instance Attribute Details

#nameObject (readonly)

Returns the value of attribute name.



10
11
12
# File 'lib/reditor/library_locator.rb', line 10

def name
  @name
end

Class Method Details

.detect(name) ⇒ Object



6
7
8
# File 'lib/reditor/library_locator.rb', line 6

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

Instance Method Details

#detectObject



16
17
18
# File 'lib/reditor/library_locator.rb', line 16

def detect
  detect_from_bundler || detect_from_loadpath || detect_from_gem
end

#detect_from_bundlerObject



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

def detect_from_bundler
  require 'bundler'

  return nil unless spec = Bundler.load.specs.find {|spec| spec.name == name }

  Pathname(spec.full_gem_path)
rescue NameError, Bundler::GemNotFound, Bundler::GemfileNotFound
  # NOP (probably it's not available bundler project)
end

#detect_from_gemObject



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

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



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

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

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

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