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.

API:

  • public

Constant Summary collapse

DEFAULT =

API:

  • public

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, #to_s

Constructor Details

#initialize(ranges) ⇒ PSemVerType

Returns a new instance of PSemVerType.

API:

  • public



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

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)

API:

  • public



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

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.

Raises:

  • when the argument cannot be converted into a version

Parameters:

  • the version to convert

Returns:

  • the converted version

API:

  • public



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

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.

API:

  • private



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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
# File 'lib/puppet/pops/types/p_sem_ver_type.rb', line 59

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 "SemVerPattern = Pattern[/\\A#{SemanticPuppet::Version::REGEX_FULL}\\Z/]"
      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 'SemVerPattern', :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

    argument_mismatch :on_error do
      param 'String', :str
    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, to_array(prerelease), to_array(build))
    end

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

    def on_error(str)
      _("The string '%{str}' cannot be converted to a SemVer") % { str: str }
    end

    private

    def to_array(component)
      component ? [component] : nil
    end
  end
end

.register_ptype(loader, ir) ⇒ Object

API:

  • public



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

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:

API:

  • public



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

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

#hash?Boolean

Returns:

API:

  • public



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

def hash?
  super ^ @ranges.hash
end

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

Returns:

API:

  • public



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

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