Method: Puppet::Pops::Types::PSemVerType.new_function

Defined in:
lib/puppet/pops/types/p_sem_ver_type.rb

.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.



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