Class: Dry::System::Identifier
- Inherits:
-
Object
- Object
- Dry::System::Identifier
- 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
-
#key ⇒ String
(also: #to_s)
readonly
The identifier’s string key.
Instance Method Summary collapse
-
#end_with?(trailing_segments) ⇒ Boolean
Returns true if the given trailing segments string is the end part of the #key.
-
#include?(segments) ⇒ Boolean
Returns true if the given segments string matches whole segments within the #key.
-
#initialize(key) ⇒ Identifier
constructor
private
A new instance of Identifier.
-
#key_with_separator(separator) ⇒ String
private
Returns the key with its segments separated by the given separator.
-
#namespaced(from:, to:) ⇒ Dry::System::Identifier
private
Returns a copy of the identifier with the key’s leading namespace(s) replaced.
-
#root_key ⇒ Symbol
Returns the root namespace segment of the identifier string, as a symbol.
-
#start_with?(leading_segments) ⇒ Boolean
Returns true if the given leading segments string is a leading part of the #key.
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
#key ⇒ String (readonly) Also known as: to_s
Returns 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.
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.
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
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
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_key ⇒ Symbol
Returns the root namespace segment of the identifier string, as a symbol
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.
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 |