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

Inherits:
PScalarType 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 PAnyType

#==, #assignable?, #callable?, #callable_args?, #check_self_recursion, #kind_of_callable?, #name, #new_function, #normalize, #really_instance?, #resolve, simple_name, #simple_name, #to_alias_expanded_s, #to_s

Methods inherited from TypedModelObject

_ptype, create_ptype, register_ptypes

Methods included from PuppetObject

#_ptype

Constructor Details

#initialize(size_type, values = EMPTY_ARRAY) ⇒ PStringType

Returns a new instance of PStringType.



1378
1379
1380
1381
# File 'lib/puppet/pops/types/types.rb', line 1378

def initialize(size_type, values = EMPTY_ARRAY)
  @size_type = size_type
  @values = values.sort.freeze
end

Instance Attribute Details

#size_typeObject (readonly)



1376
1377
1378
# File 'lib/puppet/pops/types/types.rb', line 1376

def size_type
  @size_type
end

#valuesObject (readonly)



1376
1377
1378
# File 'lib/puppet/pops/types/types.rb', line 1376

def values
  @values
end

Class Method Details

.new_function(_, loader) ⇒ Object



1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
# File 'lib/puppet/pops/types/types.rb', line 1417

def self.new_function(_, loader)
  @new_function ||= Puppet::Functions.create_loaded_function(:new_string, loader) do
    local_types do
      type 'Format = Pattern[/^%([\s\+\-#0\[\{<\(\|]*)([1-9][0-9]*)?(?:\.([0-9]+))?([a-zA-Z])/]'
      type 'ContainerFormat = Struct[{
        Optional[format]         => String,
        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



1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
# File 'lib/puppet/pops/types/types.rb', line 1363

def self.register_ptype(loader, ir)
  create_ptype(loader, ir, 'ScalarType',
    'size_type' => {
      KEY_TYPE => POptionalType.new(PType.new(PIntegerType::DEFAULT)),
      KEY_VALUE => nil
    },
    'values' => {
      KEY_TYPE => PArrayType.new(PStringType::DEFAULT),
      KEY_VALUE => EMPTY_ARRAY
    }
  )
end

Instance Method Details

#accept(visitor, guard) ⇒ Object



1383
1384
1385
1386
# File 'lib/puppet/pops/types/types.rb', line 1383

def accept(visitor, guard)
  super
  @size_type.accept(visitor, guard) unless @size_type.nil?
end

#eql?(o) ⇒ Boolean

Returns:

  • (Boolean)


1404
1405
1406
# File 'lib/puppet/pops/types/types.rb', line 1404

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

#generalizeObject



1388
1389
1390
# File 'lib/puppet/pops/types/types.rb', line 1388

def generalize
  DEFAULT
end

#hashObject



1392
1393
1394
# File 'lib/puppet/pops/types/types.rb', line 1392

def hash
  @size_type.hash ^ @values.hash
end

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

Returns:

  • (Boolean)


1408
1409
1410
1411
1412
1413
1414
1415
# File 'lib/puppet/pops/types/types.rb', line 1408

def instance?(o, guard = nil)
  # true if size compliant
  if o.is_a?(String) && (@size_type.nil? || @size_type.instance?(o.size, guard))
    @values.empty? || @values.include?(o)
  else
    false
  end
end

#iterable?(guard = nil) ⇒ Boolean

Returns:

  • (Boolean)


1396
1397
1398
# File 'lib/puppet/pops/types/types.rb', line 1396

def iterable?(guard = nil)
  true
end

#iterable_type(guard = nil) ⇒ Object



1400
1401
1402
# File 'lib/puppet/pops/types/types.rb', line 1400

def iterable_type(guard = nil)
  ITERABLE_TYPE
end