menuizer 0.2.0 → 0.2.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,15 +1,15 @@
1
1
  ---
2
2
  !binary "U0hBMQ==":
3
3
  metadata.gz: !binary |-
4
- NDRiOGYzNzI1NjcyZTg1NmE2YjJiZjcwN2MwZWYwOGE5YWI0OWNhZA==
4
+ MDZjZGE1ZjIzOWNlYTg4OTAwODg3YjExNzNkZjFiNmVjY2JlMTgyZg==
5
5
  data.tar.gz: !binary |-
6
- MWRhY2RmODdlZDIwNmYyYzgyYTg2ODE3MTdiZWIyN2RhZTNlNGEyMA==
6
+ NTMzMTczNDhiOTc4OTA2MTBkNmE5MWUzZDRhYTRjMjllYjM4YzFhYQ==
7
7
  SHA512:
8
8
  metadata.gz: !binary |-
9
- YzIyOGZhNjdiZjI4YWIwZGQ1NzZhNzBmN2YxNDQ4MjFhYWIwNGYyNDJlZjE2
10
- ZGU0MGYyYjA4ODY1MDE1MWEzMWM5Zjk1ZjQ5NGMzYmUyOGUzMjMzYTI1YTMx
11
- MGE0OTAyZWEwM2I0NGRhZTMyNzg2NmUwYTY5OTc4ZTg3ZDkzMWY=
9
+ MWU1NjM5OWViMGY0MDkwYTMyNzVlMzlkZWFjOWViZDhlZjA4MTFlNmZkOGZj
10
+ YzM2ODVlYjJhMzdlMTVlMTE3OTExYzM1NzdlZjIwZjA4YWZmZjdmMWNhMDMz
11
+ OWIwMTVhMGM2NmFkZjU2N2Q1YmVhZDA3MmYzNTljNGNmNjJhOGE=
12
12
  data.tar.gz: !binary |-
13
- OThlNTIyZDQ1YWJiNzFiYzNmMmMzZTZmODQ2MzYxOGVjMzk2MmUyOTEyYmU3
14
- NTI2OTA2MWRhYjczMjBjMTIzMjMzYjBkZmM0NzU2ZWQ5ZTU4ZTIwMWIwZmJk
15
- NDg1MWM0NmYyYzYyNmQzZjE5OTFkYjgxNjZiNGY3NTY1NDVkYzc=
13
+ MmY1OWEwYmFhNjE5NGQ2ZjZjNWFkZmIxNTNjNGUwZjQyYWFjYWFjNDI2YWU1
14
+ OTNhOTE0NjJlNjI5ZDcwNGE4YmVjYmE4N2U4ODFlNjEyOGIxYjdjYjM2NmMy
15
+ NTRlM2FhOTc0ZmU1YmNkZDk4ODNlNTEyYjBmZDFjZDY4N2E4Yzg=
data/README.md CHANGED
@@ -139,8 +139,38 @@ end
139
139
  <% end %>
140
140
  ```
141
141
 
142
+ ## Generators
143
+
144
+ Generate menu items by ruby code:
145
+
146
+ ```ruby
147
+ # config/initializers/menuizer.rb
148
+ Menuizer.configure do |config|
149
+ ...
150
+ config.generator = {
151
+ generate_items: ->{
152
+ [
153
+ {item: "generate item1"},
154
+ {item: "generate item2"},
155
+ ]
156
+ },
157
+ }
158
+ end
159
+ ```
160
+
161
+ ```yaml
162
+ # config/menuizer.yml
163
+ - header: MAIN NAVIGATION
164
+ - items: :generate_items
165
+ # =>
166
+ # - item: generate item1
167
+ # - item: generate item2
168
+ ```
169
+
142
170
  ## Converters
143
171
 
172
+ Convert menu item's property:
173
+
144
174
  ```ruby
145
175
  # config/initializers/menuizer.rb
146
176
  Menuizer.configure do |config|
@@ -159,7 +189,7 @@ end
159
189
 
160
190
  ```ruby
161
191
  menuizer.items.each do |item|
162
- item.icon #=> call converter
192
+ item.icon #=> "fa fa-circle-o" <= converter[:icon].call(icon,opts)
163
193
  end
164
194
  ```
165
195
 
@@ -184,7 +214,7 @@ opts: all key-value hash
184
214
  * convert `:"#{namespace}#{model.model_name.plural}"` if title respond to `model_name.plural`
185
215
  * or, leave `nil`
186
216
 
187
- **what namespace is?**
217
+ **what is namespace?**
188
218
 
189
219
  ↓↓↓
190
220
 
data/lib/menuizer/menu.rb CHANGED
@@ -1,16 +1,35 @@
1
1
  class Menuizer::Menu
