class Dry::System::Identifier

An identifier representing a component to be registered.

Components are eventually registered in the container using plain string identifiers, available as the `identifier` or `key` attribute here. Additional methods are provided to make it easier to evaluate or manipulate these identifiers.

@api public

Attributes

key[R]

@return [String] the identifier's string key @api public

separator[R]

@return [String] the configured namespace separator @api public

to_s[R]

@return [String] the identifier's string key @api public

Public Class Methods

new(key, separator: DEFAULT_SEPARATOR) click to toggle source

@api private

# File lib/dry/system/identifier.rb, line 27
def initialize(key, separator: DEFAULT_SEPARATOR)
  @key = key.to_s
  @separator = separator
end

Public Instance Methods

key_with_separator(separator) click to toggle source

Returns the key with its segments separated by the given separator

@example

identifier.key # => "articles.operations.create"
identifier.key_with_separator("/") # => "articles/operations/create"

@return [String] the key using the separator @api private

# File lib/dry/system/identifier.rb, line 83
def key_with_separator(separator)
  segments.join(separator)
end
namespaced(from:, to:) click to toggle source

Returns a copy of the identifier with the key's leading namespace(s) replaced

@example Changing a namespace

identifier.key # => "articles.operations.create"
identifier.namespaced(from: "articles", to: "posts").key # => "posts.commands.create"

@example Removing a namespace

identifier.key # => "articles.operations.create"
identifier.namespaced(from: "articles", to: nil).key # => "operations.create"

@example Adding a namespace

identifier.key # => "articles.operations.create"
identifier.namespaced(from: nil, to: "admin").key # => "admin.articles.operations.create"

@param from [String, nil] the leading namespace(s) to replace @param to [String, nil] the replacement for the leading namespace

@return [Dry::System::Identifier] the copy of the identifier

@see initialize @api private

# File lib/dry/system/identifier.rb, line 108
def namespaced(from:, to:)
  return self if from == to

  separated_to = "#{to}#{separator}" if to

  new_key =
    if from.nil?
      "#{separated_to}#{key}"
    else
      key.sub(
        /^#{Regexp.escape(from.to_s)}#{Regexp.escape(separator)}/,
        separated_to || EMPTY_STRING
      )
    end

  return self if new_key == key

  self.class.new(new_key, separator: separator)
end
root_key() click to toggle source

Returns the root namespace segment of the identifier string, as a symbol

@example

identifier.key # => "articles.operations.create"
identifier.root_key # => :articles

@return [Symbol] the root key @api public

# File lib/dry/system/identifier.rb, line 48
def root_key
  segments.first.to_sym
end
start_with?(leading_namespaces) click to toggle source

Returns true if the given leading namespaces are a leading part of the identifier's key

Also returns true if nil is given (technically, from nothing everything is wrought)

@example

identifier.key # => "articles.operations.create"

identifier.start_with?("articles.operations") # => true
identifier.start_with?("articles") # => true
identifier.start_with?("article") # => false
identifier.start_with?(nil) # => true

@param leading_namespaces [String] the one or more leading namespaces to check @return [Boolean] @api public

# File lib/dry/system/identifier.rb, line 69
def start_with?(leading_namespaces)
  leading_namespaces.nil? ||
    key.start_with?("#{leading_namespaces}#{separator}") ||
    key.eql?(leading_namespaces)
end

Private Instance Methods

segments() click to toggle source
# File lib/dry/system/identifier.rb, line 130
def segments
  @segments ||= key.split(separator)
end