Class: Async::Matrix::ApplicationService::Content
- Inherits:
-
Object
- Object
- Async::Matrix::ApplicationService::Content
- Defined in:
- lib/async/matrix/application_service/event.rb
Overview
Wraps the content object of a Matrix event, providing typed access to schema-defined properties via method_missing.
Common fields like msgtype, body, and membership have dedicated
accessors for convenience. Any other field defined in the event's schema
(or present in the raw hash) is accessible as a method call:
content.msgtype # => "m.text"
content.body # => "hello"
content.membership # => "join"
content.avatar_url # => "mxc://example.org/abc"
content["custom"] # => direct hash access for non-standard fields
Instance Attribute Summary collapse
-
#body ⇒ Object
readonly
Returns the value of attribute body.
-
#membership ⇒ Object
readonly
Returns the value of attribute membership.
-
#msgtype ⇒ Object
readonly
Returns the value of attribute msgtype.
Instance Method Summary collapse
-
#[](key) ⇒ Object
Direct hash access for any content field.
-
#initialize(data) ⇒ Content
constructor
A new instance of Content.
-
#method_missing(name, *args) ⇒ Object
Dynamic access to any content field present in the raw data.
- #respond_to_missing?(name, include_private = false) ⇒ Boolean
- #to_h ⇒ Object
- #to_s ⇒ Object
- #to_str ⇒ Object
Constructor Details
#initialize(data) ⇒ Content
Returns a new instance of Content.
25 26 27 28 29 30 |
# File 'lib/async/matrix/application_service/event.rb', line 25 def initialize(data) @data = data @msgtype = data["msgtype"] @body = data["body"] @membership = data["membership"] end |
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(name, *args) ⇒ Object
Dynamic access to any content field present in the raw data.
40 41 42 43 44 45 46 47 |
# File 'lib/async/matrix/application_service/event.rb', line 40 def method_missing(name, *args) key = name.to_s if @data.key?(key) @data[key] else nil end end |
Instance Attribute Details
#body ⇒ Object (readonly)
Returns the value of attribute body.
23 24 25 |
# File 'lib/async/matrix/application_service/event.rb', line 23 def body @body end |
#membership ⇒ Object (readonly)
Returns the value of attribute membership.
23 24 25 |
# File 'lib/async/matrix/application_service/event.rb', line 23 def membership @membership end |
#msgtype ⇒ Object (readonly)
Returns the value of attribute msgtype.
23 24 25 |
# File 'lib/async/matrix/application_service/event.rb', line 23 def msgtype @msgtype end |
Instance Method Details
#[](key) ⇒ Object
Direct hash access for any content field.
37 |
# File 'lib/async/matrix/application_service/event.rb', line 37 def [](key) = @data[key.to_s] |
#respond_to_missing?(name, include_private = false) ⇒ Boolean
49 50 51 |
# File 'lib/async/matrix/application_service/event.rb', line 49 def respond_to_missing?(name, include_private = false) @data.key?(name.to_s) || super end |
#to_h ⇒ Object
32 |
# File 'lib/async/matrix/application_service/event.rb', line 32 def to_h = @data |
#to_s ⇒ Object
33 |
# File 'lib/async/matrix/application_service/event.rb', line 33 def to_s = @body.to_s |
#to_str ⇒ Object
34 |
# File 'lib/async/matrix/application_service/event.rb', line 34 def to_str = to_s |