Class: DiscordRDA::Entity Abstract

Inherits:
Object
  • Object
show all
Defined in:
lib/discord_rda/entity/base.rb

Overview

This class is abstract.

Subclass and implement attributes

Base class for all Discord entities. Entities are immutable data objects representing Discord API resources.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(data = {}) ⇒ Entity

Initialize entity with data

Parameters:

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

    Entity data



53
54
55
56
57
# File 'lib/discord_rda/entity/base.rb', line 53

def initialize(data = {})
  @id = data['id'] ? Snowflake.new(data['id']) : nil
  @raw_data = data.freeze
  freeze
end

Instance Attribute Details

#idSnowflake (readonly)

Returns The entity’s unique ID.

Returns:



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

Parameters:

  • name (Symbol)

    Attribute name

  • type (Class, Proc) (defaults to: nil)

    Type or coercion function

  • default (Object) (defaults to: nil)

    Default value



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

Parameters:

  • data (Hash)

    Raw API data

Returns:

  • (Entity)

    New entity instance



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

Parameters:

  • other (Object)

    Object to compare

Returns:

  • (Boolean)

    True if IDs match



80
81
82
# File 'lib/discord_rda/entity/base.rb', line 80

def ==(other)
  other.is_a?(self.class) && @id == other.id
end

#created_atTime?

Get creation time from snowflake ID

Returns:

  • (Time, nil)

    Creation time or nil if no ID



73
74
75
# File 'lib/discord_rda/entity/base.rb', line 73

def created_at
  @id&.timestamp
end

#hashInteger

Get hash code based on ID

Returns:

  • (Integer)

    Hash code



87
88
89
# File 'lib/discord_rda/entity/base.rb', line 87

def hash
  @id.hash
end

#inspectString

Inspect the entity

Returns:

  • (String)

    Inspect string



93
94
95
# File 'lib/discord_rda/entity/base.rb', line 93

def inspect
  "#<#{self.class.name} id=#{@id}>"
end

#to_hHash

Get raw API data

Returns:

  • (Hash)

    The original data hash



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

Returns:

  • (String)

    JSON representation



67
68
69
# File 'lib/discord_rda/entity/base.rb', line 67

def to_json(*args)
  @raw_data.to_json(*args)
end