merb-menus 0.2.0 → 0.2.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,51 +1,11 @@
1
1
  class Merb::Controller
2
2
  before do; Merb::Menus.reset! end
3
- before :generate_default_menu
4
3
 
5
- class << self
6
- def create_menu(name, &data)
7
- Merb::Menus::Menu.new(name).instance_eval &data
8
- end
9
-
10
- # ==== Parameters
11
- # top<Merb::Menus::Menu>::
12
- # sub<Symbol>:: name of desired submenu
13
- def get_submenu(top, sub)
14
- top.submenus.find {|m| m.name.to_s == sub.to_s}
15
- end
16
-
17
- # ==== Parameters
18
- # menu<Merb::Menus::Submenu>::
19
- # item<Symbol>:: name of desired item
20
- def get_item(menu, item)
21
- item = menu.items.find{|e| e.name.to_s == item.to_s}
22
- end
4
+ def self.create_menu(name, &data)
5
+ Merb::Menus::Menu.new(name).instance_eval &data
23
6
  end
24
7
 
25
- def get_submenu(top, sub)
26
- self.class.get_submenu(top, sub)
27
- end
28
-
29
- def get_item(menu, item)
30
- self.class.get_item(menu, item)
31
- end
32
-
33
- def menu_item(*args)
34
- menu, submenu, item = *args
35
-
36
- if item.nil?
37
- item = submenu
38
- submenu = menu
39
- menu = Merb::Menus.default.name
40
- end
41
-
42
- top = Merb::Menus[menu]
43
- Merb::Menus.current_menu = top
44
- generate_menu(top)
45
- submenu = get_submenu(top, submenu)
46
- top.current_submenu = submenu
47
- submenu.current_item = get_item(submenu, item)
48
- end
8
+ private
49
9
 
50
10
  def current_menu
51
11
  Merb::Menus.current_menu
@@ -65,24 +25,69 @@ class Merb::Controller
65
25
  @__submenu_generation__.item(*args)
66
26
  end
67
27
 
28
+ def get_submenu(top, sub)
29
+ top.submenus.find {|m| m.name.to_s == sub.to_s}
30
+ end
31
+
32
+ def get_item(submenu, item)
33
+ submenu.items.find {|e| e.name.to_s == item.to_s}
34
+ end
35
+
68
36
  def generate_menu(menu)
69
37
  menu.submenus.each do |m|
70
38
  @__submenu_generation__ = m
71
39
  instance_eval &m.data
72
40
  m.generated!
73
41
  end
42
+
43
+ menu_generated!
44
+ end
45
+
46
+ def menu_generated!
47
+ @__menu_generated__ = true
74
48
  end
75
49
 
76
- private
50
+ def menu_generated?
51
+ !!@__menu_generated__
52
+ end
77
53
 
78
- def generate_default_menu
79
- if top = Merb::Menus.current_menu = Merb::Menus.default
80
- generate_menu(top)
54
+ def no_menu!
55
+ @__no_menu__ = true
56
+ end
57
+
58
+ def no_menu?
59
+ !!@__no_menu__
60
+ end
61
+
62
+ def _menu_setup(menu, sub, item)
63
+ raise ArgumentError, "#{menu} is not a Merb::Menu" unless Merb::Menus::Menu === menu
64
+
65
+ top = Merb::Menus.current_menu = menu
66
+ generate_menu(menu)
67
+ if submenu = top.current_submenu = get_submenu(top, sub)
68
+ submenu.current_item = get_item(submenu, item)
69
+ end
70
+ end
81
71
 
82
- if menu = top.current_submenu = get_submenu(top, controller_name)
83
- menu.current_item = get_item(menu, action_name)
84
- end
72
+ def menu_item(*args)
73
+ menu, submenu, item = *args
74
+
75
+ if item.nil?
76
+ item = submenu
77
+ submenu = menu
78
+ menu = Merb::Menus.default.name
85
79
  end
80
+
81
+ top = Merb::Menus[menu]
82
+ raise Merb::Menus::NoMenuError, "Menu #{menu} does not exist" unless top
83
+
84
+ _menu_setup(top, submenu, item)
85
+ end
86
+
87
+ def generate_default_menu
88
+ return if no_menu? || menu_generated?
89
+
90
+ _menu_setup(Merb::Menus.default, controller_name, action_name)
86
91
  end
87
92
  end
88
93
 
@@ -1,6 +1,6 @@
1
1
  module Merb
2
2
  module Menus
3
- VERSION = "0.2.0" unless defined?(::Merb::Menus::VERSION)
3
+ VERSION = "0.2.1" unless defined?(::Merb::Menus::VERSION)
4
4
  end
5
5
  end
6
6
 
metadata CHANGED
@@ -5,8 +5,8 @@ version: !ruby/object:Gem::Version
5
5
  segments:
6
6
  - 0
7
7
  - 2
8
- - 0
9
- version: 0.2.0
8
+ - 1
9
+ version: 0.2.1
10
10
  platform: ruby
11
11
  authors:
12
12
  - jonah honeyman