class Navigasmic::Item

Attributes

Public Class Methods

new(label, link, visible, options = {}) click to toggle source
# File lib/navigasmic/core/item.rb, line 4
def initialize(label, link, visible, options = {})
  @label, @link, @visible = label, link, visible
  @disabled = options.delete(:disabled_if)
  options.delete(:hidden_unless)

  @rules = calculate_highlighting_rules(options.delete(:highlights_on))
end

Public Instance Methods

disabled?() click to toggle source
# File lib/navigasmic/core/item.rb, line 16
def disabled?
  @disabled
end
hidden?() click to toggle source
# File lib/navigasmic/core/item.rb, line 12
def hidden?
  !@visible
end
highlights_on?(path, params) click to toggle source
# File lib/navigasmic/core/item.rb, line 24
def highlights_on?(path, params)
  return false unless @rules.any?
  params = params.except(*unwanted_keys)
  !!@rules.detect do |rule|
    case rule
    when String
      path == rule
    when Regexp
      path.match(rule)
    when TrueClass
      true
    when FalseClass
      false
    when Hash
      rule.except(*unwanted_keys).detect do |key, value|
        value = value.gsub(/^\//, '') if key == :controller
        value == params[key].to_s
      end
    else
      raise ArgumentError, 'Highlighting rules should be an array containing any of/or a Boolean, String, Regexp, Hash or Proc'
    end
  end
end

Private Instance Methods

calculate_highlighting_rules(rules) click to toggle source
# File lib/navigasmic/core/item.rb, line 50
def calculate_highlighting_rules(rules)
  [].tap do |highlighting_rules|
    if rules.nil?
      highlighting_rules << @link if link?
    else
      highlighting_rules.concat Array(rules)
    end
  end
end
unwanted_keys() click to toggle source
# File lib/navigasmic/core/item.rb, line 60
def unwanted_keys
  [:only_path, :use_routes]
end