class Journey::Path::Pattern - journey-1.0.4 Documentation

# class Journey::Path::Pattern

### Attributes

anchored[R]
requirements[R]
spec[R]

### Public Class Methods

new(strexp) click to toggle source

    # 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

### Public Instance Methods

=~(other)
Alias for: match
ast() click to toggle source

    # 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) click to toggle source

    # File lib/journey/path/pattern.rb, line 166
    def match other
      return unless match = to_regexp.match(other)
      MatchData.new names, offsets, match
    end

Also aliased as: =~
names() click to toggle source

    # File lib/journey/path/pattern.rb, line 46
    def names
      @names ||= spec.grep(Nodes::Symbol).map { |n| n.name }
    end

optional_names() click to toggle source

    # 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_names() click to toggle source

    # File lib/journey/path/pattern.rb, line 50
    def required_names
      @required_names ||= names - optional_names
    end

source() click to toggle source

    # File lib/journey/path/pattern.rb, line 172
    def source
      to_regexp.source
    end

to_regexp() click to toggle source

    # File lib/journey/path/pattern.rb, line 176
    def to_regexp
      @re ||= regexp_visitor.new(@separators, @requirements).accept spec
    end