Class: S4tUtils::Hidden::Arranger

Inherits:
Object
  • Object
show all
Defined in:
lib/s4t-utils/load-path-auto-adjuster.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(third_party) ⇒ Arranger

Returns a new instance of Arranger.



55
56
57
58
59
# File 'lib/s4t-utils/load-path-auto-adjuster.rb', line 55

def initialize(third_party)
  @third_party_lib = third_party
  @project_lib = third_party.parent.parent
  @test_util_root = @project_lib.parent
end

Class Method Details

.arrange_path_around(third_party) ⇒ Object



61
62
63
# File 'lib/s4t-utils/load-path-auto-adjuster.rb', line 61

def self.arrange_path_around(third_party)
  new(third_party).arrange
end

Instance Method Details

#add_everything_at_frontObject



100
101
102
103
104
# File 'lib/s4t-utils/load-path-auto-adjuster.rb', line 100

def add_everything_at_front
  $:.unshift(@test_util_root.to_s)  
  $:.unshift(@third_party_lib.to_s)
  $:.unshift(@project_lib.to_s)  # This is now first
end

#add_third_party_gemsObject



75
76
77
78
79
80
81
82
83
84
# File 'lib/s4t-utils/load-path-auto-adjuster.rb', line 75

def add_third_party_gems
  # When RubyGems 0.8.11 gets clear_paths, it does not clear the
  # cache used by Gem's overriding version of require(), so if
  # this is loaded after the first Gem is required, it will have
  # no effect on later uses of require(). (But it does affect
  # require_gem.)
  # 
  ENV['GEM_PATH']=(@third_party_lib+'gems').to_s
  Gem.clear_paths
end

#arrangeObject



65
66
67
68
69
70
71
72
# File 'lib/s4t-utils/load-path-auto-adjuster.rb', line 65

def arrange
  add_third_party_gems
  if project_lib_already_in_path?
    just_add_third_party_after_project_lib
  else
    add_everything_at_front
  end
end

#just_add_third_party_after_project_libObject



90
91
92
93
94
95
96
97
98
# File 'lib/s4t-utils/load-path-auto-adjuster.rb', line 90

def just_add_third_party_after_project_lib
  $:.each_with_index do | path_element, index |
    if path_element == @project_lib.to_s
      $:[index+1,0] = @third_party_lib.to_s
      return
    end
  end
  fail "No place to put third_party library."
end

#project_lib_already_in_path?Boolean

Returns:

  • (Boolean)


86
87
88
# File 'lib/s4t-utils/load-path-auto-adjuster.rb', line 86

def project_lib_already_in_path?
  $:.include?(@project_lib.to_s)
end