Class: Fume::Nav::NavTag

Inherits:
Object
  • Object
show all
Defined in:
lib/fume/nav/nav_tag.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(attributes = {}) ⇒ NavTag

Returns a new instance of NavTag.



6
7
8
9
10
11
# File 'lib/fume/nav/nav_tag.rb', line 6

def initialize(attributes = {})
  attributes.each_pair do |name, value|
    send("#{name}=", value)
  end
  @empty = true
end

Instance Attribute Details

#active_classObject

Returns the value of attribute active_class.



4
5
6
# File 'lib/fume/nav/nav_tag.rb', line 4

def active_class
  @active_class
end

#currentObject

Returns the value of attribute current.



4
5
6
# File 'lib/fume/nav/nav_tag.rb', line 4

def current
  @current
end

#helperObject

Returns the value of attribute helper.



4
5
6
# File 'lib/fume/nav/nav_tag.rb', line 4

def helper
  @helper
end

#hide_if_emptyObject

Returns the value of attribute hide_if_empty.



4
5
6
# File 'lib/fume/nav/nav_tag.rb', line 4

def hide_if_empty
  @hide_if_empty
end

#inactive_classObject

Returns the value of attribute inactive_class.



4
5
6
# File 'lib/fume/nav/nav_tag.rb', line 4

def inactive_class
  @inactive_class
end

Instance Method Details

#apply(value, &block) ⇒ Object



55
56
57
58
# File 'lib/fume/nav/nav_tag.rb', line 55

def apply(value, &block)
  result = match?(value, current)
  block.call(result ? active_class : inactive_class)
end

#apply_active_options(value, options) ⇒ Object



32
33
34
35
36
37
38
39
40
41
42
# File 'lib/fume/nav/nav_tag.rb', line 32

def apply_active_options(value, options)
  apply(value) do |cls|
    if options[:class]
      options[:class] += " #{cls}"
    else
      options[:class] = "#{cls}"
    end
  end

  @empty = false
end

#apply_content(value, &block) ⇒ Object



60
61
62
63
64
65
66
# File 'lib/fume/nav/nav_tag.rb', line 60

def apply_content(value, &block)
  @empty = false

  apply(value) do |cls|
    helper.capture(cls, &block)
  end
end

#content_tag(value, tag_name, options = {}, &block) ⇒ Object



27
28
29
30
# File 'lib/fume/nav/nav_tag.rb', line 27

def (value, tag_name, options = {}, &block)
  apply_active_options(value, options)
  helper.(tag_name, options, &block)
end

#hide?Boolean

Returns:

  • (Boolean)


13
14
15
# File 'lib/fume/nav/nav_tag.rb', line 13

def hide?
  hide_if_empty && @empty
end

#li_tag(value, options = {}, &block) ⇒ Object



17
18
19
# File 'lib/fume/nav/nav_tag.rb', line 17

def li_tag(value, options = {}, &block)
  (value, :li, options, &block)
end


21
22
23
24
25
# File 'lib/fume/nav/nav_tag.rb', line 21

def link_to(value, *args, &block)
  options = args.extract_options!
  apply_active_options(value, options)
  helper.link_to(*args, options, &block)
end

#match?(source, target) ⇒ Boolean

Returns:

  • (Boolean)


44
45
46
47
48
49
50
51
52
53
# File 'lib/fume/nav/nav_tag.rb', line 44

def match?(source, target)
  case source
  when Array
    source.each_with_index.all? { |value, index| match?(value, target[index]) }
  when Regexp
    source.match(target)
  else
    source.to_s == target.to_s
  end
end