Class: Puppet::Pops::Types::RubyGenerator Private

Inherits:
TypeFormatter show all
Defined in:
lib/puppet/pops/types/ruby_generator.rb

This class is part of a private API. You should avoid using this class if possible, as it may be removed or be changed in the future.

Constant Summary

Constants inherited from TypeFormatter

TypeFormatter::NAME_SEGMENT_SEPARATOR, TypeFormatter::STARTS_WITH_ASCII_CAPITAL

Instance Method Summary collapse

Methods inherited from TypeFormatter

#alias_expanded_string, #append_callable_params, #append_default, #append_string, #capitalize_segments, #debug_string, #indented_string, #ruby_string, singleton, string, #string, #string_Array, #string_FalseClass, #string_Hash, #string_Module, #string_NilClass, #string_Numeric, #string_PAnnotatedMember, #string_PAnyType, #string_PArrayType, #string_PBinaryType, #string_PBooleanType, #string_PCallableType, #string_PCatalogEntryType, #string_PCollectionType, #string_PDataType, #string_PDefaultType, #string_PEnumType, #string_PFloatType, #string_PHashType, #string_PHostClassType, #string_PIntegerType, #string_PIterableType, #string_PIteratorType, #string_PNotUndefType, #string_PNumericType, #string_PObjectType, #string_POptionalType, #string_PPatternType, #string_PRegexpType, #string_PResourceType, #string_PRuntimeType, #string_PScalarType, #string_PSemVerRangeType, #string_PSemVerType, #string_PSensitiveType, #string_PStringType, #string_PStructType, #string_PTimespanType, #string_PTimestampType, #string_PTupleType, #string_PType, #string_PTypeAliasType, #string_PTypeReferenceType, #string_PTypeSetType, #string_PUndefType, #string_PUnitType, #string_PVariantType, #string_Regexp, #string_String, #string_Symbol, #string_Timespan, #string_Timestamp, #string_TrueClass, #string_Version, #string_VersionRange, #struct_element_pair, #symbolic_key, #to_s

Instance Method Details

#class_body(obj, segments, bld) ⇒ 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.



137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
# File 'lib/puppet/pops/types/ruby_generator.rb', line 137

