Class: HttpRouter::OptionalCompiler

Inherits:
Object
  • Object
show all
Defined in:
lib/http_router/optional_compiler.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(path) ⇒ OptionalCompiler

Returns a new instance of OptionalCompiler.



4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
# File 'lib/http_router/optional_compiler.rb', line 4

def initialize(path)
  @start_index = 0
  @end_index = 1
  @paths = [""]
  @chars = path.split('')
  while !@chars.empty?
    case @chars.first
      when '('  then @chars.shift and double_paths
      when ')'  then @chars.shift and half_paths
      when '\\' then @chars.shift and add_to_current_set(@chars.shift)
      else           add_to_current_set(@chars.shift)
    end
  end
  @paths
end

Instance Attribute Details

#pathsObject (readonly)

Returns the value of attribute paths.



3
4
5
# File 'lib/http_router/optional_compiler.rb', line 3

def paths
  @paths
end

Instance Method Details

#add_to_current_set(c) ⇒ Object



20
21
22
# File 'lib/http_router/optional_compiler.rb', line 20

def add_to_current_set(c)
  (@start_index...@end_index).each { |path_index| @paths[path_index] << c }
end

#double_pathsObject

over current working set, double @paths



25
26
27
28
29
# File 'lib/http_router/optional_compiler.rb', line 25

def double_paths 
  (@start_index...@end_index).each { |path_index| @paths << @paths[path_index].dup }
  @start_index = @end_index
  @end_index = @paths.size
end

#half_pathsObject



31
32
33
# File 'lib/http_router/optional_compiler.rb', line 31

def half_paths
  @start_index -= @end_index - @start_index
end