menu_maker 0.1.0 → 0.2.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: c7e55f905ac5c7999264b81b19e25e94b025ae0a
4
- data.tar.gz: 408568b945adf3267293c96fd1db0b7490afee29
3
+ metadata.gz: 554575bd90faff34a0416ecee38bdf5a8e7bf58b
4
+ data.tar.gz: 627a276776b88a6d9c252fe0b2825853a66731b6
5
5
  SHA512:
6
- metadata.gz: 4f174811fd7e5b77870f4eaf005f8919ccd87102a222b245592822e85f018329b1bbbd83c2c24028eb4df8de6162e1becd5ec9463ca6f4c5b5c5e405a1d27ed9
7
- data.tar.gz: 333120e0015eb5e48866664a56d9bc0e7146e14509c48cfc4ffcd2ceb87642166039b0fb5b52050fe164dc39dfdb68d38c4053ed7f12f108c955d47de5913643
6
+ metadata.gz: a46e7476f38bb66ad43d3f9385fa97769bcb25265fb631e21618bc4a030caba6e21f05e78c50f7f6b4996d2db39603b3a9b79c6c05845781df45f60b7383cfce
7
+ data.tar.gz: 6dc30576b1cd50ca7c6c05dbe944399705ba94da80b705f0ee783f6a4de3b70f93575dadfda373e57c0c92d34442479edb61720c1435c4ef20b9e9609c0e6fc8
@@ -2,7 +2,7 @@ module MenuMaker
2
2
  class CustomSubmenuRenderer < MenuRenderer
3
3
  render do
4
4
  output = build_menu do |item|
5
- options = { link: { class: 'active' } } if item.path == current_path
5
+ options = { link: { class: 'active' } } if item.has_path? current_path
6
6
  h.li(item.title, item.path, options || {})
7
7
  end
8
8
 
@@ -17,8 +17,8 @@ module MenuMaker
17
17
  items.each(&block)
18
18
  end
19
19
 
20
- def add(title, path, options = {})
21
- @items[title] = MenuItem.new(title, path, options)
20
+ def add(title, *paths, **options)
21
+ @items[title] = MenuItem.new(title, *paths, options)
22
22
  @current_item = title
23
23
 
24
24
  yield current_submenu if block_given?
@@ -74,14 +74,12 @@ module MenuMaker
74
74
  end
75
75
 
76
76
  class MenuItem
77
- attr_reader :title, :paths, :options
77
+ attr_reader :title, :options
78
78
 
79
- def initialize(title, path = nil, options = nil)
79
+ def initialize(title, *paths, **options)
80
80
  @title = title
81
- @paths = []
81
+ @paths = paths.map { |p| Path.convert(p) }
82
82
  @options = options
83
-
84
- @paths << path if path
85
83
  end
86
84
 
87
85
  attr_accessor :submenu
@@ -90,6 +88,10 @@ module MenuMaker
90
88
  !@submenu.nil?
91
89
  end
92
90
 
91
+ def paths
92
+ @paths
93
+ end
94
+
93
95
  def submenu_paths
94
96
  return [] unless has_submenu?
95
97
 
@@ -103,7 +105,7 @@ module MenuMaker
103
105
  end
104
106
 
105
107
  def has_path?(path)
106
- all_paths.any? { |p| p == path }
108
+ all_paths.include? Path.convert(path)
107
109
  end
108
110
 
109
111
  def method_missing(method, *args)
@@ -115,7 +117,7 @@ module MenuMaker
115
117
  end
116
118
 
117
119
  def path
118
- @paths.first
120
+ @paths.first.address
119
121
  end
120
122
 
121
123
  def render_submenu
@@ -44,16 +44,15 @@ module MenuMaker
44
44
  private
45
45
 
46
46
  def find_current_path(current_path)
47
- current_path || request_path || ''
47
+ Path.convert(current_path || request || '')
48
48
  end
49
49
 
50
50
  def helpers_has_request?
51
- helpers.respond_to?(:request) &&
52
- helpers.request.respond_to?(:path)
51
+ helpers.respond_to?(:request)
53
52
  end
54
53
 
55
- def request_path
56
- helpers.request.path if helpers_has_request?
54
+ def request
55
+ helpers.request if helpers_has_request?
57
56
  end
58
57
  end
59
58
  end
@@ -0,0 +1,61 @@
1
+ module MenuMaker
2
+ class Path
3
+ METHODS = %i[get post put patch delete]
4
+
5
+ attr_reader :method, :address
6
+
7
+ def self.convert(path)
8
+ method = path.class.name.to_s.split('::').last.to_s.downcase
9
+ method = :other unless Path.respond_to? "from_#{method}"
10
+
11
+ Path.send "from_#{method}", path
12
+ end
13
+
14
+ def self.from_string(address)
15
+ Path.new(:get, address.to_s)
16
+ end
17
+
18
+ def self.from_array(path)
19
+ has_method = proc { |el| METHODS.include? el }
20
+
21
+ method = path.find(&has_method) || :get
22
+ address = path.delete_if(&has_method).first
23
+
24
+ new method, address
25
+ end
26
+
27
+ def self.from_other(object)
28
+ fail PathError unless %i[path method].all? { |m| object.respond_to?(m) }
29
+
30
+ new(object.method.to_sym.downcase, object.path)
31
+ end
32
+
33
+ def self.from_path(path)
34
+ path if path.is_a?(Path) or fail PathError
35
+ end
36
+
37
+ def initialize(method, address)
38
+ fail PathError unless METHODS.include? method
39
+
40
+ @method = method
41
+ @address = address.to_s
42
+ end
43
+
44
+ def ==(other)
45
+ other = self.class.convert(other)
46
+ method == other.method && address == other.address
47
+ end
48
+
49
+ def to_s
50
+ address
51
+ end
52
+
53
+ PathError = Class.new StandardError
54
+ end
55
+ end
56
+
57
+ module Kernel
58
+ def Path(*args)
59
+ ::MenuMaker::Path.convert(args)
60
+ end
61
+ end
@@ -1,3 +1,3 @@
1
1
  module MenuMaker
2
- VERSION = "0.1.0"
2
+ VERSION = "0.2.0"
3
3
  end
data/lib/menu_maker.rb CHANGED
@@ -1,4 +1,5 @@
1
1
  require "menu_maker/version"
2
+ require "menu_maker/path"
2
3
  require "menu_maker/menu"
3
4
  require "menu_maker/menu_renderer"
4
5
  require "menu_maker/menu_renderer_container"