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) ⇒ State

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.

Returns a new instance of State.

Parameters:

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


83
84
85
86
# File 'lib/logstash/plugin_mixins/ecs_compatibility_support/selector.rb', line 83

def initialize(supported_modes, active_mode)
  @supported_modes = supported_modes
  @active_mode = active_mode
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.



88
89
90
# File 'lib/logstash/plugin_mixins/ecs_compatibility_support/selector.rb', line 88

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.



89
90
91
# File 'lib/logstash/plugin_mixins/ecs_compatibility_support/selector.rb', line 89

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)


98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
# File 'lib/logstash/plugin_mixins/ecs_compatibility_support/selector.rb', line 98

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
  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?

  defined_choices.fetch(@active_mode)
end