Class: Navigasmic::Item

Inherits:
Object
  • Object
show all
Defined in:
lib/navigasmic/core/item.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(label, link, visible, options = {}) ⇒ Item

Returns a new instance of Item.


4
5
6
7
8
9
10
# 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

Instance Attribute Details

Returns the value of attribute link


3
4
5
# File 'lib/navigasmic/core/item.rb', line 3

def link
  @link
end

Instance Method Details

#disabled?Boolean

Returns:

  • (Boolean)

16
17
18
# File 'lib/navigasmic/core/item.rb', line 16

def disabled?
  @disabled
end

#hidden?Boolean

Returns:

  • (Boolean)

12
13
14
# File 'lib/navigasmic/core/item.rb', line 12

def hidden?
  !@visible
end

#highlights_on?(path, params) ⇒ Boolean

Returns:

  • (Boolean)

24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# 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

#link?Boolean

Returns:

  • (Boolean)

20
21
22
# File 'lib/navigasmic/core/item.rb', line 20

def link?
  @link.present? && !disabled?
end