Class: Journey::Path::Pattern

Inherits:
Object
  • Object
show all
Defined in:
lib/journey/path/pattern.rb

Defined Under Namespace

Classes: AnchoredRegexp, MatchData, RegexpOffsets, UnanchoredRegexp

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(strexp) ⇒ Pattern

Returns a new instance of Pattern.



6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/journey/path/pattern.rb', line 6

def initialize strexp
  parser = Journey::Parser.new

  @anchored = true

  case strexp
  when String
    @spec         = parser.parse strexp
    @requirements = {}
    @separators   = "/.?"
  when Router::Strexp
    @spec         = parser.parse strexp.path
    @requirements = strexp.requirements
    @separators   = strexp.separators.join
    @anchored     = strexp.anchor
  else
    raise "wtf bro: #{strexp}"
  end

  @names          = nil
  @optional_names = nil
  @required_names = nil
  @re             = nil
  @offsets        = nil
end

Instance Attribute Details

#anchoredObject (readonly)

Returns the value of attribute anchored.



4
5
6
# File 'lib/journey/path/pattern.rb', line 4

def anchored
  @anchored
end

#requirementsObject (readonly)

Returns the value of attribute requirements.



4
5
6
# File 'lib/journey/path/pattern.rb', line 4

def requirements
  @requirements
end

#specObject (readonly)

Returns the value of attribute spec.



4
5
6
# File 'lib/journey/path/pattern.rb', line 4

def spec
  @spec
end

Instance Method Details

#astObject



32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/journey/path/pattern.rb', line 32

def ast
  @spec.grep(Nodes::Symbol).each do |node|
    re = @requirements[node.to_sym]
    node.regexp = re if re
  end

  @spec.grep(Nodes::Star).each do |node|
    node = node.left
    node.regexp = @requirements[node.to_sym] || /(.+)/
  end

  @spec
end

#match(other) ⇒ Object Also known as: =~



166
167
168
169
# File 'lib/journey/path/pattern.rb', line 166

def match other
  return unless match = to_regexp.match(other)
  MatchData.new names, offsets, match
end

#namesObject



46
47
48
# File 'lib/journey/path/pattern.rb', line 46

def names
  @names ||= spec.grep(Nodes::Symbol).map { |n| n.name }
end

#optional_namesObject



54
55
56
57
58
# File 'lib/journey/path/pattern.rb', line 54

def optional_names
  @optional_names ||= spec.grep(Nodes::Group).map { |group|
    group.grep(Nodes::Symbol)
  }.flatten.map { |n| n.name }.uniq
end

#required_namesObject



50
51
52
# File 'lib/journey/path/pattern.rb', line 50

def required_names
  @required_names ||= names - optional_names
end

#sourceObject



172
173
174
# File 'lib/journey/path/pattern.rb', line 172

def source
  to_regexp.source
end

#to_regexpObject



176
177
178
# File 'lib/journey/path/pattern.rb', line 176

def to_regexp
  @re ||= regexp_visitor.new(@separators, @requirements).accept spec
end