Class: RegexGenerator::Target
- Inherits:
-
Object
- Object
- RegexGenerator::Target
- Defined in:
- lib/regex_generator/target.rb
Instance Method Summary collapse
-
#escape(keys: false) ⇒ String, ...
Escapes values.
-
#initialize(target) ⇒ Target
constructor
A new instance of Target.
-
#kind_of?(type) ⇒ true, false
Checks type of the target’s string representation.
- #present?(text) ⇒ true, false
-
#to_s ⇒ String, Hash
Converts target to string (or values to strings if target is a Hash).
Constructor Details
#initialize(target) ⇒ Target
Returns a new instance of Target.
5 6 7 |
# File 'lib/regex_generator/target.rb', line 5 def initialize(target) @target = target end |
Instance Method Details
#escape(keys: false) ⇒ String, ...
Escapes values
40 41 42 43 44 45 46 47 |
# File 'lib/regex_generator/target.rb', line 40 def escape(keys: false) return Regexp.escape to_s if kind_of? String return to_s.values.map { |v| Regexp.escape v } unless keys to_s.each_with_object({}) do |(key, value), result| result[key] = Regexp.escape value end end |
#kind_of?(type) ⇒ true, false
Checks type of the target’s string representation
32 33 34 |
# File 'lib/regex_generator/target.rb', line 32 def kind_of?(type) to_s.kind_of? type end |
#present?(text) ⇒ true, false
11 12 13 14 15 |
# File 'lib/regex_generator/target.rb', line 11 def present?(text) return to_s.values.all? { |t| text[t] } if kind_of? Hash !text[to_s].nil? end |
#to_s ⇒ String, Hash
Converts target to string (or values to strings if target is a Hash)
20 21 22 23 24 25 26 |
# File 'lib/regex_generator/target.rb', line 20 def to_s return @target.to_s unless @target.kind_of? Hash @target.each_with_object({}) do |(key, value), result| result[key] = value.to_s end end |