class Buildr::ArtifactNamespace::ArtifactRequirement

An artifact requirement is an object that ActsAsArtifact and has an associated VersionRequirement. It also knows the name (some times equal to the artifact id) that is used to store it in an ArtifactNamespace.

Attributes

name[R]
requirement[R]
version[RW]

Public Class Methods

new(spec) click to toggle source

Create a requirement from an `artifact requirement spec`. This spec has three parts, separated by ->

some_name ->  ar:ti:fact:3.2.5 ->  ( >2 & <4)

As you can see it's just an artifact spec, prefixed with

some_name ->

the :some_name symbol becomes this object's name and is used to store it on an ArtifactNamespace.

ar:ti:fact:3.2.5

The second part is an artifact spec by itself, and specifies all remaining attributes, the version of this spec becomes the default version of this requirement.

The last part consist of a VersionRequirement.

->  ( >2 & <4)

VersionRequirement supports RubyGem's comparision operators in adition to parens, logical and, logical or and negation. See the docs for VersionRequirement for more info on operators.

# File lib/buildr/packaging/artifact_namespace.rb, line 419
def initialize(spec)
  self.class.send :include, ActsAsArtifact unless ActsAsArtifact === self
  if ArtifactRequirement === spec
    copy_attrs(spec)
  else
    spec = requirement_hash(spec)
    apply_spec_no_validation(spec[:spec])
    self.name = spec[:name]
    @requirement = spec[:requirement]
    @version = @requirement.default if VersionRequirement.requirement?(@version)
  end
end
unversioned_spec(spec) click to toggle source
# File lib/buildr/packaging/artifact_namespace.rb, line 540
def unversioned_spec(spec)
  hash = ArtifactNamespace.to_hash(spec)
  return nil if hash.values.compact.length <= 1
  if hash[:classifier]
    "#{hash[:group]}:#{hash[:id]}:#{hash[:type]}:#{hash[:classifier]}:"
  else
    "#{hash[:group]}:#{hash[:id]}:#{hash[:type]}"
  end
end

Public Instance Methods

add_listener(&callback) click to toggle source
# File lib/buildr/packaging/artifact_namespace.rb, line 507
def add_listener(&callback)
  (@listeners ||= []) << callback
end
apply_spec_no_validation(spec) click to toggle source
# File lib/buildr/packaging/artifact_namespace.rb, line 432
def apply_spec_no_validation(spec)
  spec = ArtifactNamespace.to_hash(spec)
  ActsAsArtifact::ARTIFACT_ATTRIBUTES.each { |key| instance_variable_set("@#{key}", spec[key]) }
  self
end
artifact() click to toggle source

Return the Artifact object for the currently selected version

# File lib/buildr/packaging/artifact_namespace.rb, line 512
def artifact
  ::Buildr.artifact(self)
end
copy_attrs(other) click to toggle source

Copy attributes from other to this object

# File lib/buildr/packaging/artifact_namespace.rb, line 439
def copy_attrs(other)
  (ActsAsArtifact::ARTIFACT_ATTRIBUTES + [:name, :requirement]).each do |attr|
    value = other.instance_variable_get("@#{attr}")
    value = value.dup if value && !value.kind_of?(Numeric) && !value.kind_of?(Symbol)
    instance_variable_set("@#{attr}", value)
  end
end
name=(name) click to toggle source
# File lib/buildr/packaging/artifact_namespace.rb, line 447
def name=(name)
  @name = name.to_s
end
requirement=(version_requirement) click to toggle source

Set a the requirement, this must be an string formatted for VersionRequirement#create to parse.

# File lib/buildr/packaging/artifact_namespace.rb, line 453
def requirement=(version_requirement)
  @requirement = VersionRequirement.create(version_requirement.to_s)
end
requirement_hash(spec = self) click to toggle source

Return a hash consisting of :name, :spec, :requirement

# File lib/buildr/packaging/artifact_namespace.rb, line 458
def requirement_hash(spec = self)
  result = {}
  if String === spec
    parts = spec.split(/\s*->\s*/, 3).map(&:strip)
    case parts.size
    when 1
      result[:spec] = ArtifactNamespace.to_hash(parts.first)
    when 2
      if /^\w+$/ === parts.first
        result[:name] = parts.first
        result[:spec] = ArtifactNamespace.to_hash(parts.last)
      else
        result[:spec] = ArtifactNamespace.to_hash(parts.first)
        result[:requirement] = VersionRequirement.create(parts.last)
      end
    when 3
      result[:name] = parts.first
      result[:spec] = ArtifactNamespace.to_hash(parts[1])
      result[:requirement] = VersionRequirement.create(parts.last)
    end
  else
    result[:spec] = ArtifactNamespace.to_hash(spec)
  end
  result[:name] ||= result[:spec][:id].to_s.to_sym
  result[:requirement] ||= VersionRequirement.create(result[:spec][:version])
  result
end
satisfied_by?(spec) click to toggle source

Test if this requirement is satisfied by an artifact spec.

# File lib/buildr/packaging/artifact_namespace.rb, line 487
def satisfied_by?(spec)
  return false unless requirement
  spec = ArtifactNamespace.to_hash(spec)
  hash = to_spec_hash
  hash.delete(:version)
  version = spec.delete(:version)
  hash == spec && requirement.satisfied_by?(version)
end
selected?() click to toggle source

Has user selected a version for this requirement?

# File lib/buildr/packaging/artifact_namespace.rb, line 497
def selected?
  @selected
end
to_requirement_spec() click to toggle source

Format this requirement as an `artifact requirement spec`

# File lib/buildr/packaging/artifact_namespace.rb, line 517
def to_requirement_spec
  result = to_spec
  result = "#{name} -> #{result}" if name
  result = "#{result} -> #{requirement}" if requirement
  result
end
unversioned_spec() click to toggle source

Return an artifact spec without the version part.

# File lib/buildr/packaging/artifact_namespace.rb, line 529
def unversioned_spec
  hash = to_spec_hash
  return nil if hash.values.compact.length <= 1
  if hash[:classifier]
    "#{hash[:group]}:#{hash[:id]}:#{hash[:type]}:#{hash[:classifier]}:"
  else
    "#{hash[:group]}:#{hash[:id]}:#{hash[:type]}"
  end
end