Method: Puppet::Pops::Types::PInitType#new_function

Defined in:
lib/puppet/pops/types/p_init_type.rb

#new_functionObject



71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
# File 'lib/puppet/pops/types/p_init_type.rb', line 71

def new_function
  return super if type.nil?
  assert_initialized

  target_type = type
  single_type = @single_type
  if @init_args.empty?
    @new_function ||= Puppet::Functions.create_function(:new_Init, Puppet::Functions::InternalFunction) do
      @target_type = target_type
      @single_type = single_type

      dispatch :from_array do
        scope_param
        param 'Array', :value
      end

      dispatch :create do
        scope_param
        param 'Any', :value
      end

      def self.create(scope, value, func)
        func.call(scope, @target_type, value)
      end

      def self.from_array(scope, value, func)
        # If there is a single argument that matches the array, then that gets priority over
        # expanding the array into all arguments
        if @single_type.instance?(value) || (@other_type && !@other_type.instance?(value) && @has_optional_single && @other_type.instance?([value]))
          func.call(scope, @target_type, value)
        else
          func.call(scope, @target_type, *value)
        end
      end

      def from_array(scope, value)
        self.class.from_array(scope, value, loader.load(:function, 'new'))
      end

      def create(scope, value)
        self.class.create(scope, value, loader.load(:function, 'new'))
      end
    end
  else
    init_args = @init_args
    @new_function ||= Puppet::Functions.create_function(:new_Init, Puppet::Functions::InternalFunction) do
      @target_type = target_type
      @init_args = init_args

      dispatch :create do
        scope_param
        param 'Any', :value
      end

      def self.create(scope, value, func)
        func.call(scope, @target_type, value, *@init_args)
      end

      def create(scope, value)
        self.class.create(scope, value, loader.load(:function, 'new'))
      end
    end
  end
end