Class: Puppet::Pops::Types::PRuntimeType

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

Constant Summary collapse

TYPE_NAME_OR_PATTERN =
PVariantType.new([PStringType::NON_EMPTY, PTupleType.new([PRegexpType::DEFAULT, PStringType::NON_EMPTY])])
DEFAULT =
PRuntimeType.new(nil, nil)
RUBY =
PRuntimeType.new(:ruby, nil)

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from PAnyType

#==, #accept, #assignable?, #callable?, #callable_args?, #callable_with?, #check_self_recursion, #create, create, #generalize, #kind_of_callable?, #loader, #name, new_function, #new_function, #normalize, #really_instance?, #resolve, #roundtrip_with_string?, #simple_name, simple_name, #to_alias_expanded_s, #to_s

Methods inherited from TypedModelObject

_pcore_type, create_ptype, register_ptypes

Methods included from Visitable

#accept

Methods included from PuppetObject

#_pcore_all_contents, #_pcore_contents, #_pcore_init_hash, #_pcore_type, #to_s

Constructor Details

#initialize(runtime, name_or_pattern) ⇒ PRuntimeType

Creates a new instance of a Runtime type

Parameters:

  • runtime (String)

    the name of the runtime, e.g. ‘ruby’

  • name_or_pattern (String, Array(Regexp,String))

    name of runtime or two patterns, mapping Puppet name => runtime name



29
30
31
32
33
34
35
# File 'lib/puppet/pops/types/p_runtime_type.rb', line 29

def initialize(runtime, name_or_pattern)
  unless runtime.nil? || runtime.is_a?(Symbol)
    runtime = TypeAsserter.assert_instance_of("Runtime 'runtime'", PStringType::NON_EMPTY, runtime).to_sym
  end
  @runtime = runtime
  @name_or_pattern = TypeAsserter.assert_instance_of("Runtime 'name_or_pattern'", TYPE_NAME_OR_PATTERN, name_or_pattern, true)
end

Instance Attribute Details

#name_or_patternObject (readonly)



22
23
24
# File 'lib/puppet/pops/types/p_runtime_type.rb', line 22

def name_or_pattern
  @name_or_pattern
end

#runtimeObject (readonly)



22
23
24
# File 'lib/puppet/pops/types/p_runtime_type.rb', line 22

def runtime
  @runtime
end

Class Method Details

.register_ptype(loader, ir) ⇒ Object



9
10
11
12
13
14
15
16
17
18
19
20
# File 'lib/puppet/pops/types/p_runtime_type.rb', line 9

def self.register_ptype(loader, ir)
  create_ptype(loader, ir, 'AnyType',
    'runtime' => {
      KEY_TYPE => POptionalType.new(PStringType::NON_EMPTY),
      KEY_VALUE => nil
    },
    'name_or_pattern' => {
      KEY_TYPE => POptionalType.new(TYPE_NAME_OR_PATTERN),
      KEY_VALUE => nil
    }
  )
end

Instance Method Details

#class_or_moduleObject

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.



70
71
72
73
74
75
76
# File 'lib/puppet/pops/types/p_runtime_type.rb', line 70

def class_or_module
  raise "Only ruby classes or modules can be produced by this runtime, got '#{runtime}" unless runtime == :ruby
  raise 'A pattern based Runtime type cannot produce a class or module' if @name_or_pattern.is_a?(Array)
  com = ClassLoader.provide(self)
  raise "The name #{@name_or_pattern} does not represent a ruby class or module" if com.nil?
  com
end

#eql?(o) ⇒ Boolean

Returns:

  • (Boolean)


41
42
43
# File 'lib/puppet/pops/types/p_runtime_type.rb', line 41

def eql?(o)
  self.class == o.class && @runtime == o.runtime && @name_or_pattern == o.name_or_pattern
end

#from_puppet_name(puppet_name) ⇒ Object

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.



79
80
81
82
83
84
85
86
# File 'lib/puppet/pops/types/p_runtime_type.rb', line 79

def from_puppet_name(puppet_name)
  if @name_or_pattern.is_a?(Array)
    substituted = puppet_name.sub(*@name_or_pattern)
    substituted == puppet_name ? nil : PRuntimeType.new(@runtime, substituted)
  else
    nil
  end
end

#hashObject



37
38
39
# File 'lib/puppet/pops/types/p_runtime_type.rb', line 37

def hash
  @runtime.hash ^ @name_or_pattern.hash
end

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

Returns:

  • (Boolean)


45
46
47
# File 'lib/puppet/pops/types/p_runtime_type.rb', line 45

def instance?(o, guard = nil)
  assignable?(TypeCalculator.infer(o), guard)
end

#iterable?(guard = nil) ⇒ Boolean

Returns:

  • (Boolean)


49
50
51
52
53
54
55
56
57
58
# File 'lib/puppet/pops/types/p_runtime_type.rb', line 49

def iterable?(guard = nil)
  if @runtime == :ruby && !runtime_type_name.nil?
    begin
      c = ClassLoader.provide(self)
      return c < Iterable unless c.nil?
    rescue ArgumentError
    end
  end
  false
end

#iterable_type(guard = nil) ⇒ Object



60
61
62
# File 'lib/puppet/pops/types/p_runtime_type.rb', line 60

def iterable_type(guard = nil)
  iterable?(guard) ? PIterableType.new(self) : nil
end

#runtime_type_nameObject

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.



65
66
67
# File 'lib/puppet/pops/types/p_runtime_type.rb', line 65

def runtime_type_name
  @name_or_pattern.is_a?(String) ? @name_or_pattern : nil
end