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.



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

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
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

#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



28
29
30
31
32
33
34
# File 'lib/journey/route.rb', line 28

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



66
67
68
69
70
71
72
73
74
75
# File 'lib/journey/route.rb', line 66

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



77
78
79
# File 'lib/journey/route.rb', line 77

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

#partsObject Also known as: segment_keys



61
62
63
# File 'lib/journey/route.rb', line 61

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

#required_defaultsObject



85
86
87
88
89
90
# File 'lib/journey/route.rb', line 85

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

#required_keysObject



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

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

#required_partsObject



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

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

#requirementsObject

:nodoc:



36
37
38
39
40
41
# File 'lib/journey/route.rb', line 36

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

#score(constraints) ⇒ Object



51
52
53
54
55
56
57
58
59
# File 'lib/journey/route.rb', line 51

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



43
44
45
# File 'lib/journey/route.rb', line 43

def segments
  @path.names
end