Sha256: c901b6053c5f6475cbbac417a8f06bf636f944d40739b3dae49356801f1d8185

Contents?: true

Size: 895 Bytes

Versions: 4

Compression:

Stored size: 895 Bytes

Contents

require 'language_cards/models/card_set'
module LanguageCards
  class MenuNode
    def initialize name, child
      @name = name

      if child.is_a?(Hash) and child.has_key?("mapping")
        @mapping = child.delete("mapping") # Extra unused data for the moment
        @child = CardSet.new(child)
      else
        @child = MenuNode.new(*child)
      end
    end

    def title(fmt = ' - ', rng = 0..-1)
      label[rng].delete_if(&:empty?).join(fmt)
    end

    # @return <Mode<CardSet> < Game>
    def mode(game_mode)
      child.mode(game_mode)
    end

    # This is the preferred method for the view as this object shouldn't
    # care about how it should be displayed in the view.
    # @return Array<String>
    def label
      [@name].push(*child.label)
    end

    def to_s
      label.delete_if(&:empty?).join(' - ')
    end

    private
    attr_reader :name, :child
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
language_cards-0.3.2 lib/language_cards/menu_node.rb
language_cards-0.3.1 lib/language_cards/menu_node.rb
language_cards-0.3.0 lib/language_cards/menu_node.rb
language_cards-0.2.0 lib/language_cards/menu_node.rb