Class: HttpRouter::Variable

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

Direct Known Subclasses

Glob

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(base, name, matches_with = nil, additional_matchers = nil) ⇒ Variable

Returns a new instance of Variable.



5
6
7
8
9
10
# File 'lib/http_router/variable.rb', line 5

def initialize(base, name, matches_with = nil, additional_matchers = nil)
  @router = base
  @name = name
  @matches_with = matches_with
  @additional_matchers = additional_matchers
end

Instance Attribute Details

#matches_withObject (readonly)

Returns the value of attribute matches_with.



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

def matches_with
  @matches_with
end

#nameObject (readonly)

Returns the value of attribute name.



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

def name
  @name
end

Instance Method Details

#===(part) ⇒ Object



26
27
28
# File 'lib/http_router/variable.rb', line 26

def ===(part)
  @matches_with.nil?
end

#additional_matchers(env, test) ⇒ Object



22
23
24
# File 'lib/http_router/variable.rb', line 22

def additional_matchers(env, test)
  @additional_matchers.nil? || @additional_matchers.all?{|m| m.call(env, test)}
end

#matches(env, parts, whole_path) ⇒ Object



12
13
14
15
16
17
18
19
20
# File 'lib/http_router/variable.rb', line 12

def matches(env, parts, whole_path)
  if @matches_with.nil?
    additional_matchers(env, parts.first) ? parts.first : nil
  elsif @matches_with and match = @matches_with.match(whole_path) and additional_matchers(env, parts.first)
    whole_path.slice!(0, match[0].size)
    parts.replace(router.split(whole_path))
    match[0]
  end
end