Class: MenuConfig
- Inherits:
-
Object
- Object
- MenuConfig
- Includes:
- ActionView::Helpers::UrlHelper
- Defined in:
- lib/emenu/menu_config.rb
Instance Attribute Summary collapse
-
#config ⇒ Object
readonly
Returns the value of attribute config.
-
#selected ⇒ Object
Returns the value of attribute selected.
Class Method Summary collapse
Instance Method Summary collapse
- #all_menus ⇒ Object
- #find(key) ⇒ Object
- #find_path(key) ⇒ Object
- #header_haml ⇒ Object
-
#initialize ⇒ MenuConfig
constructor
A new instance of MenuConfig.
- #item(title, &block) ⇒ Object
- #open(name) ⇒ Object
- #opened?(name) ⇒ Boolean
- #to_haml ⇒ Object
- #to_html ⇒ Object (also: #render)
Constructor Details
#initialize ⇒ MenuConfig
Returns a new instance of MenuConfig.
8 9 10 11 |
# File 'lib/emenu/menu_config.rb', line 8 def initialize() @config = {} @opened = {} end |
Instance Attribute Details
#config ⇒ Object (readonly)
Returns the value of attribute config.
4 5 6 |
# File 'lib/emenu/menu_config.rb', line 4 def config @config end |
#selected ⇒ Object
Returns the value of attribute selected.
4 5 6 |
# File 'lib/emenu/menu_config.rb', line 4 def selected @selected end |
Class Method Details
.define(&block) ⇒ Object
13 14 15 16 17 |
# File 'lib/emenu/menu_config.rb', line 13 def self.define(&block) result = MenuConfig.new block.call(result) return result end |
.enforce_uniqueness(names) ⇒ Object
26 27 28 29 30 |
# File 'lib/emenu/menu_config.rb', line 26 def self.enforce_uniqueness(names) counted_names = names.flatten.inject(Hash.new(0)) { |count, name| count[name] += 1 ; count } more_than_one = counted_names.map { |key, count| key if count > 1 }.compact raise "It's not allowed to have same menu keys: #{more_than_one.join(", ")}" unless more_than_one.size == 0 end |
Instance Method Details
#all_menus ⇒ Object
56 57 58 |
# File 'lib/emenu/menu_config.rb', line 56 def @config.each_value.inject([]) { |result, | result << .all_items }.flatten end |
#find(key) ⇒ Object
46 47 48 49 |
# File 'lib/emenu/menu_config.rb', line 46 def find(key) .each { || return if .title == key } raise "Key #{key} not found in menu structure" end |
#find_path(key) ⇒ Object
51 52 53 54 |
# File 'lib/emenu/menu_config.rb', line 51 def find_path(key) .each { || return .title if .path == key } return nil end |
#header_haml ⇒ Object
60 61 62 |
# File 'lib/emenu/menu_config.rb', line 60 def header_haml "#menu\n" end |
#item(title, &block) ⇒ Object
19 20 21 22 23 24 |
# File 'lib/emenu/menu_config.rb', line 19 def item(title, &block) raise "It's not allowed to have same menu keys: #{title}" unless @config[title].nil? current = @config[title] = Menu.new(title, nil, self) current.instance_eval(&block) unless block.nil? MenuConfig.enforce_uniqueness @config.each_value.map(&:names) end |
#open(name) ⇒ Object
32 33 34 |
# File 'lib/emenu/menu_config.rb', line 32 def open(name) @opened[name] = :opened end |
#opened?(name) ⇒ Boolean
36 37 38 |
# File 'lib/emenu/menu_config.rb', line 36 def opened?(name) @opened[name] == :opened end |
#to_haml ⇒ Object
69 70 71 |
# File 'lib/emenu/menu_config.rb', line 69 def to_haml @config.each_value.inject(header_haml) { |result, | result << .to_haml.split("\n").map { |token| Menu::DEFAULT_SPACE + token + "\n" }.join } end |
#to_html ⇒ Object Also known as: render
64 65 66 |
# File 'lib/emenu/menu_config.rb', line 64 def to_html Haml::Engine.new(to_haml).render(self) end |