Module: Hairballs::LibraryHelpers

Included in:
Plugin, Theme
Defined in:
lib/hairballs/library_helpers.rb

Overview

Helpers specifying and requiring dependencies for Themes and Plugins.

Instance Method Summary collapse

Instance Method Details

#do_bundler_extendingObject

Add all gems in the global gemset to the $LOAD_PATH so they can be used even in places like ‘rails console’.

TODO: Use #find_latest_gem for each of #libraries.



52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/hairballs/library_helpers.rb', line 52

def do_bundler_extending
  if defined?(::Bundler)
    all_global_gem_paths = Dir.glob("#{Gem.dir}/gems/*")

    all_global_gem_paths.each do |p|
      gem_path = "#{p}/lib"
      $LOAD_PATH.unshift(gem_path)
    end
  else
    vputs 'Bundler not defined.  Skipping.'
  end
end

#find_latest_gem(gem_name) ⇒ String

Path to the highest version of the gem with the given gem.

Parameters:

  • gem_name (String)

Returns:

  • (String)

    The path to the latest install of gem_name.



42
43
44
45
46
# File 'lib/hairballs/library_helpers.rb', line 42

def find_latest_gem(gem_name)
  the_gem = Dir.glob("#{Gem.dir}/gems/#{gem_name}-*")

  the_gem.empty? ? nil : the_gem.sort.last
end

#libraries(libs = nil) ⇒ Object

Parameters:

  • libs (Array<String>) (defaults to: nil)


7
8
9
10
11
12
13
14
15
# File 'lib/hairballs/library_helpers.rb', line 7

def libraries(libs=nil)
  return @libraries if @libraries && libs.nil?

  @libraries = if libs
                 libs
               else
                 yield([])
               end
end

#require_librariesObject

Requires #libraries on load. If they’re not installed, install them. If it can’t be installed, move on to the next.



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/hairballs/library_helpers.rb', line 19

def require_libraries
  return if @libraries.nil?

  install_threads = []
  require_threads = []
  require_queue = Queue.new

  @libraries.each do |lib|
    begin
      vputs "Requiring library: #{lib}"
      require lib
    rescue LoadError
      puts "#{lib} not installed; installing now..."
      install_threads << start_install_thread(lib, require_queue)
      require_threads << start_require_thread(require_queue)
    end
  end
end

#undo_bundler_extendingObject

Undo the stuff that was done in #do_bundler_extending.

TODO: Use #find_latest_gem for each of #libraries.



68
69
70
71
72
73
74
75
76
77
78
79
# File 'lib/hairballs/library_helpers.rb', line 68

def undo_bundler_extending
  if defined?(::Bundler)
    all_global_gem_paths = Dir.glob("#{Gem.dir}/gems/*")

    all_global_gem_paths.each do |p|
      gem_path = "#{p}/lib"
      $LOAD_PATH.delete(gem_path)
    end
  else
    vputs 'Bundler not defined.  Skipping.'
  end
end