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.



59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/hairballs/library_helpers.rb', line 59

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)


49
50
51
52
53
# File 'lib/hairballs/library_helpers.rb', line 49

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
37
38
39
40
41
42
43
# File 'lib/hairballs/library_helpers.rb', line 19

def require_libraries
  return if @libraries.nil?

  @libraries.each do |lib|
    retry_count = 0

    begin
      next if retry_count == 2
      vputs "Requiring library: #{lib}"
      require lib
    rescue LoadError
      puts "#{lib} not installed; installing now..."
      Gem.install lib

      if Hairballs.rails?
        installed_gem = find_latest_gem(lib)
        $LOAD_PATH.unshift("#{installed_gem}/lib")
      end

      require lib
      retry_count += 1
      retry
    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.



75
76
77
78
79
80
81
82
83
84
85
86
# File 'lib/hairballs/library_helpers.rb', line 75

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