Class: Buildr::ArtifactNamespace::ArtifactRequirement
- 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
-
#name ⇒ Object
Returns the value of attribute name.
-
#requirement ⇒ Object
Returns the value of attribute requirement.
-
#version ⇒ Object
Returns the value of attribute version.
Class Method Summary collapse
Instance Method Summary collapse
- #add_listener(&callback) ⇒ Object
- #apply_spec_no_validation(spec) ⇒ Object
-
#artifact ⇒ Object
Return the Artifact object for the currently selected version.
-
#copy_attrs(other) ⇒ Object
Copy attributes from other to this object.
-
#initialize(spec) ⇒ ArtifactRequirement
constructor
Create a requirement from an ‘artifact requirement spec`.
-
#requirement_hash(spec = self) ⇒ Object
Return a hash consisting of :name, :spec, :requirement.
-
#satisfied_by?(spec) ⇒ Boolean
Test if this requirement is satisfied by an artifact spec.
-
#selected! ⇒ Object
:nodoc:.
-
#selected? ⇒ Boolean
Has user selected a version for this requirement?.
-
#to_requirement_spec ⇒ Object
Format this requirement as an ‘artifact requirement spec`.
-
#to_s ⇒ Object
:nodoc:.
-
#unversioned_spec ⇒ Object
Return an artifact spec without the version part.
Methods included from 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.
419 420 421 422 423 424 425 426 427 428 429 430 |
# 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 |
Instance Attribute Details
#name ⇒ Object
Returns the value of attribute name.
393 394 395 |
# File 'lib/buildr/packaging/artifact_namespace.rb', line 393 def name @name end |
#requirement ⇒ Object
Returns the value of attribute requirement.
393 394 395 |
# File 'lib/buildr/packaging/artifact_namespace.rb', line 393 def requirement @requirement end |
Class Method Details
.unversioned_spec(spec) ⇒ Object
540 541 542 543 544 545 546 547 548 |
# 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 |
Instance Method Details
#add_listener(&callback) ⇒ Object
507 508 509 |
# File 'lib/buildr/packaging/artifact_namespace.rb', line 507 def add_listener(&callback) (@listeners ||= []) << callback end |
#apply_spec_no_validation(spec) ⇒ Object
432 433 434 435 436 |
# 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 ⇒ Object
Return the Artifact object for the currently selected version
512 513 514 |
# File 'lib/buildr/packaging/artifact_namespace.rb', line 512 def artifact ::Buildr.artifact(self) end |
#copy_attrs(other) ⇒ Object
Copy attributes from other to this object
439 440 441 442 443 444 445 |
# 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 |
#requirement_hash(spec = self) ⇒ Object
Return a hash consisting of :name, :spec, :requirement
458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 |
# 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) ⇒ Boolean
Test if this requirement is satisfied by an artifact spec.
487 488 489 490 491 492 493 494 |
# 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! ⇒ Object
:nodoc:
501 502 503 504 505 |
# File 'lib/buildr/packaging/artifact_namespace.rb', line 501 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?
497 498 499 |
# File 'lib/buildr/packaging/artifact_namespace.rb', line 497 def selected? @selected end |
#to_requirement_spec ⇒ Object
Format this requirement as an ‘artifact requirement spec`
517 518 519 520 521 522 |
# 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 |
#to_s ⇒ Object
:nodoc:
524 525 526 |
# File 'lib/buildr/packaging/artifact_namespace.rb', line 524 def to_s #:nodoc: id ? to_requirement_spec : version end |
#unversioned_spec ⇒ Object
Return an artifact spec without the version part.
529 530 531 532 533 534 535 536 537 |
# 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 |