Method: Library::Ledger#activate

Defined in:
lib/library/ledger.rb

#activate(name, constraint = nil) ⇒ Library

TODO:

Should we also check $“? Eg. ‘return false if $”.include?(path)`.

Activate a library, retrieving a Library instance by name, or name and version, and ensuring only that instance will be returned for all subsequent requests. Libraries are singleton, so once activated the same object is always returned.

This method will raise a LoadError if the name is not found.

Note that activating all runtime requirements of a library being activated was considered, but decided against. There’s no reason to activatea library until it is actually needed. However this is not so when testing, or verifying available requirements, so other methods are provided such as ‘#activate_requirements`.

Parameters:

  • name (String)

    Name of library.

  • constraint (String) (defaults to: nil)

    Valid version constraint.

Returns:

  • (Library)

    The activated Library object.

Raises:



227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
# File 'lib/library/ledger.rb', line 227

def activate(name, constraint=nil)
  raise LoadError, "no such library -- #{name}" unless key?(name)

  library = self[name]

  if Library === library
    if constraint
      unless library.version.satisfy?(constraint)
        raise Library::VersionConflict, library
      end
    end
  else # library is an array of versions
    if constraint
      verscon = Version::Constraint.parse(constraint)
      library = library.select{ |lib| verscon.compare(lib.version) }.max
    else
      library = library.max
    end
    unless library
      raise VersionError, "no library version -- #{name} #{constraint}"
    end

    self[name] = library #constrain(library)
  end

  library
end