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, options) ⇒ LibraryLocator

Returns a new instance of LibraryLocator.



20
21
22
23
# File 'lib/reditor/library_locator.rb', line 20

def initialize(name, options)
  @name    = name
  @options = {global: false}.merge(options)
end

Instance Attribute Details

#nameObject (readonly)

Returns the value of attribute name.



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

def name
  @name
end

#optionsObject (readonly)

Returns the value of attribute options.



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

def options
  @options
end

Class Method Details

.detect(name, options = {}) ⇒ Object



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

def detect(name, options = {})
  new(name.to_s, options).detect
end

.detect!(name, options = {}) ⇒ Object



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

def detect!(name, options = {})
  detect(name, options) or raise LibraryNotFound, "Can't detect library `#{name}'."
end

Instance Method Details

#detectObject



25
26
27
# File 'lib/reditor/library_locator.rb', line 25

def detect
  detect_from_bundler || detect_from_loadpath || detect_from_gem
end

#detect_from_bundlerObject



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

def detect_from_bundler
  return nil if options[:global]
  return nil unless spec = bundler_specs.find {|spec| spec.name == name }

  Pathname(spec.full_gem_path)
end

#detect_from_gemObject



46
47
48
49
50
51
52
# File 'lib/reditor/library_locator.rb', line 46

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



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

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

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

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