Class: SimpleMenu

Inherits:
Object
  • Object
show all
Defined in:
lib/simple_menu.rb,
lib/simple_menu/railtie.rb,
lib/simple_menu/version.rb,
lib/simple_menu/view_helpers.rb

Defined Under Namespace

Modules: ViewHelpers Classes: Railtie

Constant Summary collapse

VERSION =
'0.1.1'

Instance Method Summary collapse

Constructor Details

#initialize(template, &block) ⇒ SimpleMenu

Returns a new instance of SimpleMenu.



8
9
10
11
12
13
14
15
16
17
18
19
# File 'lib/simple_menu.rb', line 8

def initialize(template, &block)
  self.template = template
  self.menu = []
  self.options = {
    save_with: Nokogiri::XML::Node::SaveOptions::FORMAT |
               Nokogiri::XML::Node::SaveOptions::NO_DECLARATION |
               Nokogiri::XML::Node::SaveOptions::NO_EMPTY_TAGS,
    indent: 2
  }
  
  add(&block) if block_given?
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(name, *args, &block) ⇒ Object



33
34
35
# File 'lib/simple_menu.rb', line 33

def method_missing(name, *args, &block)
  template.send(name, *args, &block)
end

Instance Method Details

#add(index = menu.length, &block) ⇒ Object



21
22
23
# File 'lib/simple_menu.rb', line 21

def add(index = menu.length, &block)
  menu.insert(index, instance_eval(&block))
end

#after(href_or_name, &block) ⇒ Object



29
30
31
# File 'lib/simple_menu.rb', line 29

def after(href_or_name, &block)
  add(index_of(href_or_name) + 1, &block)
end

#before(href_or_name, &block) ⇒ Object



25
26
27
# File 'lib/simple_menu.rb', line 25

def before(href_or_name, &block)
  add(index_of(href_or_name), &block)
end

#to_sObject



37
38
39
40
41
42
43
44
45
46
47
# File 'lib/simple_menu.rb', line 37

def to_s
  Nokogiri::XML::Builder.new do |xml|
    xml.ul do
      each do |item|
        xml.li do
          xml << highlight_current(item)
        end
      end
    end
  end.to_xml(options).html_safe
end