Sha256: 1f84789fd9587e13028ab3866ec70bc6e6f9822ccca9468b72d46159f9333755

Contents?: true

Size: 990 Bytes

Versions: 5

Compression:

Stored size: 990 Bytes

Contents

# frozen_string_literal: true

module Trifle
  module Docs
    module Helper
      class Tree
        attr_reader :mapping

        def initialize(mapping:)
          @mapping = mapping
        end

        def menu
          @menu ||= mapping.inject({}) do |out, (url, meta)|
            deep_merge(
              out, url.split('/').reverse.inject({ '_meta' => meta }) { |o, k| { k => o } }
            )
          end
        end

        def deep_merge(this_hash, other_hash, &block)
          deep_merge!(this_hash.dup, other_hash, &block)
        end

        def deep_merge!(this_hash, other_hash, &block)
          this_hash.merge!(other_hash) do |key, this_val, other_val|
            if this_val.is_a?(Hash) && other_val.is_a?(Hash)
              deep_merge(this_val, other_val, &block)
            elsif block_given?
              block.call(key, this_val, other_val)
            else
              other_val
            end
          end
        end
      end
    end
  end
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
trifle-docs-0.3.1 lib/trifle/docs/helper/tree.rb
trifle-docs-0.3.0 lib/trifle/docs/helper/tree.rb
trifle-docs-0.2.0 lib/trifle/docs/helper/tree.rb
trifle-docs-0.1.1 lib/trifle/docs/helper/tree.rb
trifle-docs-0.1.0 lib/trifle/docs/helper/tree.rb