Method: Puppet::Pops::Types::PStringType.new_function

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

.new_function(type) ⇒ Object



1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
# File 'lib/puppet/pops/types/types.rb', line 1588

def self.new_function(type)
  @new_function ||= Puppet::Functions.create_loaded_function(:new_string, type.loader) do
    local_types do
      type "Format = Pattern[/#{StringConverter::Format::FMT_PATTERN_STR}/]"
      type 'ContainerFormat = Struct[{
        Optional[format]         => Format,
        Optional[separator]      => String,
        Optional[separator2]     => String,
        Optional[string_formats] => Hash[Type, Format]
      }]'
      type 'TypeMap = Hash[Type, Variant[Format, ContainerFormat]]'
      type 'Convertible = Any'
      type 'Formats = Variant[Default, String[1], TypeMap]'
    end

    dispatch :from_args do
      param           'Convertible',  :from
      optional_param  'Formats',      :string_formats
    end

    def from_args(from, formats = :default)
      StringConverter.singleton.convert(from, formats)
    end
  end
end