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

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

Constant Summary collapse

DATA =
PArrayType.new(PDataType::DEFAULT, DEFAULT_SIZE)
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

Attributes inherited from PCollectionType

#element_type, #size_type

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from PCollectionType

#accept, #eql?, #has_empty_range?, #hash, #initialize, #iterable?, #iterable_type, #resolve, #size_range

Methods inherited from PAnyType

#==, #accept, #assignable?, #callable?, #check_self_recursion, #eql?, #hash, #iterable?, #iterable_type, #kind_of_callable?, #name, #new_function, #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 Visitable

#accept

Methods included from PuppetObject

#_ptype

Constructor Details

This class inherits a constructor from Puppet::Pops::Types::PCollectionType

Class Method Details

.new_function(_, loader) ⇒ Object

Returns a new function that produces an Array



2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
# File 'lib/puppet/pops/types/types.rb', line 2256

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



2213
2214
2215
2216
2217
2218
2219
2220
# File 'lib/puppet/pops/types/types.rb', line 2213

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

Instance Method Details

#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)


2223
2224
2225
2226
2227
2228
# File 'lib/puppet/pops/types/types.rb', line 2223

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

#generalizeObject



2230
2231
2232
2233
2234
2235
2236
# File 'lib/puppet/pops/types/types.rb', line 2230

def generalize
  if self == DATA
    self
  else
    super
  end
end

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

Returns:

  • (Boolean)


2246
2247
2248
2249
2250
2251
2252
# File 'lib/puppet/pops/types/types.rb', line 2246

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

#normalize(guard = nil) ⇒ Object



2238
2239
2240
2241
2242
2243
2244
# File 'lib/puppet/pops/types/types.rb', line 2238

def normalize(guard = nil)
  if self == DATA
    self
  else
    super
  end
end