Class: Key
- Inherits:
-
Object
- Object
- Key
- Defined in:
- lib/utils/key.rb,
lib/utils/key.rb
Overview
Static Methods
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.
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 |
.prefix ⇒ String
Returns the prefix used to identify the start of a Key in a string.
67 68 69 |
# File 'lib/utils/key.rb', line 67 def self.prefix '<||' end |
.suffix ⇒ String
Returns the suffix used to identify the end of a Key in a string.
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.
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 |