2
- def initialize(namespace)
3
- if namespace
2
+ def initialize(namespace,config)
3
+ @config = config
4
+ @parent = nil
5
+
6
+ @namespace = namespace
7
+ if @namespace
4
8
  @namespace = "#{namespace}_"
5
9
  item_class = :"Item_#{namespace}"
6
- if self.class.const_defined?(item_class)
7
- @item_class = self.class.const_get(item_class)
8
- else
9
- @item_class = self.class.const_set(item_class, Class.new(Item))
10
- end
11
10
  else
12
- @namespace = nil
13
- @item_class = Item
11
+ item_class = :ItemDefault
12
+ end
13
+
14
+ if self.class.const_defined?(item_class)
15
+ @item_class = self.class.const_get(item_class)
16
+ else
17
+ @item_class = self.class.const_set(item_class, Class.new(Item))
18
+
19
+ if converter = @config.converter
20
+ @item_class.instance_eval do
21
+ converter.each do |key,block|
22
+ define_method key do
23
+ block.call @opts[key], @opts
24
+ end
25
+ end
26
+ end
27
+ end
28
+ end
29
+
30
+ if path = @config.file_path
31
+ require "yaml"
32
+ load_data YAML.load_file(path)
14
33
  end
15
34
  end
16
35
 
@@ -26,11 +45,11 @@ class Menuizer::Menu
26
45
  end
27
46
  end
28
47
  def active_item
29
- @active_item
48
+ @active_item ||= nil
30
49
  end
31
50
  def active_items
32
51
  result = []
33
- item = @active_item
52
+ item = active_item
34
53
  while item
35
54
  result << item
36
55
  item = item.parent
@@ -38,34 +57,30 @@ class Menuizer::Menu
38
57
  result.reverse
39
58
  end
40
59
 
41
- def set_converter(key,&block)
42
- @item_class.class_eval do
43
- define_method key do
44
- block.call @opts[key], @opts
45
- end
46
- end
47
- end
48
-
49
- def load(data)
50
- return unless data.respond_to?(:each)
51
- data.each do |item|
52
- if item.respond_to?(:map) && item.respond_to?(:delete)
53
- item = item.map{|k,v| [k.to_sym,v]}.to_h
54
- if title = item.delete(:header)
55
- add_header title
56
- else
57
- add_item item.delete(:item), item
58
- end
59
- end
60
- end
61
- end
62
-
63
60
  def items
64
61
  @items ||= []
65
62
  end
66
63
 
67
64
  private
68
65
 
66
+ def load_data(data)
67
+ return unless data.respond_to?(:each)
68
+ data.each do |item|
69
+ if item.respond_to?(:map)
70
+ item = item.map{|k,v| [k.to_sym,v]}.to_h
71
+ if title = item.delete(:header)
72
+ add_header title
73
+ elsif items = item.delete(:items)
74
+ if generator = @config.generator[items]
75
+ load_data generator.call
76
+ end
77
+ else
78
+ add_item item.delete(:item), item
79
+ end
80
+ end
81
+ end
82
+ end
83
+
69
84
  def add_header(title)
70
85
  current << @item_class.new(
71
86
  type: :header,
@@ -100,7 +115,7 @@ class Menuizer::Menu
100
115
  **opts,
101
116
  )
102
117
  @current = item.children
103
- self.load children
118
+ load_data children
104
119
  @current, @parent = parents, owner
105
120
  current << item
106
121
  end
data/lib/menuizer.rb CHANGED
@@ -5,21 +5,10 @@ require "menuizer/menu/item"
5
5
  module Menuizer
6
6
  class << self
7
7
  def configure(namespace=nil)
8
- yield (config[namespace] ||= OpenStruct.new)
8
+ yield config_for_namespace(namespace)
9
9
  end
10
10
  def menu(namespace=nil)
11
- Menu.new(namespace).tap{|menu|
12
- c = config[namespace]
13
- if c.respond_to?(:converter)
14
- c.converter.each do |key,block|
15
- menu.set_converter key, &block
16
- end
17
- end
18
- if c.respond_to?(:file_path) && path = c.file_path
19
- require "yaml"
20
- menu.load YAML.load_file(path)
21
- end
22
- }
11
+ Menu.new namespace, config_for_namespace(namespace)
23
12
  end
24
13
 
25
14
  private
@@ -27,5 +16,8 @@ module Menuizer
27
16
  def config
28
17
  @config ||= {}
29
18
  end
19
+ def config_for_namespace(namespace)
20
+ config[namespace] ||= OpenStruct.new
21
+ end
30
22
  end
31
23
  end
data/version.txt CHANGED
@@ -1 +1 @@
1
- 0.2.0
1
+ 0.2.1
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: menuizer
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.0
4
+ version: 0.2.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - shun
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2016-10-05 00:00:00.000000000 Z
11
+ date: 2016-10-11 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler