class Navigator::TagActivator

Conditionally activates a tag.

Attributes

search_key[R]
search_value[R]
target_key[R]
target_value[R]

Public Class Methods

new(search_key: :href, search_value: nil, target_key: :class, target_value: "active") click to toggle source

rubocop:disable Metrics/ParameterLists

# File lib/navigator/tag_activator.rb, line 9
def initialize search_key: :href, search_value: nil, target_key: :class, target_value: "active"
  @search_key = search_key
  @search_value = search_value
  @target_key = target_key
  @target_value = target_value
end

Public Instance Methods

activatable?(attributes = {}) click to toggle source

:reek: TooManyStatements

# File lib/navigator/tag_activator.rb, line 18
def activatable? attributes = {}
  return false unless search_value.present?

  attributes = attributes.with_indifferent_access
  current_search_value = attributes[search_key]

  if current_search_value.is_a?(Regexp) || search_value.is_a?(Regexp)
    return false if current_search_value.blank?

    current_search_value.match? search_value
  else
    current_search_value == search_value
  end
end
activate(attributes = {}) click to toggle source
# File lib/navigator/tag_activator.rb, line 33
def activate attributes = {}
  attributes = attributes.with_indifferent_access

  return attributes unless activatable? attributes

  attributes[target_key] = [attributes[target_key], target_value].compact.join " "
  attributes
end