Class: Journey::Route

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, app, path, constraints, defaults = {}) ⇒ Route

path is a path constraint. constraints is a hash of constraints to be applied to this route.



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/journey/route.rb', line 13

def initialize name, app, path, constraints, defaults = {}
  constraints  = constraints.dup
  @name        = name
  @app         = app
  @path        = path
  @verb        = constraints[:request_method] || //
  @ip          = constraints.delete(:ip) || //

  @constraints = constraints
  @constraints.keep_if { |_,v| Regexp === v || String === v }
  @defaults    = defaults
  @required_defaults = nil
  @required_parts    = nil
  @parts             = nil
  @decorated_ast     = nil
  @precedence        = 0
end

Instance Attribute Details

#appObject (readonly)

Returns the value of attribute app.



3
4
5
# File 'lib/journey/route.rb', line 3

def app
  @app
end

#constraintsObject (readonly) Also known as: conditions

Returns the value of attribute constraints.



5
6
7
# File 'lib/journey/route.rb', line 5

def constraints
  @constraints
end

#defaultsObject (readonly)

Returns the value of attribute defaults.



3
4
5
# File 'lib/journey/route.rb', line 3

def defaults
  @defaults
end

#ipObject (readonly)

Returns the value of attribute ip.



3
4
5
# File 'lib/journey/route.rb', line 3

def ip
  @ip
end

#nameObject (readonly)

Returns the value of attribute name.



3
4
5
# File 'lib/journey/route.rb', line 3

def name
  @name
end

#pathObject (readonly)

Returns the value of attribute path.



3
4
5
# File 'lib/journey/route.rb', line 3

def path
  @path
end

#precedenceObject

Returns the value of attribute precedence.



8
9
10
# File 'lib/journey/route.rb', line 8

def precedence
  @precedence
end

#verbObject (readonly)

Returns the value of attribute verb.



3
4
5
# File 'lib/journey/route.rb', line 3

def verb
  @verb
end

Instance Method Details

#astObject



31
32
33
34
35
36
37
# File 'lib/journey/route.rb', line 31

def ast
  return @decorated_ast if @decorated_ast

  @decorated_ast = path.ast
  @decorated_ast.grep(Nodes::Terminal).each { |n| n.memo = self }
  @decorated_ast
end

#format(path_options) ⇒ Object



69
70
71
72
73
74
75
76
77
78
# File 'lib/journey/route.rb', line 69

def format path_options
  (defaults.keys - required_parts).each do |key|
    path_options.delete key if defaults[key].to_s == path_options[key].to_s
  end

  formatter      = Visitors::Formatter.new(path_options)

  formatted_path = formatter.accept(path.spec)
  formatted_path.gsub(/\/\x00/, '')
end

#optional_partsObject



80
81
82
# File 'lib/journey/route.rb', line 80

def optional_parts
  path.optional_names.map { |n| n.to_sym }
end

#partsObject Also known as: segment_keys



64
65
66
# File 'lib/journey/route.rb', line 64

def parts
  @parts ||= segments.map { |n| n.to_sym }
end

#required_defaultsObject



88
89
90
91
92
93
# File 'lib/journey/route.rb', line 88

def required_defaults
  @required_defaults ||= begin
    matches = parts
    @defaults.dup.delete_if { |k,_| matches.include? k }
  end
end

#required_keysObject



50
51
52
# File 'lib/journey/route.rb', line 50

def required_keys
  path.required_names.map { |x| x.to_sym } + required_defaults.keys
end

#required_partsObject



84
85
86
# File 'lib/journey/route.rb', line 84

def required_parts
  @required_parts ||= path.required_names.map { |n| n.to_sym }
end

#requirementsObject

:nodoc:



39
40
41
42
43
44
# File 'lib/journey/route.rb', line 39

def requirements # :nodoc:
  # needed for rails `rake routes`
  path.requirements.merge(@defaults).delete_if { |_,v|
    /.+?/ == v
  }
end

#score(constraints) ⇒ Object



54
55
56
57
58
59
60
61
62
# File 'lib/journey/route.rb', line 54

def score constraints
  required_keys = path.required_names
  supplied_keys = constraints.map { |k,v| v && k.to_s }.compact

  return -1 unless (required_keys - supplied_keys).empty?

  score = (supplied_keys & path.names).length
  score + (required_defaults.length * 2)
end

#segmentsObject



46
47
48
# File 'lib/journey/route.rb', line 46

def segments
  @path.names
end