def class_body(obj, segments, bld)
  if obj.parent.nil?
    bld << "\n  include " << namespace_relative(segments, Puppet::Pops::Types::PuppetObject.name) << "\n\n" # marker interface
    bld << "  def self.ref(type_string)\n"
    bld << '    ' << namespace_relative(segments, Puppet::Pops::Types::PTypeReferenceType.name) << ".new(type_string)\n"
    bld << "  end\n"
  end

  # Output constants
  constants, others = obj.attributes(true).values.partition { |a| a.kind == PObjectType::ATTRIBUTE_KIND_CONSTANT }
  constants = constants.select { |ca| ca.container.equal?(obj) }
  unless constants.empty?
    constants.each { |ca| bld << "\n  def self." << ca.name << "\n    _ptype['" << ca.name << "'].value\n  end\n" }
    constants.each { |ca| bld << "\n  def " << ca.name << "\n    self.class." << ca.name << "\n  end\n" }
  end

  init_params = others.reject { |a| a.kind == PObjectType::ATTRIBUTE_KIND_DERIVED }
  opt, non_opt = init_params.partition { |ip| ip.value? }

  # Output type safe hash constructor
  bld << "\n  def self.from_hash(i12n)\n"
  bld << '    from_asserted_hash(' << namespace_relative(segments, TypeAsserter.name) << '.assert_instance_of('
  bld << "'" << obj.label << " initializer', _ptype.i12n_type, i12n))\n  end\n\n  def self.from_asserted_hash(i12n)\n    new(\n"
  non_opt.each { |ip| bld << "      i12n['" << ip.name << "'],\n" }
  opt.each { |ip| bld << "      i12n.fetch('" << ip.name << "') { _ptype['" << ip.name << "'].value },\n" }
  bld.chomp!(",\n")
  bld << ")\n  end\n"

  # Output type safe constructor
  bld << "\n  def self.create"
  if init_params.empty?
    bld << "\n    new"
  else
    bld << '('
    non_opt.each { |ip| bld << ip.name << ', ' }
    opt.each { |ip| bld << ip.name << ' = ' << "_ptype['#{ip.name}'].value" << ', ' }
    bld.chomp!(', ')
    bld << ")\n"
    bld << '    ta = ' << namespace_relative(segments, TypeAsserter.name) << "\n"
    bld << "    attrs = _ptype.attributes(true)\n"
    init_params.each do |a|
      bld << "    ta.assert_instance_of('" << a.container.name << '[' << a.name << ']'
      bld << "', attrs['" << a.name << "'].type, " << a.name << ")\n"
    end
    bld << '    new('
    non_opt.each { |a| bld << a.name << ', ' }
    opt.each { |a| bld << a.name << ', ' }
    bld.chomp!(', ')
    bld << ')'
  end
  bld << "\n  end\n"

  # Output initializer
  bld << "\n  def initialize"
  unless init_params.empty?
    bld << '('
    non_opt.each { |ip| bld << ip.name << ', ' }
    opt.each { |ip| bld << ip.name << ' = ' << "_ptype['#{ip.name}'].value" << ', ' }
    bld.chomp!(', ')
    bld << ')'
    unless obj.parent.nil?
      bld << "\n    super"
      super_args = (non_opt + opt).select { |ip| !ip.container.equal?(obj) }
      unless super_args.empty?
        bld << '('
        super_args.each { |ip| bld << ip.name << ', ' }
        bld.chomp!(', ')
        bld << ')'
      end
    end
  end
  bld << "\n"

  init_params.each { |a| bld << '    @' << a.name << ' = ' << a.name << "\n" if a.container.equal?(obj) }
  bld << "  end\n\n"

  # Output attr_readers
  others.each do |a|
    next unless a.container.equal?(obj)
    if a.kind == PObjectType::ATTRIBUTE_KIND_DERIVED || a.kind == PObjectType::ATTRIBUTE_KIND_GIVEN_OR_DERIVED
      bld << '  def ' << a.name << "\n"
      bld << "    raise Puppet::Error, \"no method is implemented for derived attribute #{a.label}\"\n"
      bld << "  end\n"
    else
      bld << '  attr_reader :' << a.name << "\n"
    end
  end

  # Output function placeholders
  obj.functions(false).each_value do |func|
    bld << "\n  def " << func.name << "(*args)\n"
    bld << "    # Placeholder for #{func.type}\n"
    bld << "    raise Puppet::Error, \"no method is implemented for #{func.label}\"\n"
    bld << "  end\n"
  end

  # output hash and equality
  include_class = obj.include_class_in_equality?
  if obj.equality.nil?
    eq_names = obj.attributes(false).values.select { |a| a.kind != PObjectType::ATTRIBUTE_KIND_CONSTANT }.map(&:name)
  else
    eq_names = obj.equality
  end

  unless eq_names.empty? && !include_class
    bld << "\n  def hash\n    "
    bld << 'super.hash ^ ' unless obj.parent.nil?
    if eq_names.empty?
      bld << "self.class.hash\n"
    else
      bld << '['
      bld << 'self.class, ' if include_class
      eq_names.each { |eqn| bld << '@' << eqn << ', ' }
      bld.chomp!(', ')
      bld << "].hash\n"
    end
    bld << "  end\n"

    bld << "\n  def eql?(o)\n"
    bld << "    super.eql?(o) &&\n" unless obj.parent.nil?
    bld << "    self.class.eql?(o.class) &&\n" if include_class
    eq_names.each { |eqn| bld << '    @' << eqn << '.eql?(o.' <<  eqn << ") &&\n" }
    bld.chomp!(" &&\n")
    bld << "\n  end\n"
  end
end

#class_definition(obj, namespace_segments, bld, class_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.



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
135
# File 'lib/puppet/pops/types/ruby_generator.rb', line 103

def class_definition(obj, namespace_segments, bld, class_name)
  module_segments = remove_common_namespace(namespace_segments, class_name)
  leaf_name = module_segments.pop
  module_segments.each { |segment| bld << 'module ' << segment << "\n" }
  bld << 'class ' << leaf_name
  segments = class_name.split(TypeFormatter::NAME_SEGMENT_SEPARATOR)

  unless obj.parent.nil?
    ir = Loaders.implementation_registry
    parent_impl = ir.module_name_for_type(obj.parent)
    raise Puppet::Error, "Unable to create an instance of #{obj.parent.name}. No mapping exists to runtime object" if parent_impl.nil?
    bld << ' < ' << namespace_relative(segments, parent_impl[0])
  end

  bld << "\n"
  bld << "  def self._plocation\n"
  bld << "    loc = Puppet::Util.path_to_uri(\"\#{__FILE__}\")\n"
  bld << "    URI(\"#\{loc}?line=#\{__LINE__.to_i - 3}\")\n"
  bld << "  end\n"

  bld << "\n"
  bld << "  def self._ptype\n"
  bld << '    @_ptype ||= ' << namespace_relative(segments, obj.class.name) << ".new('" << obj.name << "',\n"
  bld << TypeFormatter.new.ruby_string('ref', 3, obj.i12n_hash(false)) << "    )\n"
  bld << "  end\n"

  class_body(obj, segments, bld)

  bld << "end\n"
  module_segments.size.times { bld << "end\n" }
  module_segments << leaf_name
  module_segments.join(TypeFormatter::NAME_SEGMENT_SEPARATOR)
