Class: Reginald::Expression

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

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Collection

#==, #freeze, #include?, #match, #to_regexp

Constructor Details

#initialize(*args) ⇒ Expression

Returns a new instance of Expression.



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/rack/mount/vendor/reginald/reginald/expression.rb', line 17

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.



4
5
6
# File 'lib/rack/mount/vendor/reginald/reginald/expression.rb', line 4

def extended
  @extended
end

#ignorecaseObject

Returns the value of attribute ignorecase.



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

def ignorecase
  @ignorecase
end

#multilineObject

Returns the value of attribute multiline.



4
5
6
# File 'lib/rack/mount/vendor/reginald/reginald/expression.rb', line 4

def multiline
  @multiline
end

Class Method Details

.reduce(expression_or_atom, atom = nil) ⇒ Object

:nodoc:



6
7
8
9
10
11
12
13
14
15
# File 'lib/rack/mount/vendor/reginald/reginald/expression.rb', line 6

def self.reduce(expression_or_atom, atom = nil) #:nodoc:
  if expression_or_atom.is_a?(Expression)
    expression_or_atom << atom if atom
    new(*expression_or_atom)
  elsif atom.nil?
    new(expression_or_atom)
  else
    new(expression_or_atom, atom)
  end
end

Instance Method Details

#anchored?Boolean

Returns:

  • (Boolean)


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

def anchored?
  anchored_to_start? && anchored_to_end?
end

#anchored_to_end?Boolean

Returns:

  • (Boolean)


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

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

#anchored_to_end_of_line?Boolean

Returns:

  • (Boolean)


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

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

#anchored_to_line?Boolean

Returns:

  • (Boolean)


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

def anchored_to_line?
  anchored_to_start_of_line? && anchored_to_end_of_line?
end

#anchored_to_start?Boolean

Returns:

  • (Boolean)


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

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

#anchored_to_start_of_line?Boolean

Returns:

  • (Boolean)


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

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

#casefold?Boolean

Returns:

  • (Boolean)


101
102
103
# File 'lib/rack/mount/vendor/reginald/reginald/expression.rb', line 101

def casefold?
  ignorecase
end

#dup(options = {}) ⇒ Object



73
74
75
76
77
78
79
# File 'lib/rack/mount/vendor/reginald/reginald/expression.rb', line 73

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)


105
106
107
108
109
110
# File 'lib/rack/mount/vendor/reginald/reginald/expression.rb', line 105

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

#flagsObject



69
70
71
# File 'lib/rack/mount/vendor/reginald/reginald/expression.rb', line 69

def flags
  options.to_i
end

#inspectObject

:nodoc:



97
98
99
# File 'lib/rack/mount/vendor/reginald/reginald/expression.rb', line 97

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)


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

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

#options?Boolean

Returns:

  • (Boolean)


65
66
67
# File 'lib/rack/mount/vendor/reginald/reginald/expression.rb', line 65

def options?
  options.any?(true)
end

#to_s(parent = false) ⇒ Object



81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
# File 'lib/rack/mount/vendor/reginald/reginald/expression.rb', line 81

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