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.



397
398
399
400
401
402
403
404
405
406
407
408
# File 'lib/buildr/packaging/artifact_namespace.rb', line 397

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.



371
372
373
# File 'lib/buildr/packaging/artifact_namespace.rb', line 371

def name
  @name
end

#requirementObject

Returns the value of attribute requirement.



371
372
373
# File 'lib/buildr/packaging/artifact_namespace.rb', line 371

def requirement
  @requirement
end

#versionObject

Returns the value of attribute version.



370
371
372
# File 'lib/buildr/packaging/artifact_namespace.rb', line 370

def version
  @version
end

Class Method Details

.unversioned_spec(spec) ⇒ Object

Return an artifact spec without the version part.



511
512
513
514
515
516
517
518
519
520
521
# File 'lib/buildr/packaging/artifact_namespace.rb', line 511

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



479
480
481
# File 'lib/buildr/packaging/artifact_namespace.rb', line 479

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

#artifactObject

Return the Artifact object for the currently selected version



484
485
486
# File 'lib/buildr/packaging/artifact_namespace.rb', line 484

def artifact
  ::Buildr.artifact(self)
end

#copy_attrs(other) ⇒ Object

Copy attributes from other to this object



411
412
413
414
415
416
417
# File 'lib/buildr/packaging/artifact_namespace.rb', line 411

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



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

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)


459
460
461
462
463
464
465
466
# File 'lib/buildr/packaging/artifact_namespace.rb', line 459

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:



473
474
475
476
477
# File 'lib/buildr/packaging/artifact_namespace.rb', line 473

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)


469
470
471
# File 'lib/buildr/packaging/artifact_namespace.rb', line 469

def selected?
  @selected
end

#to_requirement_specObject

Format this requirement as an ‘artifact requirement spec`



489
490
491
492
493
494
# File 'lib/buildr/packaging/artifact_namespace.rb', line 489

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

#to_sObject

:nodoc:



496
497
498
# File 'lib/buildr/packaging/artifact_namespace.rb', line 496

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

#unversioned_specObject

Return an artifact spec without the version part.



501
502
503
504
505
506
507
# File 'lib/buildr/packaging/artifact_namespace.rb', line 501

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