Class: Puppet::Pops::Types::PSemVerType

Inherits:
PScalarType show all
Defined in:
lib/puppet/pops/types/p_sem_ver_type.rb

Overview

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

Constant Summary collapse

DEFAULT =
PSemVerType.new(EMPTY_ARRAY)

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from PScalarType

#roundtrip_with_string?

Methods inherited from PAnyType

#==, #accept, #assignable?, #callable?, #callable_args?, #callable_with?, #check_self_recursion, create, #create, #generalize, #hash, #iterable?, #iterable_type, #kind_of_callable?, #loader, #name, #new_function, #normalize, #really_instance?, #resolve, #roundtrip_with_string?, simple_name, #simple_name, #to_alias_expanded_s, #to_s

Methods inherited from TypedModelObject

_pcore_type, create_ptype, register_ptypes

Methods included from Visitable

#accept

Methods included from PuppetObject

#_pcore_all_contents, #_pcore_contents, #_pcore_init_hash, #_pcore_type

Constructor Details

#initialize(ranges) ⇒ PSemVerType

Returns a new instance of PSemVerType.



20
21
22
23
24
# File 'lib/puppet/pops/types/p_sem_ver_type.rb', line 20

def initialize(ranges)
  ranges = ranges.map { |range| range.is_a?(SemanticPuppet::VersionRange) ? range : SemanticPuppet::VersionRange.parse(range) }
  ranges = merge_ranges(ranges) if ranges.size > 1
  @ranges = ranges
end

Instance Attribute Details

#rangesObject (readonly)



18
19
20
# File 'lib/puppet/pops/types/p_sem_ver_type.rb', line 18

def ranges
  @ranges
end

Class Method Details

.convert(version) ⇒ SemanticPuppet::Version

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.

Parameters:

Returns:

Raises:

  • (ArgumentError)

    when the argument cannot be converted into a version



46
47
48
49
50
51
52
53
54
55
# File 'lib/puppet/pops/types/p_sem_ver_type.rb', line 46

def self.convert(version)
  case version
  when nil, SemanticPuppet::Version
    version
  when String
    SemanticPuppet::Version.parse(version)
  else
    raise ArgumentError, "Unable to convert a #{version.class.name} to a SemVer"
  end
end

.new_function(type) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
# File 'lib/puppet/pops/types/p_sem_ver_type.rb', line 58

def self.new_function(type)
  @new_function ||= Puppet::Functions.create_loaded_function(:new_Version, type.loader) do
    local_types do
      type 'PositiveInteger = Integer[0,default]'
      type 'SemVerQualifier = Pattern[/\A(?<part>[0-9A-Za-z-]+)(?:\.\g<part>)*\Z/]'
      type 'SemVerString = String[1]'
      type 'SemVerHash = Struct[{major=>PositiveInteger,minor=>PositiveInteger,patch=>PositiveInteger,Optional[prerelease]=>SemVerQualifier,Optional[build]=>SemVerQualifier}]'
    end

    # Creates a SemVer from a string as specified by http://semver.org/
    #
    dispatch :from_string do
      param 'SemVerString', :str
    end

    # Creates a SemVer from integers, prerelease, and build arguments
    #
    dispatch :from_args do
      param 'PositiveInteger', :major
      param 'PositiveInteger', :minor
      param 'PositiveInteger', :patch
      optional_param 'SemVerQualifier', :prerelease
      optional_param 'SemVerQualifier', :build
    end

    # Same as #from_args but each argument is instead given in a Hash
    #
    dispatch :from_hash do
      param 'SemVerHash', :hash_args
    end

    def from_string(str)
      SemanticPuppet::Version.parse(str)
    end

    def from_args(major, minor, patch, prerelease = nil, build = nil)
      SemanticPuppet::Version.new(major, minor, patch, prerelease, build)
    end

    def from_hash(hash)
      SemanticPuppet::Version.new(hash['major'], hash['minor'], hash['patch'], hash['prerelease'], hash['build'])
    end
  end
end

.register_ptype(loader, ir) ⇒ Object



9
10
11
12
13
14
15
16
# File 'lib/puppet/pops/types/p_sem_ver_type.rb', line 9

def self.register_ptype(loader, ir)
  create_ptype(loader, ir, 'ScalarType',
     'ranges' => {
       KEY_TYPE => PArrayType.new(PVariantType.new([PSemVerRangeType::DEFAULT,PStringType::NON_EMPTY])),
       KEY_VALUE => []
     }
  )
end

Instance Method Details

#eql?(o) ⇒ Boolean

Returns:

  • (Boolean)


30
31
32
# File 'lib/puppet/pops/types/p_sem_ver_type.rb', line 30

def eql?(o)
  self.class == o.class && @ranges == o.ranges
end

#hash?Boolean

Returns:

  • (Boolean)


34
35
36
# File 'lib/puppet/pops/types/p_sem_ver_type.rb', line 34

def hash?
  super ^ @ranges.hash
end

#instance?(o, guard = nil) ⇒ Boolean

Returns:

  • (Boolean)


26
27
28
# File 'lib/puppet/pops/types/p_sem_ver_type.rb', line 26

def instance?(o, guard = nil)
  o.is_a?(SemanticPuppet::Version) && (@ranges.empty? || @ranges.any? {|range| range.include?(o) })
end