Module: Navtastic

Defined in:
lib/navtastic.rb,
lib/navtastic/item.rb,
lib/navtastic/menu.rb,
lib/navtastic/railtie.rb,
lib/navtastic/version.rb,
lib/navtastic/renderer.rb,
lib/navtastic/configuration.rb,
lib/navtastic/renderer/bulma.rb,
lib/navtastic/renderer/simple.rb,
lib/navtastic/renderer/bootstrap4.rb,
lib/navtastic/renderer/foundation6.rb

Overview

:nodoc:

Defined Under Namespace

Modules: ViewHelpers Classes: Configuration, Item, Menu, Railtie, Renderer

Constant Summary collapse

VERSION =
'0.2.0'.freeze

Class Method Summary collapse

Class Method Details

.all_menusArray

A list of all defined menus

Returns:

  • (Array)


74
75
76
# File 'lib/navtastic.rb', line 74

def self.all_menus
  @menu_store.keys
end

.configurationNavtastic::Configuration

Returns current configuration.

Returns:



89
90
91
# File 'lib/navtastic/configuration.rb', line 89

def self.configuration
  @configuration ||= Configuration.new
end

.configure {|current| ... } ⇒ Object

Modify Navtastic's current configuration

Examples:

Modify the current configuration

Navtastic.configure do |config|
  config.setting = :updated
end

Yield Parameters:



106
107
108
# File 'lib/navtastic/configuration.rb', line 106

def self.configure
  yield configuration
end

.define(name) {|menu, params| ... } ⇒ Object

Define a new menu to be rendered later

Examples:

Define a new menu

Navtastic.define :main do |menu, params|
  menu.item "Home", "/"
end

Parameters:

  • name

    the name of the menu

Yields:

  • (menu, params)

    block to generate a new menu

Yield Parameters:

  • menu (Menu)

    the menu to be initialized

  • params (Hash)

    runtime parameters

Raises:

  • (ArgumentError)

    if no block was given



32
33
34
35
36
37
# File 'lib/navtastic.rb', line 32

def self.define(name, &block)
  raise ArgumentError, "no block given" unless block_given?

  name = name.to_sym if name.is_a? String
  @menu_store[name] = block
end

.render(name, current_url, params = {}) ⇒ Renderer

Render a stored menu

The params parameter is passed along to the block in define.

If params contains a :renderer key, it's removed from the hash and passed to the renderer instead. Look at the renderer documentation to see which options are supported.

Parameters:

  • name

    the name of the defined menu

  • current_url (String)

    the url of the current page

  • params (Hash) (defaults to: {})

    runtime parameters

Options Hash (params):

  • :renderer (Hash)

    Options passed to the renderer

Returns:

  • (Renderer)

    the renderer for the menu

Raises:

  • (KeyError)

    if the menu was not defined



55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/navtastic.rb', line 55

def self.render(name, current_url, params = {})
  name = name.to_sym if name.is_a? String
  block = @menu_store[name]

  raise KeyError, "menu not defined: #{name.inspect}" if block.nil?

  # Merge renderer options globally with those passed at runtime
  renderer_options = configuration.renderer_options.dup
  renderer_options.merge!(params.delete(:renderer) || {})

  menu = Menu.new
  block.call(menu, params)
  menu.current_url = current_url
  Navtastic.configuration.current_renderer.render(menu, renderer_options)
end

.reset_configurationObject

This will reset the configuration back to defaults



94
95
96
# File 'lib/navtastic/configuration.rb', line 94

def self.reset_configuration
  @configuration = nil
end