Class: MenuConfig

Inherits:
Object
  • Object
show all
Includes:
ActionView::Helpers::UrlHelper
Defined in:
lib/emenu/menu_config.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeMenuConfig

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

#configObject (readonly)

Returns the value of attribute config.



4
5
6
# File 'lib/emenu/menu_config.rb', line 4

def config
  @config
end

#selectedObject

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_menusObject



56
57
58
# File 'lib/emenu/menu_config.rb', line 56

def all_menus
  @config.each_value.inject([]) { |result, menu| result << menu.all_items }.flatten
end

#find(key) ⇒ Object



46
47
48
49
# File 'lib/emenu/menu_config.rb', line 46

def find(key)
  all_menus.each { |menu| return menu if menu.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)
  all_menus.each { |menu| return menu.title if menu.path == key }
  return nil
end

#header_hamlObject



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

Returns:

  • (Boolean)


36
37
38
# File 'lib/emenu/menu_config.rb', line 36

def opened?(name)
  @opened[name] == :opened
end

#to_hamlObject



69
70
71
# File 'lib/emenu/menu_config.rb', line 69

def to_haml
  @config.each_value.inject(header_haml) { |result, menu| result << menu.to_haml.split("\n").map { |token| Menu::DEFAULT_SPACE + token + "\n" }.join }
end

#to_htmlObject 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