Class: MenuMaker::Menu::MenuItem

Inherits:
Object
  • Object
show all
Defined in:
lib/menu_maker/menu.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(title, *paths, **options) ⇒ MenuItem

Returns a new instance of MenuItem.



79
80
81
82
83
# File 'lib/menu_maker/menu.rb', line 79

def initialize(title, *paths, **options)
  @title   = title
  @paths   = paths.map { |p| Path::Converter.convert(p) }
  @options = options
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method, *args) ⇒ Object



111
112
113
# File 'lib/menu_maker/menu.rb', line 111

def method_missing(method, *args)
  options && options[method] || ''
end

Instance Attribute Details

#optionsObject (readonly)

Returns the value of attribute options.



77
78
79
# File 'lib/menu_maker/menu.rb', line 77

def options
  @options
end

Returns the value of attribute submenu.



85
86
87
# File 'lib/menu_maker/menu.rb', line 85

def submenu
  @submenu
end

#titleObject (readonly)

Returns the value of attribute title.



77
78
79
# File 'lib/menu_maker/menu.rb', line 77

def title
  @title
end

Instance Method Details

#all_pathsObject



103
104
105
# File 'lib/menu_maker/menu.rb', line 103

def all_paths
  [*paths, *submenu_paths]
end

#has_path?(path) ⇒ Boolean

Returns:

  • (Boolean)


107
108
109
# File 'lib/menu_maker/menu.rb', line 107

def has_path?(path)
  all_paths.include? Path::Converter.convert(path)
end

#has_submenu?Boolean

Returns:

  • (Boolean)


87
88
89
# File 'lib/menu_maker/menu.rb', line 87

def has_submenu?
  !@submenu.nil?
end

#pathObject



119
120
121
# File 'lib/menu_maker/menu.rb', line 119

def path
  @paths.first.path
end

#pathsObject



91
92
93
# File 'lib/menu_maker/menu.rb', line 91

def paths
  @paths
end

#render_submenuObject



123
124
125
# File 'lib/menu_maker/menu.rb', line 123

def render_submenu
  has_submenu? ? submenu.render : ''
end

#respond_to_missing?(method) ⇒ Boolean

Returns:

  • (Boolean)


115
116
117
# File 'lib/menu_maker/menu.rb', line 115

def respond_to_missing?(method)
  !!(options && options[method])
end


95
96
97
98
99
100
101
# File 'lib/menu_maker/menu.rb', line 95

def submenu_paths
  return [] unless has_submenu?

  submenu.items.reduce([]) do |all, item|
    all + item.paths + item.submenu_paths
  end.flatten
end

#to_sObject



127
128
129
# File 'lib/menu_maker/menu.rb', line 127

def to_s
  title
end