Class Amrita::NodeArray
In: lib/amrita/node_expand.rb
lib/amrita/format.rb
lib/amrita/node.rb
Parent: Object

represents an Array of Node. It is a Node also.

Methods

+   <<   ==   []   add   apply_to_children   children   clone   each   elements_with_attr   format   new   no_child?   pre_format1   size   to_a   to_ruby  

Included Modules

Node

Public Class methods

[Source]

# File lib/amrita/node.rb, line 607
    def initialize(*elements)
      if elements.size() == 1 and elements[0].kind_of?(NodeArray)
        @array = elements[0].to_a.collect {|n| n.clone}
      else
        @array = elements.collect do |a|
          #raise "can't be a parent of me!" if a.id == self.id # no recusive check because it costs too much
          to_node(a)
        end
      end
    end

Public Instance methods

[Source]

# File lib/amrita/node.rb, line 672
    def +(node)
      ret = clone
      ret << node
      ret
    end

[Source]

# File lib/amrita/node.rb, line 678
    def <<(node)
      raise "can't be a parent of me!" if node.equal?(self)
      case node
      when Array, NodeArray
        node.each {|n| self << n }
      when Node
        @array << node
      else
        @array << TextElement.new(node.to_s)
      end
      self
    end

[Source]

# File lib/amrita/node.rb, line 618
    def ==(x)
      case x
      when NodeArray, Array
        return false unless x.size() == @array.size()
        @array.each_with_index do |n, i|
          return false unless n == x[i]
        end
        true
      else
        false
      end
    end

[Source]

# File lib/amrita/node.rb, line 635
    def [](index)
      @array[index]
    end
add(node)

Alias for #<<

[Source]

# File lib/amrita/node_expand.rb, line 352
    def apply_to_children(hash, context)
      ret = []
      hid = context.tmpl_id

      nodes = to_a()
      while nodes.size > 0
        n = nodes.shift
        if n.is_a?(Element) && n.multi && n.multi.alt &&
                           (ary = hash[n.attrs[hid].value.intern]).is_a?(Array)
          # 配列を交互に展開する
          # self = <table>
          #   n = <tr amrita_id="foo+">
          # hash["foo"] = ["a", "b", ...]
          alt_id = n.attrs[hid].value
          alt_e = [n]
          while (nodes.first.is_a?(Element) && nodes.first.attrs[hid].value == alt_id) || (nodes.first.is_a?(TextElement) && nodes.first.to_s.strip == "")
            f = nodes.shift
            alt_e << f if f.is_a?(Element)
          end
          ary.each_with_index {|data, i|
            ret << alt_e[i % alt_e.size].clone.delete_attr!(hid).expand1(data, context)
          }
        else
          ret << yield(n)
        end
      end
      Node::to_node(ret)
    end

[Source]

# File lib/amrita/node.rb, line 668
    def children
      @array
    end

[Source]

# File lib/amrita/node.rb, line 664
    def clone
      NodeArray.new(self)
    end

[Source]

# File lib/amrita/node.rb, line 643
    def each
      @array.each {|e| yield e}
    end

[Source]

# File lib/amrita/node.rb, line 647
    def elements_with_attr(key, value = nil)
      raise TypeError if !key.is_a?(Symbol)
      ret = []
      @array.each {|node|
        if node.is_a?(Element) && (v = node.attrs[key])
          if value && v == value
            ret << node
          end
        end
      }
      return ret
    end

[Source]

# File lib/amrita/format.rb, line 574
    def format(f)
      @array.each { |n| n.format(f) }
    end

[Source]

# File lib/amrita/node.rb, line 639
    def no_child?
      @array.empty?
    end

[Source]

# File lib/amrita/format.rb, line 578
    def pre_format1(prf)
      @array.each do |n|
        n.pre_format1(prf)
      end
    end

[Source]

# File lib/amrita/node.rb, line 631
    def size()
      @array.size()
    end

[Source]

# File lib/amrita/node.rb, line 660
    def to_a
      @array.dup
    end

[Source]

# File lib/amrita/node.rb, line 692
    def to_ruby
      "[ " + @array.collect {|e| e.to_ruby}.join(", ") + " ]"
    end

[Validate]