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

Array of Attr s. It can hold body part for using as a model data for Node#expand. Amrita#a() method is a shortcut for Attr.new

Methods

<<   ==   []   []=   amrita_expand_node   clear   clone   delete   each   inspect   new   size   to_ruby  

Included Modules

Enumerable

Attributes

body  [R]  If you call a() { … }, block yields to body bodyは、elementの内容になる。

Public Class methods

Don‘t use AttrArray.new use a() instead

[Source]

# File lib/amrita/node.rb, line 101
    def initialize(*attrs, &block)
      @array = []
      attrs.each do |a|
        case a
        when Array, AttrArray
          a.each do |aa|
            self << aa
          end
        when Hash
          attrs[0].each do |k, v|
            k = k.id2name if k.is_a?(Symbol)
            self << Attr.new(k, v)
          end
        else
          self << a
        end
      end

      if block_given?
        @body = yield 
      else
        @body = Null
      end
    end

Public Instance methods

add an Attr

[Source]

# File lib/amrita/node.rb, line 142
    def <<(a)
      case a
      when Attr
        @array.each_with_index {|x, idx|
          if x.key == a.key
            @array[idx] = a
            return self
          end
        }
        @array << a
      when AttrArray
        a.each {|attr|
          self << attr
        }
      else
        raise TypeError, "must be Attr or AttrArray, not #{a.class}"
      end
      self
    end

含まれる属性が一致しているか。属性の順序は一致していなくても構わない。

[Source]

# File lib/amrita/node.rb, line 127
    def ==(x)
      return true if object_id == x.object_id
      return nil unless x.kind_of?(AttrArray)
      return false if size != x.size
      @array.each {|attr|
        return false if attr != x[attr.key]
      }
      true
    end

[Source]

# File lib/amrita/node.rb, line 167
    def [] key
      raise TypeError, "key must be a String/Symbol, but #{key.class}" if !key.is_a?(String) && !key.is_a?(Symbol)
      
      key = key.to_s
      @array.each do |x| return x if x.key == key end
      nil
    end

@overload [key] = aAttr

    keyとaAttr.keyが同じでなければならない。

@overload [key] = aString

    属性を設定

[Source]

# File lib/amrita/node.rb, line 179
    def []= key, val
      raise TypeError, "key must be a String/Symbol, but #{key.class}" if !key.is_a?(String) && !key.is_a?(Symbol)

      key = key.to_s
      if val.is_a?(Attr)
        raise if key != val.key
      else
        val = Attr.new key, val
      end
      self << val
      val
    end

[Source]

# File lib/amrita/node_expand.rb, line 269
    def amrita_expand_node(n, context)
      raise Amrita::ModelMisMatch(type, n.class)
    end

[Source]

# File lib/amrita/node.rb, line 162
    def clear
      @array.clear
      self
    end

[Source]

# File lib/amrita/node.rb, line 225
    def clone()
      AttrArray.new(self) {@body.clone}
    end

[Source]

# File lib/amrita/node.rb, line 192
    def delete key
      raise TypeError, "key must be a String/Symbol, but #{key.class}" if !key.is_a?(String) && !key.is_a?(Symbol)

      key = key.to_s
      @array.each_with_index {|x, idx|
        if x.key == key
          return @array.delete_at(idx)
        end
      }
      nil
    end

iterate on each Attr

[Source]

# File lib/amrita/node.rb, line 205
    def each(&block)
      @array.dup.each(&block)
    end

[Source]

# File lib/amrita/node.rb, line 137
    def inspect
      to_ruby
    end

[Source]

# File lib/amrita/node.rb, line 209
    def size
      @array.size
    end

[Source]

# File lib/amrita/node.rb, line 213
    def to_ruby
      ret = "a(" + @array.collect {|v| ":#{v.key}, #{v.value}"}.join(", ") + ")"
      case @body
      when nil, Null
      when Node
        ret += body.to_ruby
      else
        ret += body.inspect
      end
      ret
    end

[Validate]