Class: Key

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

Examples:

Creating a new Key and converting it to a string

key = Key.new("username")
key.to_s # => "<||username||>"

Finding keys within a string

keys = Key.find_keys_in("Hello, <||username||>! Your code is <||code||>.")
keys.map(&:to_s) # => ["<||username||>", "<||code||>"]

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(value) ⇒ Key

Initializes a new Key with the given value.

Parameters:

  • value (#to_s)

    the value to be encapsulated by the Key.



23
24
25
# File 'lib/utils/key.rb', line 23

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

Instance Attribute Details

#valueObject (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.

Parameters:

  • value (#to_s)

    the string to search for keys.

Returns:

  • (Array<Key>)

    an array of Key instances found 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

.prefixString

Returns the prefix used to identify the start of a Key in a string.

Returns:

  • (String)

    the prefix.



64
65
66
# File 'lib/utils/key.rb', line 64

def self.prefix
  '<||'
end

.suffixString

Returns the suffix used to identify the end of a Key in a string.

Returns:

  • (String)

    the suffix.



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.

Parameters:

  • other (Key)

    the other Key object to compare with.

Returns:

  • (Boolean)

    true if both Keys have the same value, false otherwise.



31
32
33
# File 'lib/utils/key.rb', line 31

def ==(other)
  self.class == other.class && @value == other.value
end

#to_regexpRegexp

Returns the escaped Regexp representation of the Key.to_s return.

Returns:

  • (Regexp)

    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

#to_sString

Returns the string representation of the Key, including its prefix and suffix.

Returns:

  • (String)

    the string representation of the Key.



38
39
40
# File 'lib/utils/key.rb', line 38

def to_s
  "#{Key.prefix}#{@value}#{Key.suffix}"
end