Class: Discorb::Dictionary

Inherits:
Object
  • Object
show all
Defined in:
lib/discorb/dictionary.rb

Overview

Extended hash class. This is used for storing pair of ID and object.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(hash = {}, limit: nil, sort: false) -> Dictionary

Initialize a new Dictionary.

Parameters:

  • hash (Hash) (defaults to: {})

    A hash of items to add to the dictionary.

  • limit (Integer) (defaults to: nil)

    The maximum number of items in the dictionary.

  • sort (false, Proc) (defaults to: false)

    Whether to sort the items in the dictionary.



18
19
20
21
22
23
# File 'lib/discorb/dictionary.rb', line 18

def initialize(hash = {}, limit: nil, sort: false)
  @cache = hash.transform_keys(&:to_s)
  @limit = limit
  @sort = sort
  @cache = @cache.sort_by(&@sort).to_h if @sort
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(name) -> Object

Send a message to the array of values.



112
113
114
115
116
117
118
# File 'lib/discorb/dictionary.rb', line 112

def method_missing(name, ...)
  if values.respond_to?(name)
    values.send(name, ...)
  else
    super
  end
end

Instance Attribute Details

#limit -> Integer

Returns The maximum number of items in the dictionary.

Returns:

  • (Integer)

    The maximum number of items in the dictionary.



9
10
11
# File 'lib/discorb/dictionary.rb', line 9

def limit
  @limit
end

Instance Method Details

#get(index) -> Object? Also known as: []

Get an item from the dictionary.

Parameters:

  • index (Integer)

    The index of the item.

Returns:

  • (Object)

    The item.

  • (nil)

    if the item is not found.

Parameters:

  • id (#to_s)

    The ID of the item.

Returns:

  • (Object)

    The item.

  • (nil)

    if the item was not found.



73
74
75
76
77
78
79
80
# File 'lib/discorb/dictionary.rb', line 73

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

#has?(id) -> Boolean

Checks if the dictionary has an ID.

Parameters:

  • id (#to_s)

    The ID to check.

Returns:

  • (Boolean)

    true if the dictionary has the ID, false otherwise.



105
106
107
# File 'lib/discorb/dictionary.rb', line 105

def has?(id)
  !self[id].nil?
end

#inspect -> Object



131
132
133
# File 'lib/discorb/dictionary.rb', line 131

def inspect
  "#<#{self.class} #{values.length} items>"
end

#merge(other) -> Object

Merges another dictionary into this one.

Parameters:



45
46
47
# File 'lib/discorb/dictionary.rb', line 45

def merge(other)
  @cache.merge!(other.to_h)
end

#register(id, body) -> self Also known as: []=

Registers a new item in the dictionary.

Parameters:

  • id (#to_s)

    The ID of the item.

  • body (Object)

    The item to register.

Returns:

  • (self)

    The dictionary.



33
34
35
36
37
38
# File 'lib/discorb/dictionary.rb', line 33

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

#remove(id) -> Object Also known as: delete

Removes an item from the dictionary.

Parameters:

  • id (#to_s)

    The ID of the item to remove.



54
55
56
# File 'lib/discorb/dictionary.rb', line 54

def remove(id)
  @cache.delete(id.to_s)
end

#respond_to_missing?(name) -> Boolean

Returns:

  • (Boolean)


120
121
122
123
124
125
126
# File 'lib/discorb/dictionary.rb', line 120

def respond_to_missing?(name, ...)
  if values.respond_to?(name)
    true
  else
    super
  end
end

#to_h -> Object

Convert the dictionary to a hash.



85
86
87
# File 'lib/discorb/dictionary.rb', line 85

def to_h
  @cache
end

#values -> Array

Returns the values of the dictionary.

Returns:

  • (Array)

    The values of the dictionary.



94
95
96
# File 'lib/discorb/dictionary.rb', line 94

def values
  @cache.values
end