menu_maker 0.0.4 → 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 47864268c4ed156b0782b0114ac5c7514f1d4d13
4
- data.tar.gz: 3fa07dd36aea157ca2c4563fefadb778a6d2cbce
3
+ metadata.gz: c7e55f905ac5c7999264b81b19e25e94b025ae0a
4
+ data.tar.gz: 408568b945adf3267293c96fd1db0b7490afee29
5
5
  SHA512:
6
- metadata.gz: a4c843725dc031d7d68b567f387203f50393232f0abe33fd439073e3f2cd22d2c2e969341cb282259c1ed9bc739b61dd767a298248366488a3dbc002582492f7
7
- data.tar.gz: 667a5ecc09420615305ff60b8cd21f27c26882ace11d161423d4519e8776d8a7501fcc7a8a809811cb10fbf46d34b67344d02e18380c1b7c530cb1ae6d5b7071
6
+ metadata.gz: 4f174811fd7e5b77870f4eaf005f8919ccd87102a222b245592822e85f018329b1bbbd83c2c24028eb4df8de6162e1becd5ec9463ca6f4c5b5c5e405a1d27ed9
7
+ data.tar.gz: 333120e0015eb5e48866664a56d9bc0e7146e14509c48cfc4ffcd2ceb87642166039b0fb5b52050fe164dc39dfdb68d38c4053ed7f12f108c955d47de5913643
@@ -2,10 +2,9 @@ module MenuMaker
2
2
  class Menu
3
3
  include Enumerable
4
4
 
5
- def initialize(renderer, depth = 1)
6
- @items = {}
7
- @current_depth = depth
8
- @renderer = renderer
5
+ def initialize(renderer)
6
+ @items = {}
7
+ @renderer = renderer
9
8
 
10
9
  yield self if block_given?
11
10
  end
@@ -28,9 +27,13 @@ module MenuMaker
28
27
  end
29
28
 
30
29
  def render
31
- renderer_for(@current_depth).call(self)
30
+ renderer_for(current_depth).call(self)
32
31
  end
33
32
 
33
+ protected
34
+
35
+ attr_writer :current_depth
36
+
34
37
  private
35
38
 
36
39
  def current_item
@@ -38,14 +41,14 @@ module MenuMaker
38
41
  end
39
42
 
40
43
  def current_submenu
41
- current_item.submenu ||
42
- current_item.create_submenu!(
43
- renderer_for(next_depth), next_depth
44
- )
44
+ current_item.submenu || create_submenu!
45
45
  end
46
46
 
47
- def next_depth
48
- @current_depth + 1
47
+ def create_submenu!
48
+ submenu = Menu.new renderer_for(next_depth)
49
+ submenu.current_depth = next_depth
50
+
51
+ current_item.submenu = submenu
49
52
  end
50
53
 
51
54
  def renderer_for(depth)
@@ -62,23 +65,41 @@ module MenuMaker
62
65
  renderer
63
66
  end
64
67
 
65
- MenuItem = Struct.new(:title, :path, :options) do
68
+ def current_depth
69
+ @current_depth || 1
70
+ end
71
+
72
+ def next_depth
73
+ current_depth + 1
74
+ end
75
+
76
+ class MenuItem
77
+ attr_reader :title, :paths, :options
78
+
79
+ def initialize(title, path = nil, options = nil)
80
+ @title = title
81
+ @paths = []
82
+ @options = options
83
+
84
+ @paths << path if path
85
+ end
86
+
66
87
  attr_accessor :submenu
67
88
 
68
89
  def has_submenu?
69
90
  !@submenu.nil?
70
91
  end
71
92
 
72
- def create_submenu!(renderer, depth)
73
- @submenu = Menu.new(renderer, depth)
74
- end
75
-
76
93
  def submenu_paths
77
- has_submenu? ? submenu.items.map(&:path) : []
94
+ return [] unless has_submenu?
95
+
96
+ submenu.items.reduce([]) do |all, item|
97
+ all + item.paths + item.submenu_paths
98
+ end.flatten
78
99
  end
79
100
 
80
101
  def all_paths
81
- [path] + submenu_paths
102
+ [*paths, *submenu_paths]
82
103
  end
83
104
 
84
105
  def has_path?(path)
@@ -93,6 +114,10 @@ module MenuMaker
93
114
  !!(options && options[method])
94
115
  end
95
116
 
117
+ def path
118
+ @paths.first
119
+ end
120
+
96
121
  def render_submenu
97
122
  has_submenu? ? submenu.render : ''
98
123
  end
@@ -1,3 +1,3 @@
1
1
  module MenuMaker
2
- VERSION = "0.0.4"
2
+ VERSION = "0.1.0"
3
3
  end