Class: LogStash::PluginMixins::ECSCompatibilitySupport::Selector::State Private

Inherits:
Object
  • Object
show all
Defined in:
lib/logstash/plugin_mixins/ecs_compatibility_support/selector.rb

Overview

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.

A State contains the active mode and a list of all supported modes.

It allows a developer to safely define mappings of alternative values, exactly one of which will be selected based on the effective mode.

It is NOT designed for performance, but may be helpful during instantiation.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(supported_modes, active_mode, alias_map = EMPTY_HASH) ⇒ State

Returns a new instance of State.

Parameters:

  • supported_modes (Array<Symbol>)
  • active_mode (Symbol)


122
123
124
125
126
127
128
# File 'lib/logstash/plugin_mixins/ecs_compatibility_support/selector.rb', line 122

def initialize(supported_modes, active_mode, alias_map=EMPTY_HASH)
  fail(ArgumentError, "invalid alias mapping") unless alias_map.flatten.all? {|v| supported_modes.include?(v) }

  @supported_modes = supported_modes
  @active_mode = active_mode
  @alias_map = alias_map
end

Instance Attribute Details

#active_modeObject (readonly)

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.



130
131
132
# File 'lib/logstash/plugin_mixins/ecs_compatibility_support/selector.rb', line 130

def active_mode
  @active_mode
end

#supported_modesObject (readonly)

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.



131
132
133
# File 'lib/logstash/plugin_mixins/ecs_compatibility_support/selector.rb', line 131

def supported_modes
  @supported_modes
end

Instance Method Details

#value_from(defined_choices) ⇒ Object Also known as: []

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.

With the active mode, select one of the provided options.

Parameters:

  • defined_choices (Hash{Symbol=>Object})

    : the options to chose between. it is an ArgumentError to provide a different set of options than those this State was initialized with. This ensures that all reachable code implements all supported options.

Returns:

  • (Object)


140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
# File 'lib/logstash/plugin_mixins/ecs_compatibility_support/selector.rb', line 140

def value_from(defined_choices)
  fail(ArgumentError, "defined_choices must be a Hash") unless defined_choices.kind_of?(Hash)
  fail(ArgumentError, "defined_choices cannot be empty") if defined_choices.empty?
  fail(ArgumentError, "defined_choices must have Symbol keys") unless defined_choices.keys.all? { |k| k.kind_of?(Symbol) }

  fail(ArgumentError, "at least one choice must be defined") if defined_choices.empty?

  missing = @supported_modes - (defined_choices.keys + @alias_map.keys)
  fail(ArgumentError, "missing one or more required choice definition #{missing}") if missing.any?

  unknown = defined_choices.keys - @supported_modes
  fail(ArgumentError, "unknown choices #{unknown}; valid choices are #{@supported_modes}") if unknown.any?

  # resolve aliases of missing choices
  effective_mode = @active_mode
  @alias_map.size.times do # theoretical upper limit of alias chain
    break if defined_choices.include?(effective_mode)
    effective_mode = @alias_map.fetch(effective_mode)
  end

  defined_choices.fetch(effective_mode)
end