Class: Discorb::Dictionary

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

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.



15
16
17
18
19
# 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

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.



99
100
101
102
103
104
105
# File 'lib/discorb/dictionary.rb', line 99

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.



6
7
8
# File 'lib/discorb/dictionary.rb', line 6

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.



67
68
69
70
71
72
73
74
# 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

#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.



92
93
94
# File 'lib/discorb/dictionary.rb', line 92

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

#inspect -> Object



118
119
120
# File 'lib/discorb/dictionary.rb', line 118

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

#merge(other) -> Object

Merges another dictionary into this one.

Parameters:



41
42
43
# File 'lib/discorb/dictionary.rb', line 41

def merge(other)
  @cache.merge!(other)
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.



29
30
31
32
33
34
# 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

#remove(id) -> Object

Removes an item from the dictionary.

Parameters:

  • id (#to_s)

    The ID of the item to remove.



50
51
52
# File 'lib/discorb/dictionary.rb', line 50

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

#respond_to_missing?(name, args, kwargs) -> Boolean

Returns:

  • (Boolean)


107
108
109
110
111
112
113
# 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 -> Array

Returns the values of the dictionary.

Returns:

  • (Array)

    The values of the dictionary.



81
82
83
# File 'lib/discorb/dictionary.rb', line 81

def values
  @cache.values
end