Class: HttpRouter::Node::Regex

Inherits:
HttpRouter::Node show all
Defined in:
lib/http_router/node/regex.rb

Direct Known Subclasses

SpanningRegex

Instance Attribute Summary collapse

Attributes inherited from HttpRouter::Node

#router

Instance Method Summary collapse

Methods inherited from HttpRouter::Node

#add_destination, #add_free_match, #add_glob, #add_glob_regexp, #add_host, #add_lookup, #add_match, #add_request_method, #add_scheme, #add_spanning_match, #add_user_agent, #add_variable, #depth, #inspect, #inspect_label, #inspect_matchers_body

Constructor Details

#initialize(router, parent, matcher, capturing_indicies, splitting_indicies = nil) ⇒ Regex

Returns a new instance of Regex.



7
8
9
10
11
12
13
14
# File 'lib/http_router/node/regex.rb', line 7

def initialize(router, parent, matcher, capturing_indicies, splitting_indicies = nil)
  @matcher, @capturing_indicies, @splitting_indicies = matcher, capturing_indicies, splitting_indicies
  @ordered_indicies = []
  @ordered_indicies.concat(capturing_indicies.map{|i| [i, :capture]}) if capturing_indicies
  @ordered_indicies.concat(splitting_indicies.map{|i| [i, :split]}) if splitting_indicies
  @ordered_indicies.sort!
  super(router, parent)
end

Instance Attribute Details

#capturing_indiciesObject (readonly)

Returns the value of attribute capturing_indicies.



5
6
7
# File 'lib/http_router/node/regex.rb', line 5

def capturing_indicies
  @capturing_indicies
end

#matcherObject (readonly)

Returns the value of attribute matcher.



5
6
7
# File 'lib/http_router/node/regex.rb', line 5

def matcher
  @matcher
end

#ordered_indiciesObject (readonly)

Returns the value of attribute ordered_indicies.



5
6
7
# File 'lib/http_router/node/regex.rb', line 5

def ordered_indicies
  @ordered_indicies
end

#splitting_indiciesObject (readonly)

Returns the value of attribute splitting_indicies.



5
6
7
# File 'lib/http_router/node/regex.rb', line 5

def splitting_indicies
  @splitting_indicies
end

Instance Method Details

#node_to_codeObject



4
# File 'lib/http_router/node/regex.rb', line 4

alias_method :node_to_code, :to_code

#param_capturing_codeObject



30
31
32
33
34
35
36
37
# File 'lib/http_router/node/regex.rb', line 30

def param_capturing_code
  @ordered_indicies.map{|(i, type)|
    case type
    when :capture then "request.params << match[#{i}]\n"
    when :split   then "request.params << match[#{i}].split(/\\//)\n"
    end
  }.join("")
end

#to_codeObject



20
21
22
23
24
25
26
27
28
# File 'lib/http_router/node/regex.rb', line 20

def to_code
  params_size = @splitting_indicies.size + @capturing_indicies.size
  "if match = #{@matcher.inspect}.match(request.path.first) and match.begin(0).zero?
    part = request.path.shift\n" << param_capturing_code <<
    "#{super}
    request.path.unshift part
    #{params_size == 1 ? "request.params.pop" : "request.params.slice!(#{-params_size}, #{params_size})"}
  end"
end

#usable?(other) ⇒ Boolean

Returns:

  • (Boolean)


16
17
18
# File 'lib/http_router/node/regex.rb', line 16

def usable?(other)
  other.class == self.class && other.matcher == matcher && other.splitting_indicies == splitting_indicies && other.capturing_indicies == capturing_indicies
end