module AdventureRL::Modifiers::Inventory

Constants

DEFAULT_INVENTORY_ID

Public Class Methods

new(*args) click to toggle source
Calls superclass method
# File lib/AdventureRL/Modifiers/Inventory.rb, line 6
def initialize *args
  @inventory = {}
  super
end

Public Instance Methods

<<(object, id = DEFAULT_INVENTORY_ID)
Alias for: add_object
add(object, id = DEFAULT_INVENTORY_ID)
Alias for: add_object
add_item(object, id = DEFAULT_INVENTORY_ID)
Alias for: add_object
add_object(object, id = DEFAULT_INVENTORY_ID) click to toggle source

Add any object to this Inventory. Pass an optional id, which can be used to access or remove the object afterwards.

# File lib/AdventureRL/Modifiers/Inventory.rb, line 14
def add_object object, id = DEFAULT_INVENTORY_ID
  @inventory[id] = []  unless (@inventory[id])
  @inventory[id] << object
end
Also aliased as: add_item, add, <<
added?(id)
Alias for: added_object?
added_item?(id)
Alias for: added_object?
added_object?(id) click to toggle source

Returns true, if object with id has been added to this Inventory. id can also be the object itself.

# File lib/AdventureRL/Modifiers/Inventory.rb, line 24
def added_object? id
  return (
    @inventory.key?(id) ||
    get_objects.include?(id)
  )
end
Also aliased as: added_item?, added?, has?
get(id = DEFAULT_INVENTORY_ID)
Alias for: get_object
get_object(id = DEFAULT_INVENTORY_ID) click to toggle source

Returns the last object with the given id. If no id is passed, return the last object with the unnamed id.

# File lib/AdventureRL/Modifiers/Inventory.rb, line 44
def get_object id = DEFAULT_INVENTORY_ID
  return @inventory[id].last
end
Also aliased as: get
get_objects(id = nil) click to toggle source

Returns all its objects. If optional argument id is passed, then return all objects with that id.

# File lib/AdventureRL/Modifiers/Inventory.rb, line 37
def get_objects id = nil
  return @inventory.values.flatten  unless (id)
  return @inventory[id]
end
has?(id)
Alias for: added_object?
remove(id = DEFAULT_INVENTORY_ID, call_removed_method = true)
Alias for: remove_object
remove_object(id = DEFAULT_INVENTORY_ID, call_removed_method = true) click to toggle source

Removes the last object with the given id. id can also be the object to be removed itself. If no id is given, remove the last object with the unnamed id.

# File lib/AdventureRL/Modifiers/Inventory.rb, line 81
def remove_object id = DEFAULT_INVENTORY_ID, call_removed_method = true
  if (@inventory.key? id)
    object = @inventory[id].delete_at(-1)
    object.removed  if (call_removed_method && object.methods.include?(:removed))
    return object
  end
  key = (@inventory.detect do |k, val|
    next val.include?(id)
  end || []) .first
  return nil  unless (@inventory.key? key)
  object = @inventory[key].delete id
  object.removed  if (call_removed_method && object.methods.include?(:removed))
  return object
end
Also aliased as: remove
remove_objects(id = nil) click to toggle source

Removes all objects with the given id. id can also be an added object itself; all objects with the same id will be removed. If no id is given, remove all objects.

# File lib/AdventureRL/Modifiers/Inventory.rb, line 53
def remove_objects id = nil
  unless (id)
    get_objects.each do |object|
      object.removed  if (object.methods.include? :removed)
    end
    return @inventory.clear
  end
  if (@inventory.key? id)
    objects = @inventory.delete(id)
    objects.each do |object|
      object.removed  if (object.methods.include? :removed)
    end
    return objects
  end
  return @inventory.delete((@inventory.detect do |key, val|
    if (id == val)
      @inventory[key].each do |object|
        object.removed  if (object.methods.include? :removed)
      end
      next true
    end
    next false
  end || []).first)
end
removed() click to toggle source

When removed is called on an object that has an Inventory, then also call remove_objects on that object.

# File lib/AdventureRL/Modifiers/Inventory.rb, line 99
def removed
  remove_objects
end