Sha256: 0cf2135e2310a3075f2a9b07770638904f63c0e1dca204a4626b722f0b721bd9

Contents?: true

Size: 1.14 KB

Versions: 4

Compression:

Stored size: 1.14 KB

Contents

module Navigator
  # Conditionally activates a tag.
  class TagActivator
    def initialize settings = {}
      @settings = settings.with_indifferent_access.reverse_merge search_key: :href,
                                                                 search_value: nil,
                                                                 target_key: :class,
                                                                 target_value: "active"
    end

    def search_key
      settings.fetch :search_key
    end

    def search_value
      settings.fetch :search_value
    end

    def target_key
      settings.fetch :target_key
    end

    def target_value
      settings.fetch :target_value
    end

    def activatable? attributes = {}
      attributes = attributes.with_indifferent_access
      search_value.present? && attributes[search_key] == search_value
    end

    def activate attributes = {}
      attributes = attributes.with_indifferent_access

      if activatable? attributes
        attributes[target_key] = [attributes[target_key], target_value].compact * ' '
      end

      attributes
    end

    private

    attr_reader :settings
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
navigator-0.9.1 lib/navigator/tag_activator.rb
navigator-0.9.0 lib/navigator/tag_activator.rb
navigator-0.8.0 lib/navigator/tag_activator.rb
navigator-0.7.0 lib/navigator/tag_activator.rb