Class: LogStash::PluginMixins::ECSCompatibilitySupport::Selector::State Private
- Inherits:
-
Object
- Object
- LogStash::PluginMixins::ECSCompatibilitySupport::Selector::State
- 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
- #active_mode ⇒ Object readonly private
- #supported_modes ⇒ Object readonly private
Instance Method Summary collapse
-
#initialize(supported_modes, active_mode, alias_map = EMPTY_HASH) ⇒ State
constructor
A new instance of State.
-
#value_from(defined_choices) ⇒ Object
(also: #[])
private
With the active mode, select one of the provided options.
Constructor Details
#initialize(supported_modes, active_mode, alias_map = EMPTY_HASH) ⇒ State
Returns a new instance of State.
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_mode ⇒ Object (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_modes ⇒ Object (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.
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 |