Class: Regin::Expression

Inherits:
Collection show all
Defined in:
lib/rack/mount/vendor/regin/regin/expression.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods inherited from Collection

#==, #[], #each, #first, #include?, #last, #length, #match, #to_regexp

Constructor Details

#initialize(*args) ⇒ Expression

Returns a new instance of Expression.



5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
# File 'lib/rack/mount/vendor/regin/regin/expression.rb', line 5

def initialize(*args)
  args, options = extract_options(args)

  @multiline = @ignorecase = @extended = nil

  if args.length == 1 && args.first.instance_of?(Array)
    super(args.first)
  else
    args = args.map { |e| e.instance_of?(String) ? Character.new(e) : e }
    super(args)
  end

  self.multiline  = options[:multiline] if options.key?(:multiline)
  self.ignorecase = options[:ignorecase] if options.key?(:ignorecase)
  self.extended   = options[:extended] if options.key?(:extended)
end

Instance Attribute Details

#extendedObject

Returns the value of attribute extended.



3
4
5
# File 'lib/rack/mount/vendor/regin/regin/expression.rb', line 3

def extended
  @extended
end

#ignorecaseObject

Returns the value of attribute ignorecase.



3
4
5
# File 'lib/rack/mount/vendor/regin/regin/expression.rb', line 3

def ignorecase
  @ignorecase
end

#multilineObject

Returns the value of attribute multiline.



3
4
5
# File 'lib/rack/mount/vendor/regin/regin/expression.rb', line 3

def multiline
  @multiline
end

Instance Method Details

#+(other) ⇒ Object



61
62
63
64
65
# File 'lib/rack/mount/vendor/regin/regin/expression.rb', line 61

def +(other)
  ary = other.is_a?(self.class) ? other.internal_array : other
  ary = @array + ary + [options.to_h(true)]
  self.class.new(*ary)
end

#anchored?Boolean

Returns:

  • (Boolean)


29
30
31
# File 'lib/rack/mount/vendor/regin/regin/expression.rb', line 29

def anchored?
  anchored_to_start? && anchored_to_end?
end

#anchored_to_end?Boolean

Returns:

  • (Boolean)


37
38
39
# File 'lib/rack/mount/vendor/regin/regin/expression.rb', line 37

def anchored_to_end?
  last.is_a?(Anchor) && last == '\Z'
end

#anchored_to_end_of_line?Boolean

Returns:

  • (Boolean)


49
50
51
# File 'lib/rack/mount/vendor/regin/regin/expression.rb', line 49

def anchored_to_end_of_line?
  anchored_to_end? || (last.is_a?(Anchor) && last == '$')
end

#anchored_to_line?Boolean

Returns:

  • (Boolean)


41
42
43
# File 'lib/rack/mount/vendor/regin/regin/expression.rb', line 41

def anchored_to_line?
  anchored_to_start_of_line? && anchored_to_end_of_line?
end

#anchored_to_start?Boolean

Returns:

  • (Boolean)


33
34
35
# File 'lib/rack/mount/vendor/regin/regin/expression.rb', line 33

def anchored_to_start?
  first.is_a?(Anchor) && first == '\A'
end

#anchored_to_start_of_line?Boolean

Returns:

  • (Boolean)


45
46
47
# File 'lib/rack/mount/vendor/regin/regin/expression.rb', line 45

def anchored_to_start_of_line?
  anchored_to_start? || (first.is_a?(Anchor) && first == '^')
end

#casefold?Boolean

Returns:

  • (Boolean)


95
96
97
# File 'lib/rack/mount/vendor/regin/regin/expression.rb', line 95

def casefold?
  ignorecase
end

#dup(options = {}) ⇒ Object



67
68
69
70
71
72
73
# File 'lib/rack/mount/vendor/regin/regin/expression.rb', line 67

def dup(options = {})
  expression = super()
  expression.multiline  = options[:multiline] if options.key?(:multiline)
  expression.ignorecase = options[:ignorecase] if options.key?(:ignorecase)
  expression.extended   = options[:extended] if options.key?(:extended)
  expression
end

#eql?(other) ⇒ Boolean

:nodoc:

Returns:

  • (Boolean)


99
100
101
102
103
104
# File 'lib/rack/mount/vendor/regin/regin/expression.rb', line 99

def eql?(other) #:nodoc:
  super &&
    !!self.multiline == !!other.multiline &&
    !!self.ignorecase == !!other.ignorecase &&
    !!self.extended == !!other.extended
end

#flagsObject



57
58
59
# File 'lib/rack/mount/vendor/regin/regin/expression.rb', line 57

def flags
  options.to_i
end

#inspectObject

:nodoc:



91
92
93
# File 'lib/rack/mount/vendor/regin/regin/expression.rb', line 91

def inspect #:nodoc:
  "#<Expression #{to_s.inspect}>"
end

#literal?Boolean

Returns true if expression could be treated as a literal string.

A Expression is literal if all its elements are literal.

Returns:

  • (Boolean)


25
26
27
# File 'lib/rack/mount/vendor/regin/regin/expression.rb', line 25

def literal?
  !ignorecase && all? { |e| e.literal? }
end

#options?Boolean

Returns:

  • (Boolean)


53
54
55
# File 'lib/rack/mount/vendor/regin/regin/expression.rb', line 53

def options?
  options.any?(true)
end

#to_s(parent = false) ⇒ Object



75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
# File 'lib/rack/mount/vendor/regin/regin/expression.rb', line 75

def to_s(parent = false)
  if parent || !options?
    map { |e| e.to_s(parent) }.join
  else
    with, without = [], []
    multiline ? (with << 'm') : (without << 'm')
    ignorecase ? (with << 'i') : (without << 'i')
    extended ? (with << 'x') : (without << 'x')

    with = with.join
    without = without.any? ? "-#{without.join}" : ''

    "(?#{with}#{without}:#{map { |e| e.to_s(true) }.join})"
  end
end