Class: Pod::Specification::Set

Inherits:
Object
  • Object
show all
Defined in:
lib/cocoapods-core/specification/set.rb,
lib/cocoapods-core/specification/set/presenter.rb,
lib/cocoapods-core/specification/set/statistics.rb

Overview

Note:

The alphabetical order of the sets is used to select a specification if multiple are available for a given version.

Note:

The set class is not and should be not aware of the backing store of a Source.

A Specification::Set is responsible of handling all the specifications of a Pod. This class stores the information of the dependencies that required a Pod in the resolution process.

Direct Known Subclasses

External

Defined Under Namespace

Classes: External, Presenter, Statistics

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, sources = []) ⇒ Set

Returns a new instance of Set.

Parameters:

  • name (String)

    the name of the Pod.

  • sources (Array<Source>, Source) (defaults to: [])

    the sources that contain a Pod.



35
36
37
38
39
40
41
# File 'lib/cocoapods-core/specification/set.rb', line 35

def initialize(name, sources = [])
  @name    = name
  sources  = sources.is_a?(Array) ? sources : [sources]
  @sources = sources.sort_by(&:name)
  @dependencies_by_requirer_name = {}
  @dependencies = []
end

Instance Attribute Details

#dependenciesObject

Returns the value of attribute dependencies.



198
199
200
# File 'lib/cocoapods-core/specification/set.rb', line 198

def dependencies
  @dependencies
end

#dependencies_by_requirer_nameObject

———————————————————————–#



197
198
199
# File 'lib/cocoapods-core/specification/set.rb', line 197

def dependencies_by_requirer_name
  @dependencies_by_requirer_name
end

#nameString (readonly)

Returns the name of the Pod.

Returns:

  • (String)

    the name of the Pod.



22
23
24
# File 'lib/cocoapods-core/specification/set.rb', line 22

def name
  @name
end

#sourcesArray<Source> (readonly)

Returns the sources that contain the specifications for the available versions of a Pod.

Returns:

  • (Array<Source>)

    the sources that contain the specifications for the available versions of a Pod.



27
28
29
# File 'lib/cocoapods-core/specification/set.rb', line 27

def sources
  @sources
end

Instance Method Details

#==(other) ⇒ Object



159
160
161
162
163
# File 'lib/cocoapods-core/specification/set.rb', line 159

def ==(other)
  self.class == other.class &&
    @name == other.name &&
    @sources.map(&:name) == other.sources.map(&:name)
end

#acceptable_versionsArray<Version>

Returns All the versions which are acceptable given the requirements.

Returns:

  • (Array<Version>)

    All the versions which are acceptable given the requirements.



125
126
127
# File 'lib/cocoapods-core/specification/set.rb', line 125

def acceptable_versions
  versions.select { |v| dependency.match?(name, v) }
end

#dependencyDependency

Returns A dependency that includes all the versions requirements of the stored dependencies.

Returns:

  • (Dependency)

    A dependency that includes all the versions requirements of the stored dependencies.



79
80
81
82
83
# File 'lib/cocoapods-core/specification/set.rb', line 79

def dependency
  dependencies.reduce(Dependency.new(name)) do |previous, dependency|
    previous.merge(dependency.to_root_dependency)
  end
end

#highest_versionVersion

Returns The highest version known of the specification.

Returns:

  • (Version)

    The highest version known of the specification.



138
139
140
# File 'lib/cocoapods-core/specification/set.rb', line 138

def highest_version
  versions.first
end

#highest_version_spec_pathPathname

Returns The path of the highest version.

Returns:

  • (Pathname)

    The path of the highest version.



144
145
146
# File 'lib/cocoapods-core/specification/set.rb', line 144

def highest_version_spec_path
  specification_path_for_version(highest_version)
end

#required_by(dependency, dependent_name) ⇒ void

TODO:

This should simply return a boolean. Is CocoaPods that should raise.

This method returns an undefined value.

Stores a dependency on the Pod.

