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.



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

def config
  @config
end

#config_fileObject

Returns the value of attribute config_file.



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

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.



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

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.



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

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.



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

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.



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

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().



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

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