Class: Key
- Inherits:
-
Object
- Object
- Key
- Defined in:
- lib/utils/key.rb
Overview
The Key class is designed to encapsulate strings within a specific prefix and suffix, allowing for easy identification and manipulation of placeholders within messages. This can be particularly useful in templating systems where placeholders need to be dynamically replaced with actual content.
Instance Attribute Summary collapse
-
#value ⇒ Object
readonly
Returns the value of attribute value.
Class Method Summary collapse
-
.find_keys_in(value) ⇒ Array<Key>
Finds and returns all Key instances within the given string.
-
.prefix ⇒ String
Returns the prefix used to identify the start of a Key in a string.
-
.suffix ⇒ String
Returns the suffix used to identify the end of a Key in a string.
Instance Method Summary collapse
-
#==(other) ⇒ Boolean
Checks equality of two Key objects based on their value.
-
#initialize(value) ⇒ Key
constructor
Initializes a new Key with the given value.
-
#to_regexp ⇒ Regexp
Returns the escaped Regexp representation of the Key.to_s return.
-
#to_s ⇒ String
Returns the string representation of the Key, including its prefix and suffix.
Constructor Details
#initialize(value) ⇒ Key
Initializes a new Key with the given value.
23 24 25 |
# File 'lib/utils/key.rb', line 23 def initialize(value) @value = value.to_s end |
Instance Attribute Details
#value ⇒ Object (readonly)
Returns the value of attribute value.
18 19 20 |
# File 'lib/utils/key.rb', line 18 def value @value end |
Class Method Details
.find_keys_in(value) ⇒ Array<Key>
Finds and returns all Key instances within the given string.
53 54 55 56 57 58 59 |
# File 'lib/utils/key.rb', line 53 def self.find_keys_in(value) ep = Regexp.escape(prefix) es = Regexp.escape(suffix) value.to_s.scan(/#{ep}([^#{ep}#{es}]+)#{es}/).map do |key| Key.new(key.first) end end |
.prefix ⇒ String
Returns the prefix used to identify the start of a Key in a string.
64 65 66 |
# File 'lib/utils/key.rb', line 64 def self.prefix '<||' end |
.suffix ⇒ String
Returns the suffix used to identify the end of a Key in a string.
71 72 73 |
# File 'lib/utils/key.rb', line 71 def self.suffix '||>' end |
Instance Method Details
#==(other) ⇒ Boolean
Checks equality of two Key objects based on their value.
31 32 33 |
# File 'lib/utils/key.rb', line 31 def ==(other) self.class == other.class && @value == other.value end |
#to_regexp ⇒ Regexp
Returns the escaped Regexp representation of the Key.to_s return.
45 46 47 |
# File 'lib/utils/key.rb', line 45 def to_regexp /#{Regexp.escape(to_s)}/ end |