Module: CfnDsl::Types

Included in:
AWS::Types, OS::Types
Defined in:
lib/cfndsl/types.rb

Overview

rubocop:disable Metrics/ModuleLength

Class Method Summary collapse

Class Method Details

.included(type_def) ⇒ Object

rubocop:disable Metrics/MethodLength, Metrics/AbcSize, Metrics/PerceivedComplexity, Metrics/CyclomaticComplexity



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
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
67
68
69
70
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
135
136
137
138
139
140
141
142
143
144
145
146
147
# File 'lib/cfndsl/types.rb', line 11

def self.included(type_def)
  types_list = YAML.load(File.open("#{File.dirname(__FILE__)}/#{type_def::TYPE_PREFIX}/types.yaml"))
  type_def.const_set('Types_Internal', types_list)

  # Do a little sanity checking - all of the types referenced in Resources
  # should be represented in Types
  types_list['Resources'].keys.each do |resource_name|
    resource = types_list['Resources'][resource_name]
    resource.values.each do |thing|
      thing.values.each do |type|
        if type.is_a?(Array)
          type.each do |inner_type|
            puts "unknown type #{inner_type}" unless types_list['Types'].key?(inner_type)
          end
        else
          puts "unknown type #{type}" unless types_list['Types'].key?(type)
        end
      end
    end
  end

  # All of the type values should also be references
  types_list['Types'].values do |type|
    if type.respond_to?(:values)
      type.values.each do |tv|
        puts "unknown type #{tv}" unless types_list['Types'].key?(tv)
      end
    end
  end

  classes = {}

  # Go through and declare all of the types first
  types_list['Types'].each_key do |typename|
    if !type_def.const_defined?(typename)
      klass = type_def.const_set(typename, Class.new(type_def::Type))
      classes[typename] = klass
    else
      classes[typename] = type_def.const_get(typename)
    end
  end

  # Now go through them again and define attribute setter methods
  classes.each_pair do |typename, type|
    typeval = types_list['Types'][typename]
    next unless typeval.respond_to?(:each_pair)
    typeval.each_pair do |attr_name, attr_type|
      if attr_type.is_a?(Array)
        klass = type_def.const_get(attr_type[0])
        variable = "@#{attr_name}".to_sym

        method = CfnDsl::Plurals.singularize(attr_name)
        methods = attr_name
        all_methods = CfnDsl.method_names(method) + CfnDsl.method_names(methods)
        type.class_eval do
          all_methods.each do |method_name|
            define_method(method_name) do |value = nil, *rest, &block|
              existing = instance_variable_get(variable)
              # For no-op invocations, get out now
              return existing if value.nil? && rest.empty? && !block

              # We are going to modify the value in some
              # way, make sure that we have an array to mess
              # with if we start with nothing
              existing = instance_variable_set(variable, []) unless existing

              # special case for just a block, no args
              if value.nil? && rest.empty? && block
                val = klass.new
                existing.push val
                value.instance_eval(&block(val))
                return existing
              end

              # Glue all of our parameters together into
              # a giant array - flattening one level deep, if needed
              array_params = []
              if value.is_a?(Array)
                value.each { |x| array_params.push x }
              else
                array_params.push value
              end
              unless rest.empty?
                rest.each do |v|
                  if v.is_a?(Array)
                    array_params += rest
                  else
                    array_params.push v
                  end
                end
              end

              # Here, if we were given multiple arguments either
              # as method [a,b,c], method(a,b,c), or even
              # method( a, [b], c) we end up with
              # array_params = [a,b,c]
              #
              # array_params will have at least one item
              # unless the user did something like pass in
              # a bunch of empty arrays.
              if block
                array_params.each do |array_params_value|
                  value = klass.new
                  existing.push value
                  value.instance_eval(&block(array_params_value)) if block
                end
              else
                # List of parameters with no block -
                # hope that the user knows what he is
                # doing and stuff them into our existing
                # array
                array_params.each do |_|
                  existing.push value
                end
              end
              return existing
            end
          end
        end
      else
        klass = type_def.const_get(attr_type)
        variable = "@#{attr_name}".to_sym

        type.class_eval do
          CfnDsl.method_names(attr_name) do |inner_method|
            define_method(inner_method) do |value = nil, *_rest, &block|
              value ||= klass.new
              instance_variable_set(variable, value)
              value.instance_eval(&block) if block
              value
            end
          end
        end
      end
    end
  end
end