Sha256: 9b0ed5af9b8c0575b41dba135b8012511fb5426efb1b6a8651ada8867bab42c9

Contents?: true

Size: 1.6 KB

Versions: 9

Compression:

Stored size: 1.6 KB

Contents

module Redcar
  class Menu
    include Enumerable
    include Redcar::Model
    
    attr_reader :text, :entries
  
    # A Menu will initially have nothing in it.
    def initialize(text=nil)
      @text, @entries = text || "", []
    end
  
    # Iterate over each entry
    def each
      entries.each {|e| yield e}
    end
  
    # Add a Redcar::MenuItem or a Redcar::Menu
    def <<(entry)
      entries << entry
      self
    end
  
    # Number of entries in the menu
    def length
      entries.length
    end
    
    # Fetch the sub_menu with the given name
    #
    # @param [String]
    # @return [Menu]
    def sub_menu(text)
      detect {|e| e.text == text and e.is_a?(Menu) }
    end
    
    def entry(text)
      detect {|e| e.text == text }
    end
    
    # Append items and sub_menus using the same syntax as Menu::Builder
    def build(&block)
      Menu::Builder.new(self, &block)
    end
    
    def ==(other)
      return false unless length == other.length
      return false unless text == other.text
      entries.zip(other.entries) do |here, there|
        return false unless here.class == there.class and here == there
      end
      true
    end
    
    # Merge two Menu trees together. Modifies this Menu.
    #
    # @param [Menu] another Menu
    def merge(other)
      other.entries.each do |other_entry|
        if here = entry(other_entry.text)
          if here.class == other_entry.class
            here.merge(other_entry)
          else
            entries[entries.index(here)] = other_entry
          end
        else
          self << other_entry
        end
      end
    end
  end
end

Version data entries

9 entries across 9 versions & 1 rubygems

Version Path
redcar-0.3.8.4 plugins/application/lib/application/menu.rb
redcar-0.3.8.3 plugins/application/lib/application/menu.rb
redcar-0.3.8.2 plugins/application/lib/application/menu.rb
redcar-0.3.8.1 plugins/application/lib/application/menu.rb
redcar-0.3.8 plugins/application/lib/application/menu.rb
redcar-0.3.7.1 plugins/application/lib/application/menu.rb
redcar-0.3.7 plugins/application/lib/application/menu.rb
redcar-0.3.6 plugins/application/lib/application/menu.rb
redcar-0.3.5 plugins/application/lib/application/menu.rb