class Puppet::Pops::Types::PSemVerType

A Puppet Language Type that exposes the {{SemanticPuppet::Version}} and {{SemanticPuppet::VersionRange}}. The version type is parameterized with version ranges.

@api public

Constants

DEFAULT

Attributes

ranges[R]

Public Class Methods

convert(version) click to toggle source

Creates a SemVer version from the given version argument. If the argument is `nil` or a {SemanticPuppet::Version}, it is returned. If it is a {String}, it will be parsed into a {SemanticPuppet::Version}. Any other class will raise an {ArgumentError}.

@param version [SemanticPuppet::Version,String,nil] the version to convert @return [SemanticPuppet::Version] the converted version @raise [ArgumentError] when the argument cannot be converted into a version

   # File lib/puppet/pops/types/p_sem_ver_type.rb
46 def self.convert(version)
47   case version
48   when nil, SemanticPuppet::Version
49     version
50   when String
51     SemanticPuppet::Version.parse(version)
52   else
53     raise ArgumentError, "Unable to convert a #{version.class.name} to a SemVer"
54   end
55 end
new(ranges) click to toggle source
   # File lib/puppet/pops/types/p_sem_ver_type.rb
20 def initialize(ranges)
21   ranges = ranges.map { |range| range.is_a?(SemanticPuppet::VersionRange) ? range : SemanticPuppet::VersionRange.parse(range) }
22   ranges = merge_ranges(ranges) if ranges.size > 1
23   @ranges = ranges
24 end
new_function(type) click to toggle source

@api private

    # File lib/puppet/pops/types/p_sem_ver_type.rb
 58 def self.new_function(type)
 59   @new_function ||= Puppet::Functions.create_loaded_function(:new_Version, type.loader) do
 60     local_types do
 61       type 'PositiveInteger = Integer[0,default]'
 62       type 'SemVerQualifier = Pattern[/\A(?<part>[0-9A-Za-z-]+)(?:\.\g<part>)*\Z/]'
 63       type "SemVerPattern = Pattern[/\\A#{SemanticPuppet::Version::REGEX_FULL}\\Z/]"
 64       type 'SemVerHash = Struct[{major=>PositiveInteger,minor=>PositiveInteger,patch=>PositiveInteger,Optional[prerelease]=>SemVerQualifier,Optional[build]=>SemVerQualifier}]'
 65     end
 66 
 67     # Creates a SemVer from a string as specified by http://semver.org/
 68     #
 69     dispatch :from_string do
 70       param 'SemVerPattern', :str
 71     end
 72 
 73     # Creates a SemVer from integers, prerelease, and build arguments
 74     #
 75     dispatch :from_args do
 76       param 'PositiveInteger', :major
 77       param 'PositiveInteger', :minor
 78       param 'PositiveInteger', :patch
 79       optional_param 'SemVerQualifier', :prerelease
 80       optional_param 'SemVerQualifier', :build
 81     end
 82 
 83     # Same as #from_args but each argument is instead given in a Hash
 84     #
 85     dispatch :from_hash do
 86       param 'SemVerHash', :hash_args
 87     end
 88 
 89     argument_mismatch :on_error do
 90       param 'String', :str
 91     end
 92 
 93     def from_string(str)
 94       SemanticPuppet::Version.parse(str)
 95     end
 96 
 97     def from_args(major, minor, patch, prerelease = nil, build = nil)
 98       SemanticPuppet::Version.new(major, minor, patch, to_array(prerelease), to_array(build))
 99     end
100 
101     def from_hash(hash)
102       SemanticPuppet::Version.new(hash['major'], hash['minor'], hash['patch'], to_array(hash['prerelease']), to_array(hash['build']))
103     end
104 
105     def on_error(str)
106       _("The string '%{str}' cannot be converted to a SemVer") % { str: str }
107     end
108 
109     private
110 
111     def to_array(component)
112       component ? [component] : nil
113     end
114   end
115 end
register_ptype(loader, ir) click to toggle source
   # File lib/puppet/pops/types/p_sem_ver_type.rb
 9 def self.register_ptype(loader, ir)
10   create_ptype(loader, ir, 'ScalarType',
11      'ranges' => {
12        KEY_TYPE => PArrayType.new(PVariantType.new([PSemVerRangeType::DEFAULT,PStringType::NON_EMPTY])),
13        KEY_VALUE => []
14      }
15   )
16 end

Public Instance Methods

eql?(o) click to toggle source
   # File lib/puppet/pops/types/p_sem_ver_type.rb
30 def eql?(o)
31   self.class == o.class && @ranges == o.ranges
32 end
from_args(major, minor, patch, prerelease = nil, build = nil) click to toggle source
   # File lib/puppet/pops/types/p_sem_ver_type.rb
97 def from_args(major, minor, patch, prerelease = nil, build = nil)
98   SemanticPuppet::Version.new(major, minor, patch, to_array(prerelease), to_array(build))
99 end
from_hash(hash) click to toggle source
    # File lib/puppet/pops/types/p_sem_ver_type.rb
101 def from_hash(hash)
102   SemanticPuppet::Version.new(hash['major'], hash['minor'], hash['patch'], to_array(hash['prerelease']), to_array(hash['build']))
103 end
from_string(str) click to toggle source
   # File lib/puppet/pops/types/p_sem_ver_type.rb
93 def from_string(str)
94   SemanticPuppet::Version.parse(str)
95 end
hash?() click to toggle source
Calls superclass method
   # File lib/puppet/pops/types/p_sem_ver_type.rb
34 def hash?
35   super ^ @ranges.hash
36 end
instance?(o, guard = nil) click to toggle source
   # File lib/puppet/pops/types/p_sem_ver_type.rb
26 def instance?(o, guard = nil)
27   o.is_a?(SemanticPuppet::Version) && (@ranges.empty? || @ranges.any? {|range| range.include?(o) })
28 end
on_error(str) click to toggle source
    # File lib/puppet/pops/types/p_sem_ver_type.rb
105 def on_error(str)
106   _("The string '%{str}' cannot be converted to a SemVer") % { str: str }
107 end
to_array(component) click to toggle source
    # File lib/puppet/pops/types/p_sem_ver_type.rb
111 def to_array(component)
112   component ? [component] : nil
113 end

Protected Instance Methods

_assignable?(o, guard) click to toggle source
    # File lib/puppet/pops/types/p_sem_ver_type.rb
121 def _assignable?(o, guard)
122   return false unless o.class == self.class
123   return true if @ranges.empty?
124   return false if o.ranges.empty?
125 
126   # All ranges in o must be covered by at least one range in self
127   o.ranges.all? do |o_range|
128     @ranges.any? do |range|
129       PSemVerRangeType.covered_by?(o_range, range)
130     end
131   end
132 end
merge_ranges(ranges) click to toggle source

@api private

    # File lib/puppet/pops/types/p_sem_ver_type.rb
135 def merge_ranges(ranges)
136   result = []
137   until ranges.empty?
138     unmerged = []
139     x = ranges.pop
140     result << ranges.inject(x) do |memo, y|
141       merged = PSemVerRangeType.merge(memo, y)
142       if merged.nil?
143         unmerged << y
144       else
145         memo = merged
146       end
147       memo
148     end
149     ranges = unmerged
150   end
151   result.reverse!
152 end