Class: Path::Match

Inherits:
Array
  • Object
show all
Defined in:
lib/path/match.rb

Overview

Represents the single match of a relative path against a data set.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(*args) ⇒ Match

Returns a new instance of Match.



8
9
10
11
12
# File 'lib/path/match.rb', line 8

def initialize *args
  @matches = []
  @splat   = []
  super
end

Instance Attribute Details

#matchesObject

Returns the value of attribute matches.



6
7
8
# File 'lib/path/match.rb', line 6

def matches
  @matches
end

#splatObject

Returns the value of attribute splat.



6
7
8
# File 'lib/path/match.rb', line 6

def splat
  @splat
end

Instance Method Details

#[](selector) ⇒ Object



15
16
17
18
19
20
21
22
23
24
# File 'lib/path/match.rb', line 15

def [] selector
  path_match = super

  if self.class === path_match
    path_match.matches = @matches.dup
    path_match.splat   = @splat.map{|key, sp| [key, sp.dup]}
  end

  path_match
end

#append_match_for(str, path) ⇒ Object

:nodoc:



44
45
46
47
48
49
50
51
# File 'lib/path/match.rb', line 44

def append_match_for str, path # :nodoc:
  match = @matches[str.to_i-1]
  if match && !(String === match) && path[-1].empty?
    path[-1] = match
  else
    path[-1] << match.to_s
  end
end

#append_splat(id, key) ⇒ Object

:nodoc:



27
28
29
30
31
32
33
# File 'lib/path/match.rb', line 27

def append_splat id, key # :nodoc:
  if @splat[-1] && @splat[-1][0] == id
    @splat[-1][1] << key
  else
    @splat << [id, [key]]
  end
end

#dupObject

:nodoc:



36
37
38
39
40
41
# File 'lib/path/match.rb', line 36

def dup # :nodoc:
  path_match = super
  path_match.matches = @matches.dup
  path_match.splat   = @splat.map{|key, sp| [key, sp.dup]}
  path_match
end

#make_path(path_map, regex_opts = nil, &block) ⇒ Object

Builds a path array by replacing %n and %% values with matches and splat.

matches = Path.find_in "**/foo=bar", data
# [["path", "to", "foo"]]

matches.first.make_path "root/%%/foo"
# ["root", "path", "to", "foo"]

matches = Path.find_in "path/*/(foo)=bar", data
# [["path", "to", "foo"]]

matches.first.make_path "root/%1/%2"
# ["root", "to", "foo"]


69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
# File 'lib/path/match.rb', line 69

def make_path path_map, regex_opts=nil, &block
  tmpsplat = @splat.dup
  path     = []
  escape   = false
  replace  = false
  new_item = true
  rindex   = ""

  path_map.to_s.chars do |chr|
    case chr
    when Path::ECH
      escape = true

    when Path::DCH
      new_item = true

    when Path::RCH
      if replace
        if rindex.empty?
          unless tmpsplat.empty?
            items = tmpsplat.shift[1].dup
            if new_item
              new_item = false
            else
              path[-1] = path[-1].dup << items.shift
            end
            path.concat items
          end
          replace = false
        else
          append_match_for(rindex, path)
          rindex = ""
        end

        next
      else
        replace = true
      end
    end and next unless escape

    if replace
      if new_item && !rindex.empty? || chr.to_i.to_s != chr || escape
        append_match_for(rindex, path) unless rindex.empty?
        rindex  = ""
        replace = false
      else
        rindex << chr
      end
    end

    path      << ""  if new_item
    path.last << chr unless replace

    new_item = false
    escape   = false
  end

  append_match_for(rindex, path) unless rindex.empty?

  path
end