Class: Puppet::Pops::Types::PStringType

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

Constant Summary collapse

DEFAULT =
PStringType.new(nil)
NON_EMPTY =
PStringType.new(PCollectionType::NOT_EMPTY_SIZE)
ITERABLE_TYPE =

Iterates over each character of the string

PIterableType.new(PStringType.new(PIntegerType.new(1, 1)))

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from PScalarType

#roundtrip_with_string?

Methods inherited from PAnyType

#==, #assignable?, #callable?, #callable_args?, #callable_with?, #check_self_recursion, create, #create, #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 PuppetObject

#_pcore_all_contents, #_pcore_contents, #_pcore_init_hash, #_pcore_type, #to_s

Constructor Details

#initialize(size_type_or_value, deprecated_multi_args = EMPTY_ARRAY) ⇒ PStringType

Returns a new instance of PStringType.



1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
# File 'lib/puppet/pops/types/types.rb', line 1514

def initialize(size_type_or_value, deprecated_multi_args = EMPTY_ARRAY)
  unless deprecated_multi_args.empty?
    if Puppet[:strict] != :off
      # TRANSLATORS 'PStringType#initialize' is a class and method name and should not be translated
      Puppet.warn_once('deprecations', "PStringType#initialize_multi_args",
                       _("Passing more than one argument to PStringType#initialize is deprecated"))
    end
    size_type_or_value = deprecated_multi_args[0]
  end
  @size_type_or_value = size_type_or_value.is_a?(PIntegerType) ? size_type_or_value.to_size : size_type_or_value
end

Instance Attribute Details

#size_type_or_valueObject (readonly)



1512
1513
1514
# File 'lib/puppet/pops/types/types.rb', line 1512

def size_type_or_value
  @size_type_or_value
end

Class Method Details

.new_function(type) ⇒ Object



1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
# File 'lib/puppet/pops/types/types.rb', line 1594

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

.register_ptype(loader, ir) ⇒ Object



1504
1505
1506
1507
1508
1509
1510
# File 'lib/puppet/pops/types/types.rb', line 1504

def self.register_ptype(loader, ir)
  create_ptype(loader, ir, 'ScalarDataType',
               'size_type_or_value' => {
                 KEY_TYPE => POptionalType.new(PVariantType.new([PStringType::DEFAULT, PTypeType.new(PIntegerType::DEFAULT)])),
                 KEY_VALUE => nil
               })
end

Instance Method Details

#accept(visitor, guard) ⇒ Object



1526
1527
1528
1529
# File 'lib/puppet/pops/types/types.rb', line 1526

def accept(visitor, guard)
  super
  @size_type_or_value.accept(visitor, guard) if @size_type_or_value.is_a?(PIntegerType)
end

#derived_size_typeObject



1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
# File 'lib/puppet/pops/types/types.rb', line 1582

def derived_size_type
  case @size_type_or_value
  when PIntegerType
    @size_type_or_value
  when String
    sz = @size_type_or_value.size
    PIntegerType.new(sz, sz)
  else
    PCollectionType::DEFAULT_SIZE
  end
end

#eql?(o) ⇒ Boolean

Returns:

  • (Boolean)


1547
1548
1549
# File 'lib/puppet/pops/types/types.rb', line 1547

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

#generalizeObject



1531
1532
1533
# File 'lib/puppet/pops/types/types.rb', line 1531

def generalize
  DEFAULT
end

#hashObject



1535
1536
1537
# File 'lib/puppet/pops/types/types.rb', line 1535

def hash
  @size_type_or_value.hash
end

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

Returns:

  • (Boolean)


1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
# File 'lib/puppet/pops/types/types.rb', line 1551

def instance?(o, guard = nil)
  # true if size compliant
  if o.is_a?(String)
    if @size_type_or_value.is_a?(PIntegerType)
      @size_type_or_value.instance?(o.size, guard)
    else
      @size_type_or_value.nil? ? true : o == value
    end
  else
    false
  end
end

#iterable?(guard = nil) ⇒ Boolean

Returns:

  • (Boolean)


1539
1540
1541
# File 'lib/puppet/pops/types/types.rb', line 1539

def iterable?(guard = nil)
  true
end

#iterable_type(guard = nil) ⇒ Object



1543
1544
1545
# File 'lib/puppet/pops/types/types.rb', line 1543

def iterable_type(guard = nil)
  ITERABLE_TYPE
end

#size_typeObject



1578
1579
1580
# File 'lib/puppet/pops/types/types.rb', line 1578

def size_type
  @size_type_or_value.is_a?(PIntegerType) ? @size_type_or_value : nil
end

#valueObject



1564
1565
1566
# File 'lib/puppet/pops/types/types.rb', line 1564

def value
  @size_type_or_value.is_a?(PIntegerType) ? nil : @size_type_or_value
end

#valuesObject

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.

Deprecated.


1570
1571
1572
1573
1574
1575
1576
# File 'lib/puppet/pops/types/types.rb', line 1570

def values
  if Puppet[:strict] != :off
    # TRANSLATORS 'PStringType#values' and '#value' are classes and method names and should not be translated
    Puppet.warn_once('deprecations', "PStringType#values", _("Method PStringType#values is deprecated. Use #value instead"))
  end
  @value.is_a?(String) ? [@value] : EMPTY_ARRAY
end