end

#create_class(obj) ⇒ 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.

Raises:

  • (ArgumentError)


19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/puppet/pops/types/ruby_generator.rb', line 19

def create_class(obj)
  @dynamic_classes ||= Hash.new do |hash, key|
    rp = key.resolved_parent
    parent_class = rp.is_a?(PObjectType) ? create_class(rp) : Object
    class_def = ''
    class_body(key, EMPTY_ARRAY, class_def)
    cls = Class.new(parent_class)
    cls.class_eval(class_def)
    cls.define_singleton_method(:_ptype) { return key }
    hash[key] = cls
  end
  raise ArgumentError, "Expected a Puppet Type, got '#{obj.class.name}'" unless obj.is_a?(PAnyType)
  @dynamic_classes[obj]
end

#end_module(common_prefix, aliases, class_names, bld) ⇒ 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.



73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
# File 'lib/puppet/pops/types/ruby_generator.rb', line 73

def end_module(common_prefix, aliases, class_names, bld)
  # Emit registration of contained type aliases
  unless aliases.empty?
    bld << "Puppet::Pops::Pcore.register_aliases({\n"
    aliases.each { |name, type| bld << "  '" << name << "' => " << TypeFormatter.string(type.to_s) << "\n" }
    bld.chomp!(",\n")
    bld << "})\n\n"
  end

  # Emit registration of contained types
  unless class_names.empty?
    bld << "Puppet::Pops::Pcore.register_implementations([\n"
    class_names.each { |class_name| bld << '  ' << class_name << ",\n" }
    bld.chomp!(",\n")
    bld << "])\n\n"
  end
  bld.chomp!("\n")

  common_prefix.size.times { bld << "end\n" }
end

#implementation_names(object_types) ⇒ 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.



94
95
96
97
98
99
100
101
# File 'lib/puppet/pops/types/ruby_generator.rb', line 94

def implementation_names(object_types)
  object_types.map do |type|
    ir = Loaders.implementation_registry
    impl_name = ir.module_name_for_type(type)
    raise Puppet::Error, "Unable to create an instance of #{type.name}. No mapping exists to runtime object" if impl_name.nil?
    impl_name[0]
  end
end

#module_definition(types, comment) ⇒ 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.



40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/puppet/pops/types/ruby_generator.rb', line 40

def module_definition(types, comment)
  object_types, aliased_types = types.partition { |type| type.is_a?(PObjectType) }
  impl_names = implementation_names(object_types)

  # extract common implementation module prefix
  common_prefix = []
  segmented_names = impl_names.map { |impl_name| impl_name.split(TypeFormatter::NAME_SEGMENT_SEPARATOR) }
  segments = segmented_names[0]
  segments.size.times do |idx|
    segment = segments[idx]
    break unless segmented_names.all? { |sn| sn[idx] == segment }
    common_prefix << segment
  end

  # Create class definition of all contained types
  bld = ''
  start_module(common_prefix, comment, bld)
  class_names = []
  object_types.each_with_index do |type, index|
    class_names << class_definition(type, common_prefix, bld, impl_names[index])
    bld << "\n"
  end

  aliases = Hash[aliased_types.map { |type| [type.name, type.resolved_type] }]
  end_module(common_prefix, aliases, class_names, bld)
  bld
end

#module_definition_from_typeset(typeset) ⇒ 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.



34
35
36
37
38
# File 'lib/puppet/pops/types/ruby_generator.rb', line 34

def module_definition_from_typeset(typeset)
  module_definition(
    typeset.types.values,
    "# Generated by #{self.class.name} from TypeSet #{typeset.name} on #{Date.new}\n")
end

#namespace_relative(namespace_segments, 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.



15
16
17
# File 'lib/puppet/pops/types/ruby_generator.rb', line 15

def namespace_relative(namespace_segments, name)
  remove_common_namespace(namespace_segments, name).join(TypeFormatter::NAME_SEGMENT_SEPARATOR)
end

#remove_common_namespace(namespace_segments, 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.



6
7
8
9
10
11
12
13
# File 'lib/puppet/pops/types/ruby_generator.rb', line 6

def remove_common_namespace(namespace_segments, name)
  segments = name.split(TypeFormatter::NAME_SEGMENT_SEPARATOR)
  namespace_segments.size.times do |idx|
    break if segments.empty? || namespace_segments[idx] != segments[0]
    segments.shift
  end
  segments
end

#start_module(common_prefix, comment, bld) ⇒ 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.



68
69
70
71
# File 'lib/puppet/pops/types/ruby_generator.rb', line 68

def start_module(common_prefix, comment, bld)
  bld << '# ' << comment << "\n"
  common_prefix.each { |cp| bld << 'module ' << cp << "\n" }
end