Class: Buildr::VersionRequirement

Inherits:
Object
  • Object
show all
Defined in:
lib/buildr/java/version_requirement.rb,
lib/buildr/packaging/version_requirement.rb

Overview

See ArtifactNamespace#need

Constant Summary collapse

CMP_PROCS =
Gem::Requirement::OPS.dup
CMP_REGEX =
Gem::Requirement::OP_RE.dup
CMP_CHARS =
CMP_PROCS.keys.join
BOOL_CHARS =
'\|\&\!'
VER_CHARS =
'\w\.\-'

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(op, *requirements) ⇒ VersionRequirement

:nodoc:



93
94
95
# File 'lib/buildr/java/version_requirement.rb', line 93

def initialize(op, *requirements) #:nodoc:
  @op, @requirements = op, requirements
end

Instance Attribute Details

#negativeObject

Returns the value of attribute negative.



159
160
161
# File 'lib/buildr/java/version_requirement.rb', line 159

def negative
  @negative
end

Class Method Details

.create(str) ⇒ Object

:call-seq:

VersionRequirement.create(" >1 <2 !(1.5) ") -> requirement

parse the str requirement



44
45
46
47
48
# File 'lib/buildr/java/version_requirement.rb', line 44

def create(str)
  instance_eval normalize(str)
rescue StandardError => e
  raise "Failed to parse #{str.inspect} due to: #{e}"
end

.requirement?(str) ⇒ Boolean

is str a version requirement?

Returns:

  • (Boolean)


36
37
38
# File 'lib/buildr/java/version_requirement.rb', line 36

def requirement?(str)
  /[#{BOOL_CHARS}#{CMP_CHARS}\(\)]/ === str
end

.version?(str) ⇒ Boolean

is str a version string?

Returns:

  • (Boolean)


31
32
33
# File 'lib/buildr/java/version_requirement.rb', line 31

def version?(str)
  /^\s*[#{VER_CHARS}]+\s*$/ === str
end

Instance Method Details

#&(other) ⇒ Object

Either modify the current requirement (if it’s already an and operation) or create a new requirement



147
148
149
# File 'lib/buildr/java/version_requirement.rb', line 147

def &(other)
  operation(:&, other)
end

#composed?Boolean

Is this object a composed requirement?

VersionRequirement.create('1').composed? -> false
VersionRequirement.create('1 | 2').composed? -> true
VersionRequirement.create('1 & 2').composed? -> true

Returns:

  • (Boolean)


101
102
103
# File 'lib/buildr/java/version_requirement.rb', line 101

def composed?
  requirements.size > 1
end

#defaultObject

Return the last requirement on this object having an = operator.



106
107
108
109
110
111
112
113
114
115
116
117
118
# File 'lib/buildr/java/version_requirement.rb', line 106

def default
  default = nil
  requirements.reverse.find do |r|
    if Array === r
      if !negative && (r.first.nil? || r.first.include?('='))
        default = r.last.to_s
      end
    else
      default = r.default
    end
  end
  default
end

#satisfied_by?(version) ⇒ Boolean

Test if this requirement can be satisfied by version

Returns:

  • (Boolean)


121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
# File 'lib/buildr/java/version_requirement.rb', line 121

def satisfied_by?(version)
  return false unless version
  unless version.kind_of?(Gem::Version)
    raise "Invalid version: #{version.inspect}" unless self.class.version?(version)
    version = Gem::Version.new(0).tap { |v| v.version = version.strip }
  end
  message = op == :| ? :any? : :all?
  result = requirements.send message do |req|
    if Array === req
      cmp, rv = *req
      CMP_PROCS[cmp || '='].call(version, rv)
    else
      req.satisfied_by?(version)
    end
  end
  negative ? !result : result
end

#to_sObject

return the parsed expression



152
153
154
155
156
157
# File 'lib/buildr/java/version_requirement.rb', line 152

def to_s
  str = requirements.map(&:to_s).join(" " + @op.to_s + " ").to_s
  str = "( " + str + " )" if negative || requirements.size > 1
  str = "!" + str if negative
  str
end

#|(other) ⇒ Object

Either modify the current requirement (if it’s already an or operation) or create a new requirement



141
142
143
# File 'lib/buildr/java/version_requirement.rb', line 141

def |(other)
  operation(:|, other)
end