Class: Lutaml::Model::Listener
- Inherits:
-
Object
- Object
- Lutaml::Model::Listener
- Defined in:
- lib/lutaml/model/mapping/listener.rb
Overview
Base listener class for the listener-based mapping model.
Listeners are handlers that respond to format elements/keys (e.g., XML element names, JSON keys). Multiple listeners per element are allowed, and all matching listeners are invoked during parsing.
There are two types of listeners:
-
Simple listeners: Created via map_element/map_attribute DSL (no handler block). The framework handles the default deserialization behavior.
-
Complex listeners: Created via on_element/on_attribute DSL (with handler block). The block provides custom deserialization logic.
Direct Known Subclasses
Defined Under Namespace
Classes: NoHandlerError
Instance Attribute Summary collapse
-
#handler ⇒ Object
readonly
Returns the value of attribute handler.
-
#id ⇒ Object
readonly
Returns the value of attribute id.
-
#options ⇒ Object
readonly
Returns the value of attribute options.
-
#target ⇒ Object
readonly
Returns the value of attribute target.
Instance Method Summary collapse
-
#call ⇒ Object
Invoke the listener’s handler with the given arguments.
-
#complex? ⇒ Boolean
Returns true if this is a complex listener (has a custom handler).
-
#eql?(other) ⇒ Boolean
(also: #==)
Equality based on id and target (for deduplication).
-
#initialize(id:, target:, handler: nil, **options) ⇒ Listener
constructor
A new instance of Listener.
- #inspect ⇒ Object
-
#simple? ⇒ Boolean
Returns true if this is a simple listener (no custom handler).
- #to_s ⇒ Object
Constructor Details
#initialize(id:, target:, handler: nil, **options) ⇒ Listener
Returns a new instance of Listener.
67 68 69 70 71 72 73 |
# File 'lib/lutaml/model/mapping/listener.rb', line 67 def initialize(id:, target:, handler: nil, **) @id = id @target = target.to_s if target @handler = handler @options = .freeze freeze end |
Instance Attribute Details
#handler ⇒ Object (readonly)
Returns the value of attribute handler.
59 60 61 |
# File 'lib/lutaml/model/mapping/listener.rb', line 59 def handler @handler end |
#id ⇒ Object (readonly)
Returns the value of attribute id.
59 60 61 |
# File 'lib/lutaml/model/mapping/listener.rb', line 59 def id @id end |
#options ⇒ Object (readonly)
Returns the value of attribute options.
59 60 61 |
# File 'lib/lutaml/model/mapping/listener.rb', line 59 def @options end |
#target ⇒ Object (readonly)
Returns the value of attribute target.
59 60 61 |
# File 'lib/lutaml/model/mapping/listener.rb', line 59 def target @target end |
Instance Method Details
#call ⇒ Object
Invoke the listener’s handler with the given arguments.
94 95 96 97 98 99 100 101 |
# File 'lib/lutaml/model/mapping/listener.rb', line 94 def call(*) if simple? raise NoHandlerError, "Cannot call simple listener #{id.inspect}" end @handler.call(*) end |
#complex? ⇒ Boolean
Returns true if this is a complex listener (has a custom handler).
86 87 88 |
# File 'lib/lutaml/model/mapping/listener.rb', line 86 def complex? !simple? end |
#eql?(other) ⇒ Boolean Also known as: ==
Equality based on id and target (for deduplication)
116 117 118 119 120 |
# File 'lib/lutaml/model/mapping/listener.rb', line 116 def eql?(other) other.is_a?(Listener) && id == other.id && target == other.target end |
#inspect ⇒ Object
106 107 108 109 |
# File 'lib/lutaml/model/mapping/listener.rb', line 106 def inspect "#<#{self.class.name} id=#{id.inspect} target=#{target.inspect} " \ "simple=#{simple?}>" end |
#simple? ⇒ Boolean
Returns true if this is a simple listener (no custom handler). Simple listeners rely on framework default deserialization behavior.
79 80 81 |
# File 'lib/lutaml/model/mapping/listener.rb', line 79 def simple? @handler.nil? end |
#to_s ⇒ Object
111 112 113 |
# File 'lib/lutaml/model/mapping/listener.rb', line 111 def to_s inspect end |