Class: LibGems::Dependency

Inherits:
Object
  • Object
show all
Defined in:
lib/libgems/dependency.rb

Overview

The Dependency class holds a LibGems name and a LibGems::Requirement.

Constant Summary collapse

TYPES =

Valid dependency types. – When this list is updated, be sure to change LibGems::Specification::CURRENT_SPECIFICATION_VERSION as well.

[
 :development,
 :runtime,
]

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, *requirements) ⇒ Dependency

Constructs a dependency with name and requirements. The last argument can optionally be the dependency type, which defaults to :runtime.



51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/libgems/dependency.rb', line 51

def initialize name, *requirements
  type         = Symbol === requirements.last ? requirements.pop : :runtime
  requirements = requirements.first if 1 == requirements.length # unpack

  unless TYPES.include? type
    raise ArgumentError, "Valid types are #{TYPES.inspect}, "
      + "not #{type.inspect}"
  end

  @name        = name
  @requirement = LibGems::Requirement.create requirements
  @type        = type
  @prerelease  = false

  # This is for Marshal backwards compatability. See the comments in
  # +requirement+ for the dirty details.

  @version_requirements = @requirement
end

Instance Attribute Details

#nameObject

Dependency name or regular expression.



34
35
36
# File 'lib/libgems/dependency.rb', line 34

def name
  @name
end

#prerelease=(value) ⇒ Object (writeonly)

Allows you to force this dependency to be a prerelease.



39
40
41
# File 'lib/libgems/dependency.rb', line 39

def prerelease=(value)
  @prerelease = value
end

#typeObject (readonly)

Dependency type.



44
45
46
# File 'lib/libgems/dependency.rb', line 44

def type
  @type
end

Class Method Details

.warned_version_requirementObject



11
12
13
# File 'lib/libgems/dependency.rb', line 11

def self.warned_version_requirement
  @warned_version_requirement
end

.warned_version_requirement=(value) ⇒ Object



15
16
17
# File 'lib/libgems/dependency.rb', line 15

def self.warned_version_requirement= value
  @warned_version_requirement = value
end

Instance Method Details

#<=>(other) ⇒ Object

Dependencies are ordered by name.



188
189
190
# File 'lib/libgems/dependency.rb', line 188

def <=> other
  @name <=> other.name
end

#==(other) ⇒ Object

:nodoc:



178
179
180
181
182
183
# File 'lib/libgems/dependency.rb', line 178

def == other # :nodoc:
  LibGems::Dependency === other &&
    self.name        == other.name &&
    self.type        == other.type &&
    self.requirement == other.requirement
end

#=~(other) ⇒ Object

Uses this dependency as a pattern to compare to other. This dependency will match if the name matches the other’s name, and other has only an equal version requirement that satisfies this dependency.



198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
# File 'lib/libgems/dependency.rb', line 198

def =~ other
  unless LibGems::Dependency === other
    return unless other.respond_to?(:name) && other.respond_to?(:version)
    other = LibGems::Dependency.new other.name, other.version
  end

  return false unless name === other.name

  reqs = other.requirement.requirements

  return false unless reqs.length == 1
  return false unless reqs.first.first == '='

  version = reqs.first.last

  requirement.satisfied_by? version
end

#hashObject

A dependency’s hash is the XOR of the hashes of name, type, and requirement.



75
76
77
# File 'lib/libgems/dependency.rb', line 75

def hash # :nodoc:
  name.hash ^ type.hash ^ requirement.hash
end

#inspectObject

:nodoc:



79
80
81
82
# File 'lib/libgems/dependency.rb', line 79

def inspect # :nodoc:
  "<%s type=%p name=%p requirements=%p>" %
    [self.class, @type, @name, requirement.to_s]
end

#match?(spec_name, spec_version) ⇒ Boolean

Returns:

  • (Boolean)


216
217
218
219
220
221
# File 'lib/libgems/dependency.rb', line 216

