class Kitchen::Terraform::ConfigAttribute

ConfigAttribute is the class of objects which apply the behaviour of a configuration attribute to a module which must be included by a plugin class.

Attributes

attribute[RW]
config_attribute[RW]
default_value[RW]
schema[RW]

Public Class Methods

new(attribute:, default_value:, schema:) click to toggle source

initialize prepares a new instance of the class.

@param attribute [Symbol] the name of the attribute. @param default_value [Proc] a block which returns the default value for the attribute. @param schema [Dry::Validation::Schema] the schema of the attribute.

# File lib/kitchen/terraform/config_attribute.rb, line 43
def initialize(attribute:, default_value:, schema:)
  self.attribute = attribute
  self.default_value = default_value
  self.schema = schema
end

Public Instance Methods

apply(config_attribute:) click to toggle source

apply applies the configuration attribute behaviour to a module.

@param config_attribute [Module] a module. @return [self]

# File lib/kitchen/terraform/config_attribute.rb, line 29
def apply(config_attribute:)
  self.config_attribute = config_attribute
  define_singleton_included
  define_singleton_to_sym
  define_config_attribute_default_value

  self
end

Private Instance Methods

define_config_attribute_default_value() click to toggle source
# File lib/kitchen/terraform/config_attribute.rb, line 58
def define_config_attribute_default_value
  config_attribute
    .send(
      :define_method,
      "config_#{attribute}_default_value",
      &default_value
    )
end
define_singleton_included() click to toggle source
# File lib/kitchen/terraform/config_attribute.rb, line 67
def define_singleton_included
  local_schema = schema

  config_attribute.define_singleton_method :included do |plugin_class|
    ::Kitchen::Terraform::ConfigAttributeDefiner.new(attribute: self, schema: local_schema).define(
      plugin_class: plugin_class,
    )
  end
end
define_singleton_to_sym() click to toggle source
# File lib/kitchen/terraform/config_attribute.rb, line 77
def define_singleton_to_sym
  local_attribute = attribute

  config_attribute.define_singleton_method :to_sym do
    local_attribute
  end
  config_attribute.extend ::Kitchen::Terraform::ConfigAttributeCacher
end