Class: TheDude::Expression

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

Constant Summary collapse

PLACEHOLDER_REGEXP =
/\s\:(\S+)/

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(expr) ⇒ TheDude::Expression

Creates a new Expression instance

Parameters:

  • The (String | Regexp)

    expression



13
14
15
16
# File 'lib/the_dude/expression.rb', line 13

def initialize expr
  @expression = expr
  #to_regexp
end

Instance Attribute Details

#expressionObject (readonly)

String | Regexp

The expression



6
7
8
# File 'lib/the_dude/expression.rb', line 6

def expression
  @expression
end

Instance Method Details

#to_regexpRegexp

Converts the expression to a regex. If the expression is a string, it is converted to a regex that exactly matches the string. If the expression contains variable placeholders, they are substituted for the associated regexs

Returns:

  • (Regexp)


24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/the_dude/expression.rb', line 24

def to_regexp
  return @regexp unless @regexp.nil?

  # If we have a string, escape it turn it into a regex and send it back
  @regexp = /^#{Regexp.quote @expression}$/ and return @regexp if @expression.kind_of? String
  @regexp = @expression

  substitute_all_variables
  check_for_undefined_variables

  return @regexp
end