Class: Puppet::Pops::Types::PHashType
Constant Summary
collapse
- DEFAULT =
PHashType.new(nil, nil)
- KEY_PAIR_TUPLE_SIZE =
PIntegerType.new(2,2)
- DEFAULT_KEY_PAIR_TUPLE =
PTupleType.new([PUnitType::DEFAULT, PUnitType::DEFAULT], KEY_PAIR_TUPLE_SIZE)
- EMPTY =
PHashType.new(PUnitType::DEFAULT, PUnitType::DEFAULT, PIntegerType.new(0, 0))
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?, #size_range
Methods inherited from PAnyType
#==, #assignable?, #callable?, #callable_args?, #callable_with?, #check_self_recursion, create, #create, #kind_of_callable?, #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
Constructor Details
#initialize(key_type, value_type, size_type = nil) ⇒ PHashType
Returns a new instance of PHashType.
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
|
# File 'lib/puppet/pops/types/types.rb', line 2492
def initialize(key_type, value_type, size_type = nil)
super(size_type)
if !size_type.nil? && size_type.from == 0 && size_type.to == 0
@key_type = PUnitType::DEFAULT
@value_type = PUnitType::DEFAULT
else
@key_type = key_type.nil? ? PAnyType::DEFAULT : key_type
@value_type = value_type.nil? ? PAnyType::DEFAULT : value_type
end
end
|
Instance Attribute Details
2490
2491
2492
|
# File 'lib/puppet/pops/types/types.rb', line 2490
def key_type
@key_type
end
|
#value_type ⇒ Object
2490
2491
2492
|
# File 'lib/puppet/pops/types/types.rb', line 2490
def value_type
@value_type
end
|
Class Method Details
.new_function(_, loader) ⇒ Object
Returns a new function that produces a Hash
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
|
# File 'lib/puppet/pops/types/types.rb', line 2582
def self.new_function(_, loader)
@new_function ||= Puppet::Functions.create_loaded_function(:new_hash, loader) do
local_types do
type 'KeyValueArray = Array[Tuple[Any,Any],1]'
end
dispatch :from_tuples do
param 'KeyValueArray', :from
end
dispatch :from_array do
param 'Any', :from
end
def from_tuples(tuple_array)
Hash[tuple_array]
end
def from_array(from)
case from
when NilClass
throw :undefined_value
when Array
if from.size == 0
{}
else
unless from.size % 2 == 0
raise TypeConversionError.new("odd number of arguments for Hash")
end
Hash[*from]
end
when Hash
from
else
if PIterableType::DEFAULT.instance?(from)
Hash[*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 Hash")
end
end
end
end
end
|
.register_ptype(loader, ir) ⇒ Object
Instance Method Details
#accept(visitor, guard) ⇒ Object
2503
2504
2505
2506
2507
|
# File 'lib/puppet/pops/types/types.rb', line 2503
def accept(visitor, guard)
super
@key_type.accept(visitor, guard)
@value_type.accept(visitor, guard)
end
|
#element_type ⇒ Object
2509
2510
2511
2512
2513
2514
2515
|
# File 'lib/puppet/pops/types/types.rb', line 2509
def element_type
if Puppet[:strict] != :off
Puppet.warn_once('deprecations', 'Puppet::Pops::Types::PHashType#element_type',
'Puppet::Pops::Types::PHashType#element_type is deprecated, use #value_type instead')
end
@value_type
end
|
#eql?(o) ⇒ Boolean
2566
2567
2568
|
# File 'lib/puppet/pops/types/types.rb', line 2566
def eql?(o)
super && @key_type == o.key_type && @value_type == o.value_type
end
|
#generalize ⇒ Object
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
|
# File 'lib/puppet/pops/types/types.rb', line 2517
def generalize
if self == DEFAULT || self == EMPTY
self
else
key_t = @key_type
key_t = key_t.generalize
value_t = @value_type
value_t = value_t.generalize
@size_type.nil? && @key_type.equal?(key_t) && @value_type.equal?(value_t) ? self : PHashType.new(key_t, value_t, nil)
end
end
|
2539
2540
2541
|
# File 'lib/puppet/pops/types/types.rb', line 2539
def hash
super ^ @key_type.hash ^ @value_type.hash
end
|
#instance?(o, guard = nil) ⇒ Boolean
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
|
# File 'lib/puppet/pops/types/types.rb', line 2543
def instance?(o, guard = nil)
return false unless o.instance_of?(Hash)
if o.keys.all? {|key| @key_type.instance?(key, guard) } && o.values.all? {|value| @value_type.instance?(value, guard) }
size_t = size_type
size_t.nil? || size_t.instance?(o.size, guard)
else
false
end
end
|
#is_the_empty_hash? ⇒ Boolean
2570
2571
2572
|
# File 'lib/puppet/pops/types/types.rb', line 2570
def is_the_empty_hash?
self == EMPTY
end
|
#iterable?(guard = nil) ⇒ Boolean
2554
2555
2556
|
# File 'lib/puppet/pops/types/types.rb', line 2554
def iterable?(guard = nil)
true
end
|
#iterable_type(guard = nil) ⇒ Object
#normalize(guard = nil) ⇒ Object
2529
2530
2531
2532
2533
2534
2535
2536
2537
|
# File 'lib/puppet/pops/types/types.rb', line 2529
def normalize(guard = nil)
if self == DEFAULT || self == EMPTY
self
else
key_t = @key_type.normalize(guard)
value_t = @value_type.normalize(guard)
@size_type.nil? && @key_type.equal?(key_t) && @value_type.equal?(value_t) ? self : PHashType.new(key_t, value_t, @size_type)
end
end
|
#resolve(type_parser, loader) ⇒ Object
2574
2575
2576
2577
2578
|
# File 'lib/puppet/pops/types/types.rb', line 2574
def resolve(type_parser, loader)
rkey_type = @key_type.resolve(type_parser, loader)
rvalue_type = @value_type.resolve(type_parser, loader)
rkey_type.equal?(@key_type) && rvalue_type.equal?(@value_type) ? self : self.class.new(rkey_type, rvalue_type, @size_type)
end
|