Class: Regexify

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

Overview

Where the regex magic happens

Defined Under Namespace

Classes: Error

Constant Summary collapse

PATTERNS =

a small list of popular regex tokens that are available with regexify

{
  number: '0-9',
  uppercase: 'A-Z',
  lowercase: 'a-z',
  letter: 'a-zA-Z',
  alphanumeric: 'a-zA-Z0-9',
  anything: '.',
  whitespace: '\\s',
  tab: '\\t',
  space: ' '
}
ESCAPED_CHARS =

chars that needs to be escaped in a regex

%w(* . ? ^ + $ | ( ) [ ] { } \\)

Instance Method Summary collapse

Constructor Details

#initializeRegexify

default constructor



24
25
26
27
# File 'lib/regexify.rb', line 24

def initialize
  @str = ""
  @complete = false
end

Instance Method Details

#begin_with(*args, exactly: nil, range: nil) ⇒ Regexify

Defines the beginning of the regex and adds ‘^`

Parameters:

  • args

    symbols and strings (supporting single and multiple characters)

  • exactly (defaults to: nil)

    specific number of repetition

  • range (defaults to: nil)

    range of repetition

Returns:

Raises:



34
35
36
37
38
# File 'lib/regexify.rb', line 34

def begin_with(*args, exactly: nil, range: nil)
  raise Regexify::Error.new('#begin_with? called multiple times') unless @str.empty?
  @str += "^#{parse(args, exactly: exactly, range: range)}"
  self
end

#end_with(*args, exactly: nil, range: nil) ⇒ Regexify

Defines the ending of the regex and adds ‘$`

Parameters:

  • args

    symbols and strings (supporting single and multiple characters)

  • exactly (defaults to: nil)

    specific number of repetition

  • range (defaults to: nil)

    range of repetition

Returns:

Raises:



45
46
47
48
49
50
# File 'lib/regexify.rb', line 45

def end_with(*args, exactly: nil, range: nil)
  raise Regexify::Error.new('#end_with? called multiple times') if @complete
  @str += "#{parse(args, exactly: exactly, range: range)}$"
  @complete = true
  self
end

#not(*args, exactly: nil, range: nil) ⇒ Regexify

Adds a new part to the regex that is negated using ‘^`

Parameters:

  • args

    symbols and strings (supporting single and multiple characters)

  • exactly (defaults to: nil)

    specific number of repetition

  • range (defaults to: nil)

    range of repetition

Returns:



67
68
69
70
# File 'lib/regexify.rb', line 67

def not(*args, exactly: nil, range: nil)
  @str += parse(args, exactly: exactly, range: range).insert(1, "^")
  self
end

#regexObject

Converts Regexify object to Regexp



73
74
75
# File 'lib/regexify.rb', line 73

def regex
  Regexp.new(@str)
end

#then(*args, exactly: nil, range: nil) ⇒ Regexify

Adds a new part to the regex

Parameters:

  • args

    symbols and strings (supporting single and multiple characters)

  • exactly (defaults to: nil)

    specific number of repetition

  • range (defaults to: nil)

    range of repetition

Returns:



57
58
59
60
# File 'lib/regexify.rb', line 57

def then(*args, exactly: nil, range: nil)
  @str += parse(args, exactly: exactly, range: range)
  self
end