Module: LocalGem::Singleton

Extended by:
Singleton
Included in:
Singleton
Defined in:
lib/local_gem.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#configObject

Holds the mapping of local gems and their paths to load.



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

def config
  @config
end

#config_fileObject

Returns the value of attribute config_file.



22
23
24
# File 'lib/local_gem.rb', line 22

def config_file
  @config_file
end

Instance Method Details

#load_local_gem(library) ⇒ Object

Adds a given library’s path (specified in config) to the beginning of $LOAD_PATH.



54
55
56
57
58
59
60
61
62
63
64
# File 'lib/local_gem.rb', line 54

def load_local_gem(library)
  if path = config[:gems][library]
    path = [path] unless path.is_a?(Array)
    path.map {|e| File.expand_path(e) }.each do |f|
      $:.unshift(f) unless $:.include?(f)
    end
    true
  else
    false
  end
end

#local_gem(*args) ⇒ Object

Loads the local gem if found or defaults to a normal gem call.



43
44
45
# File 'lib/local_gem.rb', line 43

def local_gem(*args)
  load_local_gem(args[0]) || gem(*args)
end

#local_require(lib) ⇒ Object

Loads the local gem if found and then does a normal require on it.



48
49
50
51
# File 'lib/local_gem.rb', line 48

def local_require(lib)
  load_local_gem(lib)
  require(lib)
end

#read_configObject

Reads config from @config_file and returns a hash. ~/.local_gem.yml.



32
33
34
35
# File 'lib/local_gem.rb', line 32

def read_config
  @config_file ||= ['local_gem.yml', File.join("~", ".local_gem.yml")].detect {|e| File.exists?(File.expand_path(e)) }
  @config_file ? YAML::load(File.new(File.expand_path(@config_file))) : {:gems=>{}}
end

#setup_config(config = nil, &block) ⇒ Object

Takes either a hash or a block and initializes config().



38
39
40
# File 'lib/local_gem.rb', line 38

def setup_config(config=nil, &block)
  @config = config || ConfigStruct.block_to_hash(block)
end