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
47
48
# File 'lib/navigasmic/core/item.rb', line 24

def highlights_on?(path, params)
  params = clean_unwanted_keys(params)
  result = false

  @rules.each do |rule|
    highlighted = true

    case rule
      when String then highlighted &= path == rule
      when Regexp then highlighted &= path.match(rule)
      when TrueClass then highlighted &= rule
      when FalseClass then highlighted &= rule
      when Hash
        clean_unwanted_keys(rule).each do |key, value|
          value.gsub!(/^\//, '') if key == :controller
          highlighted &= value == params[key].to_s
        end
      else raise 'highlighting rules should be an array containing any of/or a Boolean, String, Regexp, Hash or Proc'
    end

    result |= highlighted
  end

  result
end

#link?Boolean

Returns:

  • (Boolean)


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

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