Class: Isolate::Entry

Inherits:
Object
  • Object
show all
Defined in:
lib/isolate/entry.rb

Overview

An isolated Gem, with requirement, environment restrictions, and installation options. Generally intended for internal use.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(sandbox, name, *requirements) ⇒ Entry

Create a new entry. Takes sandbox (currently an instance of Isolate::Sandbox), name (as above), and any number of optional version requirements (generally strings). Options can be passed as a trailing hash. Well-known keys:

:args

Command-line build arguments. Passed to the gem at installation time.

:source

An alternative RubyGems source for this gem.



42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/isolate/entry.rb', line 42

def initialize sandbox, name, *requirements
  @environments = []
  @file         = nil
  @name         = name
  @options      = {}
  @requirement  = Gem::Requirement.default
  @sandbox      = sandbox

  if /\.gem$/ =~ @name && File.file?(@name)
    @file = File.expand_path @name

    @name = File.basename(@file, ".gem").
      gsub(/-#{Gem::Version::VERSION_PATTERN}$/, "")
  end

  update(*requirements)
end

Instance Attribute Details

#environmentsObject (readonly)

Which environments does this entry care about? Generally an Array of Strings. An empty array means “all”, not “none”.



16
17
18
# File 'lib/isolate/entry.rb', line 16

def environments
  @environments
end

#nameObject (readonly)

What’s the name of this entry? Generally the name of a gem.



20
21
22
# File 'lib/isolate/entry.rb', line 20

def name
  @name
end

#optionsObject (readonly)

Extra information or hints for installation. See initialize for well-known keys.



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

def options
  @options
end

#requirementObject (readonly)

What version of this entry is required? Expressed as a Gem::Requirement, which see.



30
31
32
# File 'lib/isolate/entry.rb', line 30

def requirement
  @requirement
end

Instance Method Details

#activateObject

Activate this entry.

Raises:

  • (Gem::LoadError)


62
63
64
65
66
# File 'lib/isolate/entry.rb', line 62

def activate
  spec = self.specification
  raise Gem::LoadError, "Couldn't resolve: #{self}" unless spec
  spec.activate
end

#installObject

Install this entry in the sandbox.



70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
# File 'lib/isolate/entry.rb', line 70

def install
  old = Gem.sources.dup

  begin
    installer =
      Gem::DependencyInstaller.new(:development   => false,
                                   :document      => [],
                                   :generate_rdoc => false,
                                   :generate_ri   => false,
                                   :install_dir   => @sandbox.path)

    Gem::Command.build_args = Array(options[:args]) if options[:args]
    Gem.sources += Array(options[:source])          if options[:source]

    installer.install @file || name, requirement
  ensure
    Gem.sources = old
    Gem::Command.build_args = nil
  end
end

#matches?(environment) ⇒ Boolean

Is this entry interested in environment?

Returns:

  • (Boolean)


93
94
95
# File 'lib/isolate/entry.rb', line 93

def matches? environment
  environments.empty? || environments.include?(environment)
end

#matches_spec?(spec) ⇒ Boolean

Is this entry satisfied by spec (generally a Gem::Specification)?

Returns:

  • (Boolean)


100
101
102
# File 'lib/isolate/entry.rb', line 100

def matches_spec? spec
  name == spec.name and requirement.satisfied_by? spec.version
end

#specificationObject

The Gem::Specification for this entry or nil if it isn’t resolveable.



106
107
108
109
110
# File 'lib/isolate/entry.rb', line 106

def specification
  Gem::Specification.find_by_name(name, requirement)
rescue Gem::LoadError
  nil
end

#to_sObject Also known as: inspect



124
125
126
# File 'lib/isolate/entry.rb', line 124

def to_s
  "Entry[#{name.inspect}, #{requirement.to_s.inspect}]"
end

#update(*reqs) ⇒ Object

Updates this entry’s environments, options, and requirement. Environments and options are merged, requirement is replaced.



116
117
118
119
120
121
122
# File 'lib/isolate/entry.rb', line 116

def update *reqs
  @environments |= @sandbox.environments
  @options.merge! reqs.pop if Hash === reqs.last
  @requirement = Gem::Requirement.new reqs unless reqs.empty?

  self
end