Class: Kin::Nav::ItemMatcher

Inherits:
Object
  • Object
show all
Defined in:
lib/kin/nav.rb

Overview

Used to match a controller name and action name to an item.

This is used to find out which item should be considered active for the given controller and action.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(item, controller_name, action_name) ⇒ ItemMatcher

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Creates a new ItemMatcher instance.

Parameters:

  • item (Kin::Nav::Item)

    The item to be considered active for which the given controller and action pair.

  • controller_name (String)

    The name of the controller.

  • action_name (String)

    The name of the action, or ‘*’ if the item is active for all actions on the controller.



299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
# File 'lib/kin/nav.rb', line 299

def initialize(item, controller_name, action_name)
  # Can't use match an action name with a generic controller.
  if controller_name == '*' && action_name != '*'
    # @todo spec
    raise ArgumentError, "Can't match any controller with a specific " \
      "action: #{controller_name}/#{action_name}"
  end

  @item = item
  @controller_name = controller_name

  if match = action_name.match(/^\{(.*)\}$/)
    # The action name is a glob containing multiple actions.
    @action_name = match[1].split(',')
  else
    @action_name = [action_name]
  end
end

Instance Attribute Details

#itemObject (readonly)

Returns the item which this matcher is associated.



283
284
285
# File 'lib/kin/nav.rb', line 283

def item
  @item
end

Instance Method Details

#action?Boolean

Returns if this is matcher has an action specified.

Returns:

  • (Boolean)


336
337
338
# File 'lib/kin/nav.rb', line 336

def action?
  @action_name.any? && @action_name.first != '*'
end

#controller?Boolean

Returns if this matcher has a controller specified.

Returns:

  • (Boolean)


325
326
327
# File 'lib/kin/nav.rb', line 325

def controller?
  @controller_name && @controller_name != '*'
end

#matches?(controller) ⇒ Boolean

Attempts to match against the given controller.

Parameters:

  • controller (#controller_name, #action_name)

    An object which behaves like a controller.

Returns:

  • (Boolean)


350
351
352
353
354
355
356
357
358
359
# File 'lib/kin/nav.rb', line 350

def matches?(controller)
  if not controller?
    true
  elsif not action?
    @controller_name == controller.controller_name
  else
    @controller_name == controller.controller_name &&
    @action_name.include?(controller.action_name)
  end
end