class Discorb::Dictionary

Attributes

limit[RW]

@return [Integer] The maximum number of items in the dictionary.

Public Class Methods

new(hash = {}, limit: nil, sort: false) click to toggle source

Initialize a new Dictionary.

@param [Hash] hash A hash of items to add to the dictionary. @param [Integer] limit The maximum number of items in the dictionary. @param [false, Proc] sort Whether to sort the items in the dictionary.

# File lib/discorb/dictionary.rb, line 15
def initialize(hash = {}, limit: nil, sort: false)
  @cache = hash.transform_keys(&:to_s)
  @limit = limit
  @sort = sort
end

Public Instance Methods

[](id)
Alias for: get
[]=(id, body)
Alias for: register
get(id) click to toggle source

Get an item from the dictionary.

@param [#to_s] id The ID of the item. @return [Object] The item. @return [nil] if the item was not found.

@overload get(index)

@param [Integer] index The index of the item.

@return [Object] The item.
@return [nil] if the item is not found.
# File lib/discorb/dictionary.rb, line 67
def get(id)
  res = @cache[id.to_s]
  if res.nil? && id.is_a?(Integer) && id < @cache.length
    @cache.values[id]
  else
    res
  end
end
Also aliased as: []
has?(id) click to toggle source

Checks if the dictionary has an ID.

@param [#to_s] id The ID to check.

@return [Boolean] `true` if the dictionary has the ID, `false` otherwise.

# File lib/discorb/dictionary.rb, line 92
def has?(id)
  !self[id].nil?
end
inspect() click to toggle source
# File lib/discorb/dictionary.rb, line 118
def inspect
  "#<#{self.class} #{values.length} items>"
end
merge(other) click to toggle source

Merges another dictionary into this one.

@param [Discorb::Dictionary] other The dictionary to merge.

# File lib/discorb/dictionary.rb, line 41
def merge(other)
  @cache.merge!(other)
end
method_missing(name, ...) click to toggle source

Send a message to the array of values.

Calls superclass method
# File lib/discorb/dictionary.rb, line 99
def method_missing(name, ...)
  if values.respond_to?(name)
    values.send(name, ...)
  else
    super
  end
end
register(id, body) click to toggle source

Registers a new item in the dictionary.

@param [#to_s] id The ID of the item. @param [Object] body The item to register.

@return [self] The dictionary.

# File lib/discorb/dictionary.rb, line 29
def register(id, body)
  @cache[id.to_s] = body
  @cache = @cache.sort_by(&@sort).to_h if @sort
  @cache.delete(@cache.keys[0]) if !@limit.nil? && @cache.size > @limit
  body
end
Also aliased as: []=
remove(id) click to toggle source

Removes an item from the dictionary.

@param [#to_s] id The ID of the item to remove.

# File lib/discorb/dictionary.rb, line 50
def remove(id)
  @cache.remove(id.to_s)
end
respond_to_missing?(name, args, kwargs) click to toggle source
Calls superclass method
# File lib/discorb/dictionary.rb, line 107
def respond_to_missing?(name, args, kwargs)
  if values.respond_to?(name)
    true
  else
    super
  end
end
values() click to toggle source

Returns the values of the dictionary.

@return [Array] The values of the dictionary.

# File lib/discorb/dictionary.rb, line 81
def values
  @cache.values
end