Class: RailsNavigation::Matcher

Inherits:
Object
  • Object
show all
Defined in:
lib/rails_navigation/matcher.rb

Instance Method Summary collapse

Constructor Details

#initialize(controller_name, action_name, params) ⇒ Matcher

Returns a new instance of Matcher.



3
4
5
6
7
# File 'lib/rails_navigation/matcher.rb', line 3

def initialize(controller_name, action_name, params)
  @controller_name = controller_name
  @action_name = action_name
  @params = params
end

Instance Method Details

#active?(str) ⇒ Boolean

Returns:

  • (Boolean)


29
30
31
# File 'lib/rails_navigation/matcher.rb', line 29

def active?(str)
  check_controller(str) && check_action(str) && check_parameters(str)
end

#check_action(str) ⇒ Object



14
15
16
17
# File 'lib/rails_navigation/matcher.rb', line 14

def check_action(str)
  result = Array(str.match(/#([a-z_\-]+)/))[1]
  result ? (result == @action_name) : true
end

#check_controller(str) ⇒ Object



9
10
11
12
# File 'lib/rails_navigation/matcher.rb', line 9

def check_controller(str)
  result = Array(str.match(/^([a-z_\-]+)/))[1]
  result ? (result == @controller_name) : true
end

#check_parameters(str) ⇒ Object



19
20
21
22
23
24
25
26
27
# File 'lib/rails_navigation/matcher.rb', line 19

def check_parameters(str)
  result = Array(str.match(/\?([!a-z0-9&_\-=]+)/))[1]
  if result
    key, value = result.split('=')
    result.include?('=') ? value == @params[key.to_sym] : result == @params[:id]
  else
    true
  end
end