Class: Dry::System::Identifier

Inherits:
Object
  • Object
show all
Defined in:
lib/dry/system/identifier.rb

Overview

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.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(key) ⇒ Identifier

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 Identifier.



22
23
24
# File 'lib/dry/system/identifier.rb', line 22

def initialize(key)
  @key = key.to_s
end

Instance Attribute Details

#keyString (readonly) Also known as: to_s

Returns the identifier’s string key.

Returns:

  • (String)

    the identifier’s string key



19
20
21
# File 'lib/dry/system/identifier.rb', line 19

def key
  @key
end

Instance Method Details

#end_with?(trailing_segments) ⇒ Boolean

Returns true if the given trailing segments string is the end part of the #key.

Also returns true if nil or an empty string is given.

Examples:

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

identifier.end_with?("create") # => true
identifier.end_with?("operations.create") # => true
identifier.end_with?("ate") # => false, not a whole segment
identifier.end_with?("nup") # => false, not in key at all

Parameters:

  • trailing_segments (String)

    the one or more trailing key segments to check

Returns:

  • (Boolean)


82
83
84
85
86
# File 'lib/dry/system/identifier.rb', line 82

def end_with?(trailing_segments)
  trailing_segments.to_s.empty? ||
    key.end_with?("#{KEY_SEPARATOR}#{trailing_segments}") ||
    key.eql?(trailing_segments)
end

#include?(segments) ⇒ Boolean

Returns true if the given segments string matches whole segments within the #key.

Examples:

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

identifier.include?("operations") # => true
identifier.include?("articles.operations") # => true
identifier.include?("operations.create") # => true

identifier.include?("article") # => false, not a whole segment
identifier.include?("update") # => false, not in key at all

Parameters:

  • segments (String)

    the one of more key segments to check

Returns:

  • (Boolean)


103
104
105
106
107
108
109
110
111
112
113
114
# File 'lib/dry/system/identifier.rb', line 103

def include?(segments)
  return false if segments.to_s.empty?

  sep_re = Regexp.escape(KEY_SEPARATOR)
  key.match?(
    /
      (\A|#{sep_re})
      #{Regexp.escape(segments)}
      (\Z|#{sep_re})
    /x
  )
end

#key_with_separator(separator) ⇒ String

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 the key with its segments separated by the given separator

Examples:

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

Returns:

  • (String)

    the key using the separator



124
125
126
# File 'lib/dry/system/identifier.rb', line 124

def key_with_separator(separator)
  segments.join(separator)
end

#namespaced(from:, to:) ⇒ Dry::System::Identifier

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 copy of the identifier with the key’s leading namespace(s) replaced

Examples:

Changing a namespace

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

Removing a namespace

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

Adding a namespace

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

Parameters:

  • from (String, nil)

    the leading namespace(s) to replace

  • to (String, nil)

    the replacement for the leading namespace

Returns:

See Also:



149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
# File 'lib/dry/system/identifier.rb', line 149

def namespaced(from:, to:)
  return self if from == to

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

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

  return self if new_key == key

  self.class.new(new_key)
end

#root_keySymbol

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

Examples:

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

Returns:

  • (Symbol)

    the root key



42
43
44
# File 'lib/dry/system/identifier.rb', line 42

def root_key
  segments.first.to_sym
end

#start_with?(leading_segments) ⇒ Boolean

Returns true if the given leading segments string is a leading part of the #key.

Also returns true if nil or an empty string is given.

Examples:

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

Parameters:

  • leading_segments (String)

    the one or more leading segments to check

Returns:

  • (Boolean)


61
62
63
64
65
# File 'lib/dry/system/identifier.rb', line 61

def start_with?(leading_segments)
  leading_segments.to_s.empty? ||
    key.start_with?("#{leading_segments}#{KEY_SEPARATOR}") ||
    key.eql?(leading_segments)
end