Class: Navigator::TagActivator

Inherits:
Object
  • Object
show all
Defined in:
lib/navigator/tag_activator.rb

Overview

Conditionally activates a tag.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(search_key: :href, search_value: nil, target_key: :class, target_value: "active") ⇒ TagActivator

rubocop:disable Metrics/ParameterLists



9
10
11
12
13
14
# 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

Instance Attribute Details

#search_keyObject (readonly)

Returns the value of attribute search_key.



6
7
8
# File 'lib/navigator/tag_activator.rb', line 6

def search_key
  @search_key
end

#search_valueObject (readonly)

Returns the value of attribute search_value.



6
7
8
# File 'lib/navigator/tag_activator.rb', line 6

def search_value
  @search_value
end

#target_keyObject (readonly)

Returns the value of attribute target_key.



6
7
8
# File 'lib/navigator/tag_activator.rb', line 6

def target_key
  @target_key
end

#target_valueObject (readonly)

Returns the value of attribute target_value.



6
7
8
# File 'lib/navigator/tag_activator.rb', line 6

def target_value
  @target_value
end

Instance Method Details

#activatable?(attributes = {}) ⇒ Boolean

:reek:TooManyStatements

Returns:

  • (Boolean)


18
19
20
21
22
23
24
25
26
27
28
29
30
31
# 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 = {}) ⇒ Object



33
34
35
36
37
38
39
40
# 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