Class: NBTFile::Types::Compound

Inherits:
Object
  • Object
show all
Includes:
Enumerable, Private::Base
Defined in:
lib/nbtfile.rb

Instance Method Summary collapse

Constructor Details

#initialize(contents = {}) ⇒ Compound

Returns a new instance of Compound.



960
961
962
963
964
965
966
# File 'lib/nbtfile.rb', line 960

def initialize(contents={})
  @hash = {}
  @key_order = []
  for key, value in contents
    self[key] = value
  end
end

Instance Method Details

#==(other) ⇒ Object



1018
1019
1020
# File 'lib/nbtfile.rb', line 1018

def ==(other)
  self.class == other.class && @hash == other.to_hash
end

#[](key) ⇒ Object



985
986
987
# File 'lib/nbtfile.rb', line 985

def [](key)
  @hash[key]
end

#[]=(key, value) ⇒ Object



973
974
975
976
977
978
979
980
981
982
983
# File 'lib/nbtfile.rb', line 973

def []=(key, value)
  unless key.instance_of? ::String
    raise TypeError, "Key must be a string"
  end
  unless value.kind_of? Private::Base
    raise TypeError, "#{value.class} is not an NBT type"
  end
  @key_order << key unless @hash.has_key? key
  @hash[key] = value
  value
end

#delete(key) ⇒ Object



989
990
991
992
993
994
995
# File 'lib/nbtfile.rb', line 989

def delete(key)
  if @hash.has_key? key
    @key_order.delete key
    @hash.delete key
  end
  self
end

#eachObject



1005
1006
1007
1008
1009
1010
1011
1012
# File 'lib/nbtfile.rb', line 1005

def each
  if block_given?
    @key_order.each { |k| yield k, @hash[k] }
    self
  else
    @key_order.each
  end
end

#has_key?(key) ⇒ Boolean Also known as: include?

Returns:

  • (Boolean)


968
969
970
# File 'lib/nbtfile.rb', line 968

def has_key?(key)
  @hash.has_key? key
end

#keysObject



997
998
999
# File 'lib/nbtfile.rb', line 997

def keys
  @key_order.dup
end

#to_hashObject



1014
1015
1016
# File 'lib/nbtfile.rb', line 1014

def to_hash
  @hash.dup
end

#valuesObject



1001
1002
1003
# File 'lib/nbtfile.rb', line 1001

def values
  @key_order.map { |k| @hash[k] }
end