Class: DiscordRDA::Entity Abstract
- Inherits:
-
Object
- Object
- DiscordRDA::Entity
- Defined in:
- lib/discord_rda/entity/base.rb
Overview
Subclass and implement attributes
Base class for all Discord entities. Entities are immutable data objects representing Discord API resources.
Direct Known Subclasses
Application, ApplicationCommand, Attachment, AuditLogEntry, AutoModerationRule, Channel, Embed, EmbedAuthor, EmbedField, EmbedFooter, EmbedImage, EmbedProvider, EmbedThumbnail, EmbedVideo, Emoji, Entitlement, Guild, GuildScheduledEvent, Integration, Interaction, Member, Message, Presence, Role, StageInstance, Sticker, Team, User, VoiceServer, VoiceState, Webhook
Instance Attribute Summary collapse
-
#id ⇒ Snowflake
readonly
The entity’s unique ID.
Class Method Summary collapse
-
.attribute(name, type: nil, default: nil) ⇒ Object
Define an attribute with type coercion.
-
.from_hash(data) ⇒ Entity
Create an entity from API data.
Instance Method Summary collapse
-
#==(other) ⇒ Boolean
(also: #eql?)
Check equality with another entity.
-
#created_at ⇒ Time?
Get creation time from snowflake ID.
-
#hash ⇒ Integer
Get hash code based on ID.
-
#initialize(data = {}) ⇒ Entity
constructor
Initialize entity with data.
-
#inspect ⇒ String
Inspect the entity.
-
#to_h ⇒ Hash
Get raw API data.
-
#to_json(*args) ⇒ String
Convert to JSON string.
Constructor Details
#initialize(data = {}) ⇒ Entity
Initialize entity with data
53 54 55 56 57 58 59 60 61 62 63 64 |
# File 'lib/discord_rda/entity/base.rb', line 53 def initialize(data = {}) normalized_data = data.each_with_object({}) do |(key, value), hash| hash[key.to_s] = value end normalized_data.each do |key, value| instance_variable_set("@#{key}", value) end @id = normalized_data['id'] ? Snowflake.new(normalized_data['id']) : nil @raw_data = normalized_data.freeze end |
Instance Attribute Details
#id ⇒ Snowflake (readonly)
Returns The entity’s unique ID.
11 12 13 |
# File 'lib/discord_rda/entity/base.rb', line 11 def id @id end |
Class Method Details
.attribute(name, type: nil, default: nil) ⇒ Object
Define an attribute with type coercion
18 19 20 21 22 23 24 25 26 |
# File 'lib/discord_rda/entity/base.rb', line 18 def attribute(name, type: nil, default: nil) define_method(name) do value = instance_variable_get("@#{name}") return default if value.nil? return value if type.nil? coerce_value(value, type) end end |
.from_hash(data) ⇒ Entity
Create an entity from API data
31 32 33 |
# File 'lib/discord_rda/entity/base.rb', line 31 def from_hash(data) new(data) end |
Instance Method Details
#==(other) ⇒ Boolean Also known as: eql?
Check equality with another entity
87 88 89 |
# File 'lib/discord_rda/entity/base.rb', line 87 def ==(other) other.is_a?(self.class) && @id == other.id end |
#created_at ⇒ Time?
Get creation time from snowflake ID
80 81 82 |
# File 'lib/discord_rda/entity/base.rb', line 80 def created_at @id&. end |
#hash ⇒ Integer
Get hash code based on ID
94 95 96 |
# File 'lib/discord_rda/entity/base.rb', line 94 def hash @id.hash end |
#inspect ⇒ String
Inspect the entity
100 101 102 |
# File 'lib/discord_rda/entity/base.rb', line 100 def inspect "#<#{self.class.name} id=#{@id}>" end |
#to_h ⇒ Hash
Get raw API data
68 69 70 |
# File 'lib/discord_rda/entity/base.rb', line 68 def to_h @raw_data end |
#to_json(*args) ⇒ String
Convert to JSON string
74 75 76 |
# File 'lib/discord_rda/entity/base.rb', line 74 def to_json(*args) @raw_data.to_json(*args) end |