Class: Groem::Route

Inherits:
Object
  • Object
show all
Defined in:
lib/groem/route.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(action, *path) ⇒ Route

Returns a new instance of Route.



21
22
23
24
# File 'lib/groem/route.rb', line 21

def initialize action, *path
  path = path.flatten
  @pattern = self.class.parse action, *path
end

Instance Attribute Details

#patternObject (readonly)

Returns the value of attribute pattern.



19
20
21
# File 'lib/groem/route.rb', line 19

def pattern
  @pattern
end

Class Method Details

.matches?(pattern, parts) ⇒ Boolean

Returns:

  • (Boolean)


10
11
12
13
14
15
# File 'lib/groem/route.rb', line 10

def matches? pattern, parts
  parts = [parts] unless Array === parts
  pattern.zip(parts).all? do |exp, act|
    exp.nil? || exp == act
  end
end

.parse(action, *path) ⇒ Object



6
7
8
# File 'lib/groem/route.rb', line 6

def parse action, *path
  [action] + Array.new(2).fill {|i| path[i] }
end

Instance Method Details

#<=>(other) ⇒ Object

sort nil parts after named parts (unless named parts begin with ~)



32
33
34
# File 'lib/groem/route.rb', line 32

def <=>(other)
  pattern.map {|it| it || '~'} <=> other.pattern.map {|it| it || '~'}
end

#matches?(*args) ⇒ Boolean

Returns:

  • (Boolean)


26
27
28
# File 'lib/groem/route.rb', line 26

def matches?(*args)
  self.class.matches?(pattern, args.flatten)
end