Class: Dux::Enum::AliasMap Private

Inherits:
Object
  • Object
show all
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

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.

Parameters:



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.

Returns:

  • (Boolean)


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.

Returns:

  • (Boolean)


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.

Parameters:

  • aliaz (String, Symbol)

Returns:



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.

Parameters:

  • value (String, Symbol)

Returns:

Raises:

  • (TypeError)


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_hHash

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:

  • (Hash)


245
246
247
248
249
# File 'lib/dux/enum.rb', line 245

def to_h
  # :nocov:
  @mapping.to_h
  # :nocov:
end