Sha256: 8c6d10e4635c84d18707adbcb428da04f3d9e42eea88fcae541b442b39ed9db8

Contents?: true

Size: 719 Bytes

Versions: 9

Compression:

Stored size: 719 Bytes

Contents

require 'json'

module SimpleNavigation
  module Renderer
    # Renders the navigation items as a object tree serialized as a json string,
    # can also output raw ruby Hashes
    class Json < SimpleNavigation::Renderer::Base
      def render(item_container)
        results = hash_render(item_container)
        options[:as_hash] ? results : results.to_json
      end

      private

      def hash_render(item_container)
        return nil unless item_container

        item_container.items.map do |item|
          {
            items: hash_render(item.sub_navigation),
            name: item.name,
            selected: item.selected?,
            url: item.url
          }
        end
      end
    end
  end
end

Version data entries

9 entries across 9 versions & 1 rubygems

Version Path
simple-navigation-4.3.0 lib/simple_navigation/renderer/json.rb
simple-navigation-4.2.0 lib/simple_navigation/renderer/json.rb
simple-navigation-4.1.0 lib/simple_navigation/renderer/json.rb
simple-navigation-4.0.5 lib/simple_navigation/renderer/json.rb
simple-navigation-4.0.4 lib/simple_navigation/renderer/json.rb
simple-navigation-4.0.3 lib/simple_navigation/renderer/json.rb
simple-navigation-4.0.2 lib/simple_navigation/renderer/json.rb
simple-navigation-4.0.1 lib/simple_navigation/renderer/json.rb
simple-navigation-4.0.0 lib/simple_navigation/renderer/json.rb