Sha256: 6d9dd498505d3a932723278d1eef50336b816064504ba7bb65f1b14acad40d3b

Contents?: true

Size: 1.54 KB

Versions: 2

Compression:

Stored size: 1.54 KB

Contents

module RShade
  class EventTree
    include Enumerable

    attr_reader :current, :head

    def initialize
      @current = @head = EventTreeNode.new(nil, 0, nil)
    end

    def add(value, level)
      if current.level + 1 == level
        current.children << EventTreeNode.new(value, level, current)
        return
      end
      if current.level + 1 < level

        last = current.children.last
        unless last
          current.children << EventTreeNode.new(nil, current.level + 1 , current)
        end
        @current = current.children.last
        add(value, level)
        return
      end

      if current.level + 1 > level
        unless current.parent
          return
        end
        @current = current.parent
        add(value, level)
      end
    end

    def current!(&block)
      block.call(current.children.last) if current.children.last
    end

    def each(&block)
      @head.each(&block)
    end
  end

  class EventTreeNode
    include Enumerable
    attr_reader :children, :level, :vlevel, :value
    attr_accessor :parent

    def initialize(value, level, parent=nil)
      @children = []
      @level = level
      @parent = parent
      @value = value
      @vlevel = set_vlevel(parent)
    end

    def each(&block)
      block.call(self) if parent != nil || value != nil
      children.each { |item| item.each(&block) }
    end

    private

    def set_vlevel(node)
      return 0 if node == nil

      if node.value || node.level == 0
        node.vlevel + 1
      else
        set_vlevel(node.parent)
      end
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
rshade-0.1.9.1 lib/rshade/event_tree.rb
rshade-0.1.9 lib/rshade/event_tree.rb