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
ApplicationCommand, Attachment, AutoModerationRule, Channel, Embed, EmbedAuthor, EmbedField, EmbedFooter, EmbedImage, EmbedProvider, EmbedThumbnail, EmbedVideo, Emoji, Guild, GuildScheduledEvent, Interaction, Member, Message, Role, Sticker, User
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
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
80 81 82 |
# File 'lib/discord_rda/entity/base.rb', line 80 def ==(other) other.is_a?(self.class) && @id == other.id end |
#created_at ⇒ Time?
Get creation time from snowflake ID
73 74 75 |
# File 'lib/discord_rda/entity/base.rb', line 73 def created_at @id&. end |
#hash ⇒ Integer
Get hash code based on ID
87 88 89 |
# File 'lib/discord_rda/entity/base.rb', line 87 def hash @id.hash end |
#inspect ⇒ String
Inspect the entity
93 94 95 |
# File 'lib/discord_rda/entity/base.rb', line 93 def inspect "#<#{self.class.name} id=#{@id}>" end |
#to_h ⇒ Hash
Get raw API data
61 62 63 |
# File 'lib/discord_rda/entity/base.rb', line 61 def to_h @raw_data end |
#to_json(*args) ⇒ String
Convert to JSON string
67 68 69 |
# File 'lib/discord_rda/entity/base.rb', line 67 def to_json(*args) @raw_data.to_json(*args) end |