Sha256: 25d2f16d718f3857e92c553fa1bfb3b8314b75ed3e801f19a813ef87ccebf435

Contents?: true

Size: 1.57 KB

Versions: 11

Compression:

Stored size: 1.57 KB

Contents

module With
  class Node
    include Implementation
    
    attr_accessor :name, :parent, :children, :calls
    
    def initialize(name = nil, &block)
      @name = name
      @block = block
      @children = []
      @calls = {}
    end
    
    def initialize_copy(orig)
      @children = orig.children.map { |child| child.clone }
      @children.each { |child| child.parent = self }
      @calls = {}
      orig.calls.each { |key, calls| @calls[key] = calls.map{ |call| call.clone } }
    end
    
    def define(&block)
      @block = block
      instance_eval &block
    end
    
    # def select(&block)
    #   result = yield(self) ? [self] : [] #  || !@calls.select(&block).empty?
    #   result += @children.map { |child| child.select(&block) }.flatten
    # end
    
    def filter(conditions)
      if With.applies?(parents.map(&:name), conditions)
        @children.each {|child| child.filter(conditions) }
      else
        parent.children.delete(self)
      end
    end
    
    def calls(stage = nil)
      stage.nil? ? @calls : (@calls[stage] ||= [])
    end

    def collect(stage)
      (parent ? parent.collect(stage) : []) + calls(stage).select {|call| call.applies?(self) }
    end

    def parents
      parent ? parent.parents + [parent] : []
    end

    def leafs
      return [self] if children.empty?
      children.map { |child| child.leafs }.flatten
    end
    
    def add_child(child)
      @children << child
      child.parent = self
    end

    def append_children(children)
      leafs.each { |leaf| children.each {|child| leaf.add_child(child.dup) } }
    end
  end
end

Version data entries

11 entries across 11 versions & 1 rubygems

Version Path
adva-0.2.4 test/with/lib/with/node.rb
adva-0.2.3 test/with/lib/with/node.rb
adva-0.2.2 test/with/lib/with/node.rb
adva-0.2.1 test/with/lib/with/node.rb
adva-0.2.0 test/with/lib/with/node.rb
adva-0.1.4 test/with/lib/with/node.rb
adva-0.1.3 test/with/lib/with/node.rb
adva-0.1.2 test/with/lib/with/node.rb
adva-0.1.1 test/with/lib/with/node.rb
adva-0.1.0 test/with/lib/with/node.rb
adva-0.0.1 test/with/lib/with/node.rb