Class: Discorb::Dictionary
- Inherits:
-
Object
- Object
- Discorb::Dictionary
- Defined in:
- lib/discorb/dictionary.rb
Instance Attribute Summary collapse
-
#limit -> Integer
The maximum number of items in the dictionary.
Instance Method Summary collapse
-
#get(index) -> Object?
(also: #[])
Get an item from the dictionary.
-
#has?(id) -> Boolean
Checks if the dictionary has an ID.
-
#initialize(hash = {}, limit: nil, sort: false) -> Dictionary
constructor
Initialize a new Dictionary.
- #inspect -> Object
-
#merge(other) -> Object
Merges another dictionary into this one.
-
#method_missing(name) -> Object
Send a message to the array of values.
-
#register(id, body) -> self
(also: #[]=)
Registers a new item in the dictionary.
-
#remove(id) -> Object
Removes an item from the dictionary.
- #respond_to_missing?(name) -> Boolean
-
#to_h -> Object
Convert the dictionary to a hash.
-
#values -> Array
Returns the values of the dictionary.
Constructor Details
#initialize(hash = {}, limit: nil, sort: false) -> Dictionary
Initialize a new Dictionary.
15 16 17 18 19 20 |
# File 'lib/discorb/dictionary.rb', line 15 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.
107 108 109 110 111 112 113 |
# File 'lib/discorb/dictionary.rb', line 107 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.
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.
68 69 70 71 72 73 74 75 |
# File 'lib/discorb/dictionary.rb', line 68 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.
100 101 102 |
# File 'lib/discorb/dictionary.rb', line 100 def has?(id) !self[id].nil? end |
#inspect -> Object
126 127 128 |
# File 'lib/discorb/dictionary.rb', line 126 def inspect "#<#{self.class} #{values.length} items>" end |
#merge(other) -> Object
Merges another dictionary into this one.
42 43 44 |
# File 'lib/discorb/dictionary.rb', line 42 def merge(other) @cache.merge!(other.to_h) end |
#register(id, body) -> self Also known as: []=
Registers a new item in the dictionary.
30 31 32 33 34 35 |
# File 'lib/discorb/dictionary.rb', line 30 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.
51 52 53 |
# File 'lib/discorb/dictionary.rb', line 51 def remove(id) @cache.delete(id.to_s) end |
#respond_to_missing?(name) -> Boolean
115 116 117 118 119 120 121 |
# File 'lib/discorb/dictionary.rb', line 115 def respond_to_missing?(name, ...) if values.respond_to?(name) true else super end end |
#to_h -> Object
Convert the dictionary to a hash.
80 81 82 |
# File 'lib/discorb/dictionary.rb', line 80 def to_h @cache end |
#values -> Array
Returns the values of the dictionary.
89 90 91 |
# File 'lib/discorb/dictionary.rb', line 89 def values @cache.values end |