class RubyTerraform::Options::Definition

rubocop:disable Metrics/ClassLength

Public Class Methods

new(opts) click to toggle source

rubocop:disable Metrics/MethodLength

Calls superclass method
# File lib/ruby_terraform/options/definition.rb, line 23
def initialize(opts)
  raise 'Missing name.' unless opts[:name]

  super(
    name: Name.new(opts[:name]),
    option_type: Types.resolve(opts[:option_type]) || Types::Standard,
    value_type: Values.resolve(opts[:value_type]) || Values::String,
    repeatable: opts[:repeatable] || false,
    separator: opts[:separator],
    placement: opts[:placement],
    extra_keys:
      { singular: [], plural: [] }
        .merge(opts[:extra_keys] || {}),
    override_keys:
      { singular: nil, plural: nil }
        .merge(opts[:override_keys] || {})
  )
end

Public Instance Methods

build(parameters) click to toggle source
# File lib/ruby_terraform/options/definition.rb, line 47
def build(parameters)
  build_singular_options(parameters) + build_plural_options(parameters)
end
matches?(name) click to toggle source

rubocop:enable Metrics/MethodLength

# File lib/ruby_terraform/options/definition.rb, line 43
def matches?(name)
  @name == Name.new(name)
end

Private Instance Methods

all_plural_keys() click to toggle source
# File lib/ruby_terraform/options/definition.rb, line 73
def all_plural_keys
  ([resolved_plural_key] + extra_keys[:plural]).compact
end
all_singular_keys() click to toggle source
# File lib/ruby_terraform/options/definition.rb, line 61
def all_singular_keys
  ([resolved_singular_key] + extra_keys[:singular]).compact
end
build_key_value(key, value) click to toggle source
# File lib/ruby_terraform/options/definition.rb, line 109
def build_key_value(key, value)
  Values::KeyValue.new(key, build_value(value))
end
build_key_value_options(value) click to toggle source
# File lib/ruby_terraform/options/definition.rb, line 156
def build_key_value_options(value)
  value.map { |k, v| build_option(build_key_value(k, v)) }
end
build_multiple_options(value) click to toggle source
# File lib/ruby_terraform/options/definition.rb, line 160
def build_multiple_options(value)
  value.map { |v| build_option(build_value(v)) }
end
build_no_options() click to toggle source
# File lib/ruby_terraform/options/definition.rb, line 168
def build_no_options
  []
end
build_option(value) click to toggle source
# File lib/ruby_terraform/options/definition.rb, line 101
def build_option(value)
  option_type.new(name, value, separator: separator, placement: placement)
end
build_plural(value) click to toggle source
# File lib/ruby_terraform/options/definition.rb, line 133
def build_plural(value)
  if only_singular?(value)
    build_no_options
  elsif key_valued?(value)
    build_key_value_options(value)
  elsif multi_valued?(value)
    build_multiple_options(value)
  else
    build_single_option(value)
  end
end
build_plural_options(parameters) click to toggle source
# File lib/ruby_terraform/options/definition.rb, line 149
def build_plural_options(parameters)
  keys = all_plural_keys
  values = values(parameters, keys)

  build_plurals(values)
end
build_plurals(values) click to toggle source
# File lib/ruby_terraform/options/definition.rb, line 145
def build_plurals(values)
  values.map { |p| build_plural(p) }.flatten
end
build_single_option(value) click to toggle source
# File lib/ruby_terraform/options/definition.rb, line 164
def build_single_option(value)
  [build_option(build_value(value))]
end
build_singular(value) click to toggle source
# File lib/ruby_terraform/options/definition.rb, line 113
def build_singular(value)
  build_single_option(value)
end
build_singular_options(parameters) click to toggle source
# File lib/ruby_terraform/options/definition.rb, line 121
def build_singular_options(parameters)
  keys = all_singular_keys
  values = values(parameters, keys)

  if too_many_values?(values)
    raise "Multiple values provided for '#{name}' " \
          "(with keys #{keys}) and option not repeatable."
  end

  build_singulars(values)
end
build_singulars(values) click to toggle source
# File lib/ruby_terraform/options/definition.rb, line 117
def build_singulars(values)
  values.map { |p| build_singular(p) }.flatten
end
build_value(value) click to toggle source
# File lib/ruby_terraform/options/definition.rb, line 105
def build_value(value)
  value_type.new(value)
end
key_valued?(value) click to toggle source
# File lib/ruby_terraform/options/definition.rb, line 93
def key_valued?(value)
  value.respond_to?(:keys)
end
multi_valued?(value) click to toggle source
# File lib/ruby_terraform/options/definition.rb, line 97
def multi_valued?(value)
  value.respond_to?(:each)
end
needs_plural?(value) click to toggle source
# File lib/ruby_terraform/options/definition.rb, line 85
def needs_plural?(value)
  repeatable? && !value.nil?
end
only_singular?(value) click to toggle source
# File lib/ruby_terraform/options/definition.rb, line 89
def only_singular?(value)
  !needs_plural?(value)
end
resolved_plural_key() click to toggle source
# File lib/ruby_terraform/options/definition.rb, line 65
def resolved_plural_key
  if override_keys[:plural] == false
    nil
  else
    override_keys[:plural] || name.as_plural_key
  end
end
resolved_singular_key() click to toggle source
# File lib/ruby_terraform/options/definition.rb, line 53
def resolved_singular_key
  if override_keys[:singular] == false
    nil
  else
    override_keys[:singular] || name.as_singular_key
  end
end
too_many_values?(values) click to toggle source
# File lib/ruby_terraform/options/definition.rb, line 77
def too_many_values?(values)
  !repeatable? && values.length > 1
end
values(parameters, keys) click to toggle source
# File lib/ruby_terraform/options/definition.rb, line 81
def values(parameters, keys)
  keys.map { |k| parameters[k] }.compact
end