Class: Key

Inherits:
Object
  • Object
show all
Defined in:
lib/utils/key.rb,
lib/utils/key.rb

Overview

Static Methods

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.



56
57
58
59
60
61
62
# File 'lib/utils/key.rb', line 56

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.gsub(/(#{ep})|(#{es})/, ''))
  end
end

.prefixString

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

Returns:

  • (String)

    the prefix.



67
68
69
# File 'lib/utils/key.rb', line 67

def self.prefix
  '<||'
end

.suffixString

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

Returns:

  • (String)

    the suffix.



74
75
76
# File 'lib/utils/key.rb', line 74

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