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(*args) ⇒ 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(*args) ⇒ Object
Invoke the listener’s handler with the given arguments.
rubocop:disable Style/ArgumentsForwarding – anonymous * requires Ruby 3.2+
95 96 97 98 99 100 101 102 103 |
# File 'lib/lutaml/model/mapping/listener.rb', line 95 def call(*args) if simple? raise NoHandlerError, "Cannot call simple listener #{id.inspect}" end @handler.call(*args) # rubocop:enable Style/ArgumentsForwarding 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)
118 119 120 121 122 |
# File 'lib/lutaml/model/mapping/listener.rb', line 118 def eql?(other) other.is_a?(Listener) && id == other.id && target == other.target end |
#inspect ⇒ Object
108 109 110 111 |
# File 'lib/lutaml/model/mapping/listener.rb', line 108 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
113 114 115 |
# File 'lib/lutaml/model/mapping/listener.rb', line 113 def to_s inspect end |