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

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

Overview

API:

  • public

Constant Summary collapse

DATA =

API:

  • public

PArrayType.new(PDataType::DEFAULT, DEFAULT_SIZE)
DEFAULT =

API:

  • public

PArrayType.new(nil)
EMPTY =

API:

  • public

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?, #check_self_recursion, #create, #iterable?, #kind_of_callable?, #name, #new_function, #really_instance?, #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(element_type, size_type = nil) ⇒ PArrayType

Returns a new instance of PArrayType.

API:

  • public



2269
2270
2271
2272
2273
2274
2275
2276
# File 'lib/puppet/pops/types/types.rb', line 2269

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

Instance Attribute Details

#element_typeObject (readonly)

API:

  • public



2267
2268
2269
# File 'lib/puppet/pops/types/types.rb', line 2267

def element_type
  @element_type
end

Class Method Details

.new_function(_, loader) ⇒ Object

Returns a new function that produces an Array

API:

  • public



2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
# File 'lib/puppet/pops/types/types.rb', line 2341

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

API:

  • public



2258
2259
2260
2261
2262
2263
2264
2265
# File 'lib/puppet/pops/types/types.rb', line 2258

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

Instance Method Details

#accept(visitor, guard) ⇒ Object

API:

  • public



2278
2279
2280
2281
# File 'lib/puppet/pops/types/types.rb', line 2278

def accept(visitor, guard)
  super
  @element_type.accept(visitor, guard) unless @element_type.nil?
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:

API:

  • private



2284
2285
2286
2287
2288
2289
# File 'lib/puppet/pops/types/types.rb', line 2284

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:

API:

  • public



2302
2303
2304
# File 'lib/puppet/pops/types/types.rb', line 2302

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

#generalizeObject

API:

  • public



2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
# File 'lib/puppet/pops/types/types.rb', line 2291

def generalize
  if self == DATA
    self
  elsif @element_type.nil?
    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

API:

  • public



2306
2307
2308
# File 'lib/puppet/pops/types/types.rb', line 2306

def hash
  super ^ @element_type.hash
end

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

Returns:

API:

  • public



2327
2328
2329
2330
2331
2332
2333
# File 'lib/puppet/pops/types/types.rb', line 2327

def instance?(o, guard = nil)
  return false unless o.is_a?(Array)
  element_t = element_type
  return false unless element_t.nil? || o.all? {|element| element_t.instance?(element, guard) }
  size_t = size_type
  size_t.nil? || size_t.instance?(o.size, guard)
end

#iterable_type(guard = nil) ⇒ Object

API:

  • public



2335
2336
2337
# File 'lib/puppet/pops/types/types.rb', line 2335

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

#normalize(guard = nil) ⇒ Object

API:

  • public



2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
# File 'lib/puppet/pops/types/types.rb', line 2310

def normalize(guard = nil)
  if self == DATA
    self
  elsif @element_type.nil?
    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

API:

  • public



2321
2322
2323
2324
2325
# File 'lib/puppet/pops/types/types.rb', line 2321

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