Class: RegexGenerator::Generator

Inherits:
Object
  • Object
show all
Defined in:
lib/regex_generator/generator.rb

Instance Method Summary collapse

Constructor Details

#initialize(target, text, options = {}) ⇒ Generator

Returns a new instance of Generator.

Parameters:

  • target string or hash with named targets

  • source text

  • (defaults to: {})

    options to generate regex with

Options Hash (options):

  • :exact_target (true, false)

    to generate regex with exact target value

  • :self_recognition (String, Array)

    to recognize chars as itself



11
12
13
14
15
16
# File 'lib/regex_generator/generator.rb', line 11

def initialize(target, text, options = {})
  @text = text
  @target = RegexGenerator::Target.new(target)
  @target_str = @target.to_s
  @options = options
end

Instance Method Details

#generateRegexp

Returns:

Raises:

  • if target text was not found in the text



20
21
22
23
24
25
26
27
28
# File 'lib/regex_generator/generator.rb', line 20

def generate
  raise RegexGenerator::TargetNotFoundError unless @target.present?(@text)

  string_regex_chars = recognize_text(cut_nearest_text, options)
  string_patterns_array = slice_to_identicals(string_regex_chars)
  string_regex_str = join_patterns(string_patterns_array)

  Regexp.new string_regex_str
end