Class: Puppet::Pops::Types::PArrayType

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

Constant Summary collapse

DEFAULT =
PArrayType.new(nil)
EMPTY =
PArrayType.new(PUnitType::DEFAULT, ZERO_SIZE)

Constants inherited from PCollectionType

Puppet::Pops::Types::PCollectionType::DEFAULT_SIZE, Puppet::Pops::Types::PCollectionType::NOT_EMPTY_SIZE, Puppet::Pops::Types::PCollectionType::ZERO_SIZE

Instance Attribute Summary collapse

Attributes inherited from PCollectionType

#size_type

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from PCollectionType

#has_empty_range?, #iterable?, #size_range

Methods inherited from PAnyType

#==, #assignable?, #callable?, #callable_with?, #check_self_recursion, create, #create, #iterable?, #kind_of_callable?, #name, #new_function, #really_instance?, #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

Constructor Details

#initialize(element_type, size_type = nil) ⇒ PArrayType

Returns a new instance of PArrayType.



2340
2341
2342
2343
2344
2345
2346
2347
# File 'lib/puppet/pops/types/types.rb', line 2340

def initialize(element_type, size_type = nil)
  super(size_type)
  if !size_type.nil? && size_type.from == 0 && size_type.to == 0
    @element_type = PUnitType::DEFAULT
  else
    @element_type = element_type.nil? ? PAnyType::DEFAULT : element_type
  end
end

Instance Attribute Details

#element_typeObject (readonly)



2338
2339
2340
# File 'lib/puppet/pops/types/types.rb', line 2338

def element_type
  @element_type
end

Class Method Details

.new_function(_, loader) ⇒ Object

Returns a new function that produces an Array



2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
# File 'lib/puppet/pops/types/types.rb', line 2407

def self.new_function(_, loader)
  @new_function ||= Puppet::Functions.create_loaded_function(:new_array, loader) do

    dispatch :from_args do
      param           'Any',  :from
      optional_param  'Boolean',      :wrap
    end

    def from_args(from, wrap = false)
      case from
      when NilClass
        if wrap
          [nil]
        else
          throw :undefined_value
        end
      when Array
        from
      when Hash
        wrap ? [from] : from.to_a

      when PBinaryType::Binary
        # For older rubies, the #bytes method returns an Enumerator that must be rolled out
        wrap ? [from] : from.binary_buffer.bytes.to_a

      else
        if wrap
          [from]
        else
          if PIterableType::DEFAULT.instance?(from)
            Iterable.on(from).to_a
          else
            t = Puppet::Pops::Types::TypeCalculator.singleton.infer(from).generalize
            raise TypeConversionError.new("Value of type '#{t}' cannot be converted to Array")
          end
        end
      end
    end
  end
end

.register_ptype(loader, ir) ⇒ Object



2329
2330
2331
2332
2333
2334
2335
2336
# File 'lib/puppet/pops/types/types.rb', line 2329

def self.register_ptype(loader, ir)
  create_ptype(loader, ir, 'CollectionType',
    'element_type' => {
      KEY_TYPE => POptionalType.new(PType::DEFAULT),
      KEY_VALUE => PAnyType::DEFAULT
    }
  )
end

Instance Method Details

#accept(visitor, guard) ⇒ Object



2349
2350
2351
2352
# File 'lib/puppet/pops/types/types.rb', line 2349

def accept(visitor, guard)
  super
  @element_type.accept(visitor, guard)
end

#callable_args?(callable, guard = nil) ⇒ Boolean

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.

Returns:

  • (Boolean)


2355
2356
2357
2358
2359
2360
# File 'lib/puppet/pops/types/types.rb', line 2355

def callable_args?(callable, guard = nil)
  param_t = callable.param_types
  block_t = callable.block_type
  # does not support calling with a block, but have to check that callable is ok with missing block
  (param_t.nil? || param_t.assignable?(self, guard)) && (block_t.nil? || block_t.assignable?(PUndefType::DEFAULT, guard))
end

#eql?(o) ⇒ Boolean

Returns:

  • (Boolean)


2371
2372
2373
# File 'lib/puppet/pops/types/types.rb', line 2371

def eql?(o)
  super && @element_type == o.element_type
end

#generalizeObject



2362
2363
2364
2365
2366
2367
2368
2369
# File 'lib/puppet/pops/types/types.rb', line 2362

def generalize
  if PAnyType::DEFAULT.eql?(@element_type)
    DEFAULT
  else
    ge_type = @element_type.generalize
    @size_type.nil? && @element_type.equal?(ge_type) ? self : self.class.new(ge_type, nil)
  end
end

#hashObject



2375
2376
2377
# File 'lib/puppet/pops/types/types.rb', line 2375

def hash
  super ^ @element_type.hash
end

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

Returns:

  • (Boolean)


2393
2394
2395
2396
2397
2398
2399
# File 'lib/puppet/pops/types/types.rb', line 2393

def instance?(o, guard = nil)
  # The inferred type of a class derived from Array is either Runtime or Object. It's not assignable to the Array type.
  return false unless o.instance_of?(Array)
  return false unless o.all? {|element| @element_type.instance?(element, guard) }
  size_t = size_type
  size_t.nil? || size_t.instance?(o.size, guard)
end

#iterable_type(guard = nil) ⇒ Object



2401
2402
2403
# File 'lib/puppet/pops/types/types.rb', line 2401

def iterable_type(guard = nil)
  PAnyType::DEFAULT.eql?(@element_type) ? PIterableType::DEFAULT : PIterableType.new(@element_type)
end

#normalize(guard = nil) ⇒ Object



2379
2380
2381
2382
2383
2384
2385
2386
# File 'lib/puppet/pops/types/types.rb', line 2379

def normalize(guard = nil)
  if PAnyType::DEFAULT.eql?(@element_type)
    DEFAULT
  else
    ne_type = @element_type.normalize(guard)
    @element_type.equal?(ne_type) ? self : self.class.new(ne_type, @size_type)
  end
end

#resolve(type_parser, loader) ⇒ Object



2388
2389
2390
2391
# File 'lib/puppet/pops/types/types.rb', line 2388

def resolve(type_parser, loader)
  relement_type = @element_type.resolve(type_parser, loader)
  relement_type.equal?(@element_type) ? self : self.class.new(relement_type, @size_type)
end