Module: Serega::SeregaPlugins::Presenter
- Defined in:
- lib/serega/plugins/presenter/presenter.rb
Overview
Plugin :presenter — moves computed attribute logic into a dedicated Presenter class.
Presenter inherits from SimpleDelegator:
-
All methods of the serialized object are available directly inside presenter methods.
-
Methods not defined on Presenter are resolved via method_missing on the first call and then defined as real delegators, so subsequent calls skip method_missing entirely.
-
The original object is accessible via __getobj__ (standard SimpleDelegator API).
-
The serialization context is accessible via the private method __ctx__.
class UserSerializer < Serega
plugin :presenter attribute :name attribute :role class Presenter def name [first_name, last_name].compact.join(' ') # first_name/last_name delegated to object end def role id == __ctx__[:current_user_id] ? :self : :other end endend
Defined Under Namespace
Modules: ClassMethods, SeregaObjectSerializerInstanceMethods Classes: Presenter
Class Method Summary collapse
-
.after_load_plugin(serializer_class, **_opts) ⇒ void
Runs callbacks after plugin was attached.
-
.load_plugin(serializer_class, **_opts) ⇒ void
Applies plugin code to specific serializer.
-
.plugin_name ⇒ Symbol
Plugin name.
Class Method Details
.after_load_plugin(serializer_class, **_opts) ⇒ void
This method returns an undefined value.
Runs callbacks after plugin was attached
61 62 63 64 65 |
# File 'lib/serega/plugins/presenter/presenter.rb', line 61 def self.after_load_plugin(serializer_class, **_opts) presenter_class = Class.new(Presenter) presenter_class.serializer_class = serializer_class serializer_class.const_set(:Presenter, presenter_class) end |
.load_plugin(serializer_class, **_opts) ⇒ void
This method returns an undefined value.
Applies plugin code to specific serializer
48 49 50 51 |
# File 'lib/serega/plugins/presenter/presenter.rb', line 48 def self.load_plugin(serializer_class, **_opts) serializer_class.extend(ClassMethods) serializer_class::SeregaObjectSerializer.include(SeregaObjectSerializerInstanceMethods) end |
.plugin_name ⇒ Symbol
Returns Plugin name.
36 37 38 |
# File 'lib/serega/plugins/presenter/presenter.rb', line 36 def self.plugin_name :presenter end |