Class: Dux::Enum::AliasMap Private
- Inherits:
-
Object
- Object
- Dux::Enum::AliasMap
- Defined in:
- lib/dux/enum.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.
An indifferent, read-only hash-like object for mapping aliases.
Instance Method Summary collapse
-
#acceptable?(value) ⇒ Boolean
private
private
Check if the value is acceptable for mapping.
- #alias?(aliaz) ⇒ Boolean (also: #key?) private
- #fetch(aliaz) ⇒ Dux::IndifferentString (also: #[]) private
- #indifferentize(value) ⇒ Dux::IndifferentString private private
-
#initialize(*targets, **aliases) ⇒ AliasMap
constructor
private
A new instance of AliasMap.
- #to_h ⇒ Hash private
Constructor Details
#initialize(*targets, **aliases) ⇒ AliasMap
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 AliasMap.
209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 |
# File 'lib/dux/enum.rb', line 209 def initialize(*targets, **aliases) @mapping = aliases.each_with_object({}) do |(aliaz, target), mapping| aliaz = indifferentize(aliaz) target = indifferentize(target) if targets.include? aliaz raise MemberAsAliasError, "alias `#{aliaz}` is already an enum member" end unless targets.include? target raise InvalidAliasTargetError, "alias target `#{target}` is not an enum member" end mapping[aliaz] = target end.freeze @aliases = @mapping.keys.freeze freeze end |
Instance Method Details
#acceptable?(value) ⇒ Boolean (private)
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.
Check if the value is acceptable for mapping.
254 255 256 |
# File 'lib/dux/enum.rb', line 254 def acceptable?(value) value.kind_of?(String) || value.kind_of?(Symbol) || value.kind_of?(Dux::IndifferentString) end |
#alias?(aliaz) ⇒ Boolean Also known as: key?
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.
230 231 232 |
# File 'lib/dux/enum.rb', line 230 def alias?(aliaz) @aliases.include? aliaz end |
#fetch(aliaz) ⇒ Dux::IndifferentString 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.
238 239 240 |
# File 'lib/dux/enum.rb', line 238 def fetch(aliaz) @mapping[indifferentize(aliaz)] end |
#indifferentize(value) ⇒ Dux::IndifferentString (private)
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.
260 261 262 263 264 |
# File 'lib/dux/enum.rb', line 260 def indifferentize(value) raise TypeError, "invalid aliaz or target: #{value.inspect}" unless acceptable?(value) Dux::IndifferentString.new value end |
#to_h ⇒ Hash
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.
245 246 247 248 249 |
# File 'lib/dux/enum.rb', line 245 def to_h # :nocov: @mapping.to_h # :nocov: end |