Sha256: 0dc7de76f871cdb1f9e1182c951f8c0d199b3cb673ba18320796d4dd3701be02

Contents?: true

Size: 1.06 KB

Versions: 3

Compression:

Stored size: 1.06 KB

Contents

module MenuMaker
  class MenuRenderer
    attr_accessor :menu
    attr_reader   :current_path, :helpers

    alias_method :h, :helpers

    def self.render(&block)
      define_method :render do
        build_html do
          instance_eval(&block)
        end
      end
    end

    def initialize(helpers, path = nil)
      @helpers      = helpers
      @current_path = find_current_path(path)
    end

    def build_menu
      build_html do
        menu.items.inject('') do |out, item|
          out + yield(item, [])
        end
      end
    end

    def build_html
      output = yield ''

      if output.respond_to? :html_safe
        output.html_safe
      else
        output
      end
    end

    def call(menu)
      self.menu = menu
      render
    end

    private

    def find_current_path(current_path)
      current_path || request_path || ''
    end

    def helpers_has_request?
      helpers.respond_to?(:request) &&
        helpers.request.respond_to?(:path)
    end

    def request_path
      helpers.request.path if helpers_has_request?
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
menu_maker-0.1.0 lib/menu_maker/menu_renderer.rb
menu_maker-0.0.4 lib/menu_maker/menu_renderer.rb
menu_maker-0.0.3 lib/menu_maker/menu_renderer.rb