Class: Puppet::Pops::Types::PArrayType
Constant Summary
collapse
- DEFAULT =
PArrayType.new(nil)
- EMPTY =
PArrayType.new(PUnitType::DEFAULT, ZERO_SIZE)
Puppet::Pops::Types::PCollectionType::DEFAULT_SIZE, Puppet::Pops::Types::PCollectionType::NOT_EMPTY_SIZE, Puppet::Pops::Types::PCollectionType::ZERO_SIZE
Instance Attribute Summary collapse
#size_type
Class Method Summary
collapse
Instance Method Summary
collapse
#has_empty_range?, #iterable?, #size_range
Methods inherited from PAnyType
#==, #assignable?, #callable?, #callable_with?, #check_self_recursion, create, #create, #iterable?, #kind_of_callable?, #loader, #name, #new_function, #really_instance?, #roundtrip_with_string?, #simple_name, simple_name, #to_alias_expanded_s, #to_s
_pcore_type, create_ptype, register_ptypes
#_pcore_all_contents, #_pcore_contents, #_pcore_init_hash, #_pcore_type, #to_s
Constructor Details
#initialize(element_type, size_type = nil) ⇒ PArrayType
Returns a new instance of PArrayType.
2535
2536
2537
2538
2539
2540
2541
2542
|
# File 'lib/puppet/pops/types/types.rb', line 2535
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_type ⇒ Object
2533
2534
2535
|
# File 'lib/puppet/pops/types/types.rb', line 2533
def element_type
@element_type
end
|
Class Method Details
.new_function(type) ⇒ Object
Returns a new function that produces an Array
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
|
# File 'lib/puppet/pops/types/types.rb', line 2603
def self.new_function(type)
@new_function ||= Puppet::Functions.create_loaded_function(:new_array, type.loader) do
dispatch :to_array do
param 'Variant[Array,Hash,Binary,Iterable]', :from
optional_param 'Boolean[false]', :wrap
end
dispatch :wrapped do
param 'Any', :from
param 'Boolean[true]', :wrap
end
argument_mismatch :on_error do
param 'Any', :from
optional_param 'Boolean', :wrap
end
def wrapped(from, _)
from.is_a?(Array) ? from : [from]
end
def to_array(from, _ = false)
case from
when Array
from
when Hash
from.to_a
when PBinaryType::Binary
from.binary_buffer.bytes.to_a
else
Iterable.on(from).to_a
end
end
def on_error(from, _ = false)
t = TypeCalculator.singleton.infer(from).generalize
_("Value of type %{type} cannot be converted to Array") % { type: t }
end
end
end
|
.register_ptype(loader, ir) ⇒ Object
Instance Method Details
#accept(visitor, guard) ⇒ Object
2544
2545
2546
2547
|
# File 'lib/puppet/pops/types/types.rb', line 2544
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.
2550
2551
2552
2553
2554
2555
|
# File 'lib/puppet/pops/types/types.rb', line 2550
def callable_args?(callable, guard = nil)
param_t = callable.param_types
block_t = callable.block_type
(param_t.nil? || param_t.assignable?(self, guard)) && (block_t.nil? || block_t.assignable?(PUndefType::DEFAULT, guard))
end
|
#eql?(o) ⇒ Boolean
2566
2567
2568
|
# File 'lib/puppet/pops/types/types.rb', line 2566
def eql?(o)
super && @element_type == o.element_type
end
|
#generalize ⇒ Object
2557
2558
2559
2560
2561
2562
2563
2564
|
# File 'lib/puppet/pops/types/types.rb', line 2557
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
|
2570
2571
2572
|
# File 'lib/puppet/pops/types/types.rb', line 2570
def hash
super ^ @element_type.hash
end
|
#instance?(o, guard = nil) ⇒ Boolean
2588
2589
2590
2591
2592
2593
2594
2595
|
# File 'lib/puppet/pops/types/types.rb', line 2588
def instance?(o, guard = nil)
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
#normalize(guard = nil) ⇒ Object
2574
2575
2576
2577
2578
2579
2580
2581
|
# File 'lib/puppet/pops/types/types.rb', line 2574
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(loader) ⇒ Object
2583
2584
2585
2586
|
# File 'lib/puppet/pops/types/types.rb', line 2583
def resolve(loader)
relement_type = @element_type.resolve(loader)
relement_type.equal?(@element_type) ? self : self.class.new(relement_type, @size_type)
end
|