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)
- DATA =
PHashType.new(PScalarType::DEFAULT, PDataType::DEFAULT, DEFAULT_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, #kind_of_callable?, #name, #new_function, #really_instance?, #simple_name, simple_name, #to_alias_expanded_s, #to_s
_ptype, create_ptype, register_ptypes
#_ptype
Constructor Details
#initialize(key_type, value_type, size_type = nil) ⇒ PHashType
Returns a new instance of PHashType.
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
|
# File 'lib/puppet/pops/types/types.rb', line 2480
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
@value_type = value_type
end
end
|
Instance Attribute Details
2478
2479
2480
|
# File 'lib/puppet/pops/types/types.rb', line 2478
def key_type
@key_type
end
|
#value_type ⇒ Object
2478
2479
2480
|
# File 'lib/puppet/pops/types/types.rb', line 2478
def value_type
@value_type
end
|
Class Method Details
.new_function(_, loader) ⇒ Object
Returns a new function that produces a Hash
2576
2577
2578
2579
2580
2581
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
|
# File 'lib/puppet/pops/types/types.rb', line 2576
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
2491
2492
2493
2494
2495
|
# File 'lib/puppet/pops/types/types.rb', line 2491
def accept(visitor, guard)
super
@key_type.accept(visitor, guard) unless @key_type.nil?
@value_type.accept(visitor, guard) unless @value_type.nil?
end
|
#element_type ⇒ Object
2497
2498
2499
2500
2501
2502
2503
|
# File 'lib/puppet/pops/types/types.rb', line 2497
def element_type
if Puppet[:strict] != :off
Puppet.warn_once(:deprecation, '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
2558
2559
2560
|
# File 'lib/puppet/pops/types/types.rb', line 2558
def eql?(o)
super && @key_type == o.key_type && @value_type == o.value_type
end
|
#generalize ⇒ Object
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
|
# File 'lib/puppet/pops/types/types.rb', line 2505
def generalize
if self == DEFAULT || self == DATA || self == EMPTY
self
else
key_t = @key_type
key_t = key_t.generalize unless key_t.nil?
value_t = @value_type
value_t = value_t.generalize unless value_t.nil?
@size_type.nil? && @key_type.equal?(key_t) && @value_type.equal?(value_t) ? self : PHashType.new(key_t, value_t, nil)
end
end
|
2529
2530
2531
|
# File 'lib/puppet/pops/types/types.rb', line 2529
def hash
super ^ @key_type.hash ^ @value_type.hash
end
|
#instance?(o, guard = nil) ⇒ Boolean
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
|
# File 'lib/puppet/pops/types/types.rb', line 2533
def instance?(o, guard = nil)
return false unless o.is_a?(Hash)
key_t = key_type
value_t = value_type
if (key_t.nil? || o.keys.all? {|key| key_t.instance?(key, guard) }) &&
(value_t.nil? || o.values.all? {|value| value_t.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
2562
2563
2564
|
# File 'lib/puppet/pops/types/types.rb', line 2562
def is_the_empty_hash?
self == EMPTY
end
|
#iterable?(guard = nil) ⇒ Boolean
2546
2547
2548
|
# File 'lib/puppet/pops/types/types.rb', line 2546
def iterable?(guard = nil)
true
end
|
#iterable_type(guard = nil) ⇒ Object
#normalize(guard = nil) ⇒ Object
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
|
# File 'lib/puppet/pops/types/types.rb', line 2517
def normalize(guard = nil)
if self == DEFAULT || self == DATA || self == EMPTY
self
else
key_t = @key_type
key_t = key_t.normalize(guard) unless key_t.nil?
value_t = @value_type
value_t = value_t.normalize(guard) unless value_t.nil?
@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
2566
2567
2568
2569
2570
2571
2572
|
# File 'lib/puppet/pops/types/types.rb', line 2566
def resolve(type_parser, loader)
rkey_type = @key_type
rkey_type = rkey_type.resolve(type_parser, loader) unless rkey_type.nil?
rvalue_type = @value_type
rvalue_type = rvalue_type.resolve(type_parser, loader) unless rvalue_type.nil?
rkey_type.equal?(@key_type) && rvalue_type.equal?(@value_type) ? self : self.class.new(rkey_type, rvalue_type, @size_type)
end
|