Parameters:

  • dependency (Dependency)

    a dependency that requires the Pod.

  • dependent_name (String)

    the name of the owner of the dependency. It is used only to display the Pod::Informative.

Raises:

  • If the versions requirement of the dependency are not compatible with the previously stored dependencies.



60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/cocoapods-core/specification/set.rb', line 60

def required_by(dependency, dependent_name)
  dependencies_by_requirer_name[dependent_name] ||= []
  dependencies_by_requirer_name[dependent_name] << dependency
  dependencies << dependency

  if acceptable_versions.empty?
    message = "Unable to satisfy the following requirements:\n"
    dependencies_by_requirer_name.each do |name, dependencies|
      dependencies.each do |dep|
        message << "- `#{dep}` required by `#{name}`"
      end
    end
    raise Informative, message
  end
end

#required_versionVersion

TODO:

This should simply return nil. CocoaPods should raise instead.

Returns the highest version that satisfies the stored dependencies.

Returns:

  • (Version)

    the highest version that satisfies the stored dependencies.



113
114
115
116
117
118
119
120
# File 'lib/cocoapods-core/specification/set.rb', line 113

def required_version
  version = versions.find { |v| dependency.match?(name, v) }
  unless version
    raise Informative, "Required version (#{dependency}) not found " \
      "for `#{name}`.\nAvailable versions: #{versions.join(', ')}"
  end
  version
end

#specificationSpecification

Note:

If multiple sources have a specification for the #required_version The alphabetical order of their names is used to disambiguate.

Returns the top level specification of the Pod for the #required_version.

Returns:



92
93
94
95
# File 'lib/cocoapods-core/specification/set.rb', line 92

def specification
  path = specification_path_for_version(required_version)
  Specification.from_file(path)
end

#specification_path_for_version(version) ⇒ Object

TODO



99
100
101
102
103
104
105
106
# File 'lib/cocoapods-core/specification/set.rb', line 99

def specification_path_for_version(version)
  sources = []
  versions_by_source.each do |source, source_versions|
    sources << source if source_versions.include?(required_version)
  end
  source = sources.sort_by(&:name).first
  source.specification_path(name, required_version)
end

#to_hashHash

Returns a hash representation of the set composed by dumb data types.

Examples:


"name" => "CocoaLumberjack",
"versions" => { "master" => [ "1.6", "1.3.3"] },
"highest_version" => "1.6",
"highest_version_spec" => 'REPO/CocoaLumberjack/1.6/CocoaLumberjack.podspec'

Returns:

  • (Hash)

    The hash representation.



182
183
184
185
186
187
188
189
190
191
192
193
# File 'lib/cocoapods-core/specification/set.rb', line 182

def to_hash
  versions = versions_by_source.reduce({}) do |memo, (source, version)|
    memo[source.name] = version.map(&:to_s)
    memo
  end
  {
    'name' => name,
    'versions' => versions,
    'highest_version' => highest_version.to_s,
    'highest_version_spec' => highest_version_spec_path.to_s,
  }
end

#to_sObject Also known as: inspect



165
166
167
168
# File 'lib/cocoapods-core/specification/set.rb', line 165

def to_s
  "#<#{self.class.name} for `#{name}' with required version " \
    "`#{required_version}' available at `#{sources.map(&:name) * ', '}'>"
end

#versionsArray<Version>

Returns all the available versions for the Pod, sorted from highest to lowest.

Returns:

  • (Array<Version>)

    all the available versions for the Pod, sorted from highest to lowest.



132
133
134
# File 'lib/cocoapods-core/specification/set.rb', line 132

def versions
  versions_by_source.values.flatten.uniq.sort.reverse
end

#versions_by_sourceHash{Source => Version}

Returns all the available versions for the Pod grouped by source.

Returns:

  • (Hash{Source => Version})

    all the available versions for the Pod grouped by source.



151
152
153
154
155
156
157
# File 'lib/cocoapods-core/specification/set.rb', line 151

def versions_by_source
  result = {}
  sources.each do |source|
    result[source] = source.versions(name)
  end
  result
end