Class: CraftBook::NBT::EnumerableTag Abstract

Inherits:
Tag
  • Object
show all
Includes:
Enumerable
Defined in:
lib/craftbook/nbt/enumerable_tag.rb

Overview

This class is abstract.

Abstract base class for tags that can be enumerated.

Direct Known Subclasses

ByteArrayTag, ContainerTag, IntArrayTag, LongArrayTag

Constant Summary

Constants inherited from Tag

Tag::TYPE_BYTE, Tag::TYPE_BYTE_ARRAY, Tag::TYPE_COMPOUND, Tag::TYPE_DOUBLE, Tag::TYPE_END, Tag::TYPE_FLOAT, Tag::TYPE_INT, Tag::TYPE_INT_ARRAY, Tag::TYPE_LIST, Tag::TYPE_LONG, Tag::TYPE_LONG_ARRAY, Tag::TYPE_SHORT, Tag::TYPE_STRING

Instance Attribute Summary

Attributes inherited from Tag

#name, #type

Instance Method Summary collapse

Methods inherited from Tag

parse, #pretty, #pretty_print, #stringify, #to_json

Constructor Details

#initialize(type, name, *values) ⇒ EnumerableTag

Creates a new instance of the CraftBook::NBT::EnumerableTag class.

Parameters:

  • type (Integer)

    One of the TAG_* constants indicating the primitive tag type.

  • name (String, NilClass)

    The name of the tag, or nil when unnamed.

  • values (Array<Object>)

    Zero or more values to add during initialization.



18
19
20
21
22
# File 'lib/craftbook/nbt/enumerable_tag.rb', line 18

def initialize(type, name, *values)
  super(type, name)
  @values = Array.new
  values.each { |value| push(value) }
end

Instance Method Details

#[](index) ⇒ Object

Retrieves the child at the given index, or nil if index is out of bounds.

Parameters:

  • index (Integer)

    The zero-based index of the child element to retrieve.

Returns:

  • (Object)

    The element, or nil if index was out of bounds.



67
68
69
# File 'lib/craftbook/nbt/enumerable_tag.rb', line 67

def [](index)
  @values[index]
end

#[]=(index, value) ⇒ Object

Note:

Unlike normal Array object, when index is beyond the bounds of the collection, it will not insert nil elements to fill the space, the new item is simply appended to the end of the collection.

Sets the child element at the given index.

Parameters:

  • index (Integer)

    The zero-based index of the child element to set.

  • value (Object)

    The value to set.

Returns:

  • (Object)

    the value that was passed in.

Raises:

  • (TypeError)

    When value is nil.



82
83
84
85
# File 'lib/craftbook/nbt/enumerable_tag.rb', line 82

def []=(index, value)
  validate(value)
  @values[index] = value
end

#each {|child| ... } ⇒ self #eachEnumerable

Overloads:

  • #each {|child| ... } ⇒ self

    When called with a block, yields each child element to the block before returning self.

    Yield Parameters:

    • child (Object)

      Yields a child element to the block.

    Returns:

    • (self)
  • #eachEnumerable

    When called without a block, returns an Enumerator object for this instance.

    Returns:

    • (Enumerable)


44
45
46
47
48
# File 'lib/craftbook/nbt/enumerable_tag.rb', line 44

def each
  return enum_for(__method__) unless block_given?
  @values.compact.each { |child| yield child }
  self
end

#push(child) ⇒ Object Also known as: <<, add

Appends a value as a child of this instance.

Parameters:

  • child (Object)

    The value to add.

Returns:

  • (Object)

    the value that was added.

Raises:

  • (TypeError)

    When child is nil.



31
32
33
# File 'lib/craftbook/nbt/enumerable_tag.rb', line 31

def push(child)
  @values.push(validate(child))
end

#sizeInteger Also known as: length

Returns the number of child elements.

Returns:

  • (Integer)

    the number of child elements.



58
59
60
# File 'lib/craftbook/nbt/enumerable_tag.rb', line 58

def size
  @values.compact.size
end

#to_hHash{Symbol => Object}

Returns the hash-representation of this object.

Returns:

  • (Hash{Symbol => Object})

    the hash-representation of this object.



52
53
54
# File 'lib/craftbook/nbt/enumerable_tag.rb', line 52

def to_h
  { name: @name, type: @type, values: @values }
end