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



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

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)



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

def name_or_pattern
  @name_or_pattern
end

#runtimeObject (readonly)



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

def runtime
  @runtime
end

Class Method Details

.register_ptype(loader, ir) ⇒ Object



9
10
11
12
13
14
15
16
17
18
19
# 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.



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

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)


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

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.



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

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



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

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

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

Returns:

  • (Boolean)


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

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

#iterable?(guard = nil) ⇒ Boolean

Returns:

  • (Boolean)


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

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



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

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.



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

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