Class: Trifle::Docs::Helper::Tree

Inherits:
Object
  • Object
show all
Defined in:
lib/trifle/docs/helper/tree.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(mapping:) ⇒ Tree

Returns a new instance of Tree.



9
10
11
# File 'lib/trifle/docs/helper/tree.rb', line 9

def initialize(mapping:)
  @mapping = mapping
end

Instance Attribute Details

#mappingObject (readonly)

Returns the value of attribute mapping.



7
8
9
# File 'lib/trifle/docs/helper/tree.rb', line 7

def mapping
  @mapping
end

Instance Method Details

#deep_merge(this_hash, other_hash, &block) ⇒ Object



21
22
23
# File 'lib/trifle/docs/helper/tree.rb', line 21

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

#deep_merge!(this_hash, other_hash, &block) ⇒ Object



25
26
27
28
29
30
31
32
33
34
35
# File 'lib/trifle/docs/helper/tree.rb', line 25

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


13
14
15
16
17
18
19
# File 'lib/trifle/docs/helper/tree.rb', line 13

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