Class: Lich::Gemstone::Disk

Inherits:
Object
  • Object
show all
Defined in:
lib/gemstone/disk.rb

Overview

Represents a disk item in the game.

Constant Summary collapse

NOUNS =
%w{cassone chest coffer coffin coffret disk hamper saucer sphere trunk tureen}

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(obj) ⇒ Disk

Initializes a new Disk object.

Examples:

disk = Disk.new(some_game_object)

Parameters:

  • obj (Object)

    The object representing the disk.



61
62
63
64
65
66
# File 'lib/gemstone/disk.rb', line 61

def initialize(obj)
  @id   = obj.id
  @name = obj.name.split(" ").find do |word|
    word[0].upcase.eql?(word[0])
  end
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method, *args) ⇒ Object

Handles missing methods by delegating to the GameObj.

Examples:

disk.some_missing_method

Parameters:

  • method (Symbol)

    The method name that was called.

  • args (Array)

    The arguments passed to the method.

Returns:

  • (Object)

    The result of the method call on GameObj.



95
96
97
# File 'lib/gemstone/disk.rb', line 95

def method_missing(method, *args)
  GameObj[@id].send(method, *args)
end

Instance Attribute Details

#idObject (readonly)

Returns the value of attribute id.



53
54
55
# File 'lib/gemstone/disk.rb', line 53

def id
  @id
end

#nameObject (readonly)

Returns the value of attribute name.



53
54
55
# File 'lib/gemstone/disk.rb', line 53

def name
  @name
end

Class Method Details

.allArray<Disk>

Retrieves all disk objects from the loot.

Examples:

Disk.all

Returns:

  • (Array<Disk>)

    An array of Disk objects.



45
46
47
48
49
50
51
# File 'lib/gemstone/disk.rb', line 45

def self.all()
  (GameObj.loot || []).select do |item|
    is_disk?(item)
  end.map do |i|
    Disk.new(i)
  end
end

.find_by_name(name) ⇒ Disk?

Finds a disk by its name.

Examples:

Disk.find_by_name("golden disk")

Parameters:

  • name (String)

    The name of the disk to find.

Returns:

  • (Disk, nil)

    Returns a Disk object if found, nil otherwise.



23
24
25
26
27
28
29
# File 'lib/gemstone/disk.rb', line 23

def self.find_by_name(name)
  disk = GameObj.loot.find do |item|
    is_disk?(item) && item.name.include?(name)
  end
  return nil if disk.nil?
  Disk.new(disk)
end

.is_disk?(thing) ⇒ Boolean

Checks if the given object is a disk based on its name.

Examples:

Disk.is_disk?(some_object)

Parameters:

  • thing (Object)

    The object to check.

Returns:

  • (Boolean)

    Returns true if the object is a disk, false otherwise.



13
14
15
# File 'lib/gemstone/disk.rb', line 13

def self.is_disk?(thing)
  thing.name =~ /\b([A-Z][a-z]+) #{Regexp.union(NOUNS)}\b/
end

.mineDisk?

Mines a disk based on the character’s name.

Examples:

Disk.mine

Returns:

  • (Disk, nil)

    Returns a Disk object if found, nil otherwise.



36
37
38
# File 'lib/gemstone/disk.rb', line 36

def self.mine
  find_by_name(Char.name)
end

Instance Method Details

#==(other) ⇒ Boolean

Compares this Disk object with another for equality.

Examples:

disk1 == disk2

Parameters:

  • other (Object)

    The object to compare with.

Returns:

  • (Boolean)

    Returns true if the objects are equal, false otherwise.



74
75
76
# File 'lib/gemstone/disk.rb', line 74

def ==(other)
  other.is_a?(Disk) && other.id == self.id
end

#eql?(other) ⇒ Boolean

Checks if this Disk object is equal to another.

Examples:

disk1.eql?(disk2)

Parameters:

  • other (Object)

    The object to compare with.

Returns:

  • (Boolean)

    Returns true if the objects are equal, false otherwise.



84
85
86
# File 'lib/gemstone/disk.rb', line 84

def eql?(other)
  self == other
end

#to_containerContainer, Object

Note:

This method depends on the existence of the Container class.

Converts the Disk object to a container.

Examples:

disk.to_container

Returns:

  • (Container, Object)

    Returns a Container object if defined, otherwise the GameObj.



105
106
107
108
109
110
111
# File 'lib/gemstone/disk.rb', line 105

def to_container
  if defined?(Container)
    Container.new(@id)
  else
    GameObj["#{@id}"]
  end
end