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

Returns a new instance of TagActivator.



8
9
10
11
12
13
# File 'lib/navigator/tag_activator.rb', line 8

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

Returns:

  • (Boolean)


15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/navigator/tag_activator.rb', line 15

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)
    !!(current_search_value =~ search_value)
  else
    current_search_value == search_value
  end
end

#activate(attributes = {}) ⇒ Object



28
29
30
31
32
# File 'lib/navigator/tag_activator.rb', line 28

def activate attributes = {}
  attributes = attributes.with_indifferent_access
  attributes[target_key] = [attributes[target_key], target_value].compact.join(" ") if activatable? attributes
  attributes
end