Class: Buildr::ArtifactNamespace::ArtifactRequirement

Inherits:
Object
  • Object
show all
Includes:
DClone
Defined in:
lib/buildr/packaging/artifact_namespace.rb

Overview

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.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from DClone

#dclone

Constructor Details

#initialize(spec) ⇒ ArtifactRequirement

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.



394
395
396
397
398
399
400
401
402
403
404
405
# File 'lib/buildr/packaging/artifact_namespace.rb', line 394

def initialize(spec)
  self.class.send :include, ActsAsArtifact unless ActsAsArtifact === self
  if ArtifactRequirement === spec
    copy_attrs(spec)
  else
    spec = requirement_hash(spec)
    apply_spec(spec[:spec])
    self.name = spec[:name]
    @requirement = spec[:requirement]
    @version = @requirement.default if VersionRequirement.requirement?(@version)
  end
end

Instance Attribute Details

#nameObject

Returns the value of attribute name.



368
369
370
# File 'lib/buildr/packaging/artifact_namespace.rb', line 368

def name
  @name
end

#requirementObject

Returns the value of attribute requirement.



368
369
370
# File 'lib/buildr/packaging/artifact_namespace.rb', line 368

def requirement
  @requirement
end

#versionObject

Returns the value of attribute version.



367
368
369
# File 'lib/buildr/packaging/artifact_namespace.rb', line 367

def version
  @version
end

Class Method Details

.unversioned_spec(spec) ⇒ Object

Return an artifact spec without the version part.



508
509
510
511
512
513
514
515
516
517
518
# File 'lib/buildr/packaging/artifact_namespace.rb', line 508

def unversioned_spec(spec)
  str = spec.to_s
  return nil if str =~ /^:+/
  ary = str.split(':')
  ary = ary[0...-1] if ary.size > 3
  if ary.size > 2
    ary.join(':')
  else
    new(spec).unversioned_spec
  end
end

Instance Method Details

#add_listener(&callback) ⇒ Object



476
477
478
# File 'lib/buildr/packaging/artifact_namespace.rb', line 476

def add_listener(&callback)
  (@listeners ||= []) << callback
end

#artifactObject

Return the Artifact object for the currently selected version



481
482
483
# File 'lib/buildr/packaging/artifact_namespace.rb', line 481

def artifact
  ::Buildr.artifact(self)
end

#copy_attrs(other) ⇒ Object

Copy attributes from other to this object



408
409
410
411
412
413
414
# File 'lib/buildr/packaging/artifact_namespace.rb', line 408

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

#requirement_hash(spec = self) ⇒ Object

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



427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
# File 'lib/buildr/packaging/artifact_namespace.rb', line 427

def requirement_hash(spec = self)
  result = {}
  if String === spec
    parts = spec.split(/\s*->\s*/, 3).map(&:strip)
    case parts.size
    when 1
      result[:spec] = Artifact.to_hash(parts.first)
    when 2
      if /^\w+$/ === parts.first
        result[:name] = parts.first
        result[:spec] = Artifact.to_hash(parts.last)
      else
        result[:spec] = Artifact.to_hash(parts.first)
        result[:requirement] = VersionRequirement.create(parts.last)
      end
    when 3
      result[:name] = parts.first
      result[:spec] = Artifact.to_hash(parts[1])
      result[:requirement] = VersionRequirement.create(parts.last)
    end
  else
    result[:spec] = Artifact.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) ⇒ Boolean

Test if this requirement is satisfied by an artifact spec.

Returns:

  • (Boolean)


456
457
458
459
460
461
462
463
# File 'lib/buildr/packaging/artifact_namespace.rb', line 456

def satisfied_by?(spec)
  return false unless requirement
  spec = Artifact.to_hash(spec)
  hash = to_spec_hash
  hash.delete(:version)
  version = spec.delete(:version)
  hash == spec && requirement.satisfied_by?(version)
end

#selected!Object

:nodoc:



470
471
472
473
474
# File 'lib/buildr/packaging/artifact_namespace.rb', line 470

def selected! #:nodoc:
  @selected = true
  @listeners.each { |l| l.call(self) } if @listeners
  self
end

#selected?Boolean

Has user selected a version for this requirement?

Returns:

  • (Boolean)


466
467
468
# File 'lib/buildr/packaging/artifact_namespace.rb', line 466

def selected?
  @selected
end

#to_requirement_specObject

Format this requirement as an ‘artifact requirement spec`



486
487
488
489
490
491
# File 'lib/buildr/packaging/artifact_namespace.rb', line 486

def to_requirement_spec
  result = to_spec
  result = "#{name} -> #{result}" if name
  result = "#{result} -> #{requirement}" if requirement
  result
end

#to_sObject

:nodoc:



493
494
495
# File 'lib/buildr/packaging/artifact_namespace.rb', line 493

def to_s #:nodoc:
  id ? to_requirement_spec : version
end

#unversioned_specObject

Return an artifact spec without the version part.



498
499
500
501
502
503
504
# File 'lib/buildr/packaging/artifact_namespace.rb', line 498

def unversioned_spec
  str = to_spec
  return nil if str =~ /^:+/
  ary = str.split(':')
  ary = ary[0...-1] if ary.size > 3
  ary.join(':')
end