Class: Humidifier::Loader::Compiler

Inherits:
Object
  • Object
show all
Defined in:
lib/humidifier/loader.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(specification) ⇒ Compiler

Returns a new instance of Compiler.



10
11
12
13
14
15
16
17
18
# File 'lib/humidifier/loader.rb', line 10

def initialize(specification)
  @specification = specification

  # Set an initial value for each property types so that we can handle
  # cycles in the specification
  @property_types = specification["PropertyTypes"].to_h do |name, _|
    [name, {}]
  end
end

Instance Attribute Details

#property_typesObject (readonly)

Returns the value of attribute property_types.



8
9
10
# File 'lib/humidifier/loader.rb', line 8

def property_types
  @property_types
end

#specificationObject (readonly)

Returns the value of attribute specification.



8
9
10
# File 'lib/humidifier/loader.rb', line 8

def specification
  @specification
end

Instance Method Details

#compileObject



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
# File 'lib/humidifier/loader.rb', line 20

def compile
  # Loop through every property type that's already defined and build up
  # each of the properties into the list
  property_types.each do |property_type_name, property_type|
    prefix = property_type_name.split(".", 2).first

    subspec = specification["PropertyTypes"].fetch(property_type_name)
    subspec.fetch("Properties") { {} }.each do |property_name, property|
      property = build_property(prefix, property_name, property)
      property_type[property.name] = property if property
    end
  end

  # Loop through every resource type in the specification and define a
  # class for each one dynamically.
  specification["ResourceTypes"].each do |aws_name, resource_type|
    _top, group, resource = aws_name.split("::")

    properties = {}
    resource_type["Properties"].each do |property_name, property|
      property = build_property(aws_name, property_name, property)
      properties[property.name] = property if property
    end

    resource_class =
      Class.new(Resource) do
        self.aws_name = aws_name
        self.props = properties
      end

    group_module =
      if Humidifier.const_defined?(group)
        Humidifier.const_get(group)
      else
        Humidifier.const_set(group, Module.new)
      end

    Humidifier.registry[aws_name] =
      group_module.const_set(resource, resource_class)
  end

  Humidifier.registry.freeze
end