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
# 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
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



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

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: =~



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

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

#namesObject



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

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

#optional_namesObject



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

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

#required_namesObject



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

def required_names
  @required_names ||= names - optional_names
end

#sourceObject



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

def source
  to_regexp.source
end