def match?(spec_name, spec_version)
  return false unless name === spec_name
  return true if requirement.none?

  requirement.satisfied_by? LibGems::Version.new(spec_version)
end

#matches_spec?(spec) ⇒ Boolean

Returns:

  • (Boolean)


223
224
225
226
227
228
# File 'lib/libgems/dependency.rb', line 223

def matches_spec?(spec)
  return false unless name === spec.name # name can be a Regexp, so use ===
  return true  if requirement.none?

  requirement.satisfied_by?(spec.version)
end

#prerelease?Boolean

Does this dependency require a prerelease?

Returns:

  • (Boolean)


87
88
89
# File 'lib/libgems/dependency.rb', line 87

def prerelease?
  @prerelease || requirement.prerelease?
end

#pretty_print(q) ⇒ Object

:nodoc:



91
92
93
94
95
96
97
98
99
100
101
102
103
104
# File 'lib/libgems/dependency.rb', line 91

def pretty_print(q) # :nodoc:
  q.group 1, 'LibGems::Dependency.new(', ')' do
    q.pp name
    q.text ','
    q.breakable

    q.pp requirement

    q.text ','
    q.breakable

    q.pp type
  end
end

#requirementObject Also known as: __requirement

What does this dependency require?



109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
# File 'lib/libgems/dependency.rb', line 109

def requirement
  return @requirement if defined?(@requirement) and @requirement

  # @version_requirements and @version_requirement are legacy ivar
  # names, and supported here because older gems need to keep
  # working and Dependency doesn't implement marshal_dump and
  # marshal_load. In a happier world, this would be an
  # attr_accessor. The horrifying instance_variable_get you see
  # below is also the legacy of some old restructurings.
  #
  # Note also that because of backwards compatibility (loading new
  # gems in an old SlimGems installation), we can't add explicit
  # marshaling to this class until we want to make a big
  # break. Maybe 2.0.
  #
  # Children, define explicit marshal and unmarshal behavior for
  # public classes. Marshal formats are part of your public API.

  if defined?(@version_requirement) && @version_requirement
    version = @version_requirement.instance_variable_get :@version
    @version_requirement  = nil
    @version_requirements = LibGems::Requirement.new version
  end

  @requirement = @version_requirements if defined?(@version_requirements)
end

#requirements_listObject

:nodoc:



142
143
144
# File 'lib/libgems/dependency.rb', line 142

def requirements_list
  requirement.as_list
end

#to_sObject

:nodoc:



146
147
148
# File 'lib/libgems/dependency.rb', line 146

def to_s # :nodoc:
  "#{name} (#{requirement}, #{type})"
end

#to_yaml_typeObject



150
151
152
# File 'lib/libgems/dependency.rb', line 150

def to_yaml_type
  "!ruby/object:Gem::Dependency"
end

#version_requirementsObject Also known as: version_requirement

:nodoc:



154
155
156
157
158
159
160
161
162
163
164
165
# File 'lib/libgems/dependency.rb', line 154

def version_requirements # :nodoc:
  unless LibGems::Dependency.warned_version_requirement then
    warn "#{LibGems.location_of_caller.join ':'}:Warning: " \
         "LibGems::Dependency#version_requirements is deprecated " \
         "and will be removed on or after August 2010.  " \
         "Use #requirement"

    LibGems::Dependency.warned_version_requirement = true
  end

  __requirement
end

#version_requirements=(requirements) ⇒ Object

:nodoc:



169
170
171
172
173
174
175
176
# File 'lib/libgems/dependency.rb', line 169

def version_requirements= requirements # :nodoc:
  warn "#{LibGems.location_of_caller.join ':'}:Warning: " \
       "LibGems::Dependency#version_requirements= is deprecated " \
       "and will be removed on or after August 2010.  " \
       "Use LibGems::Dependency.new."

  @requirement = LibGems::Requirement.create requirements
end