Module: Serega::SeregaPlugins::If
- Defined in:
- lib/serega/plugins/if/if.rb,
lib/serega/plugins/if/validations/check_opt_if.rb,
lib/serega/plugins/if/validations/check_opt_unless.rb,
lib/serega/plugins/if/validations/check_opt_if_value.rb,
lib/serega/plugins/if/validations/check_opt_unless_value.rb
Overview
Plugin adds :if, :unless, :if_value, :unless_value options to
attributes so we can remove attributes from response in various ways.
Use :if and :unless when you want to hide attributes before finding attribute value,
and use :if_value and :unless_value to hide attributes after we find final value.
Options :if and :unless accept currently serialized object and context as parameters.
Options :if_value and :unless_value accept already found serialized value and context as parameters.
Options :if_value and :unless_value cannot be used with :serializer option, as
serialized objects have no "serialized value". Use :if and :unless in this case.
See also a :hide option that is available without any plugins to hide
attribute without conditions. Look at README.md#selecting-fields for :hide usage examples.
Examples: class UserSerializer < Serega attribute :email, if: :active? # if user.active? attribute :email, if: proc {|user| user.active?} # same attribute :email, if: proc {|user, ctx| user == ctx[:current_user]} # using context attribute :email, if: CustomPolicy.method(:view_email?) # You can provide own callable object
attribute :email, unless: :hidden? # unless user.hidden?
attribute :email, unless: proc {|user| user.hidden?} # same
attribute :email, unless: proc {|user, context| context[:show_emails]} # using context
attribute :email, unless: CustomPolicy.method(:hide_email?) # You can provide own callable object
attribute :email, if_value: :present? # if email.present?
attribute :email, if_value: proc {|email| email.present?} # same
attribute :email, if_value: proc {|email, ctx| ctx[:show_emails]} # using context
attribute :email, if_value: CustomPolicy.method(:view_email?) # You can provide own callable object
attribute :email, unless_value: :blank? # unless email.blank?
attribute :email, unless_value: proc {|email| email.blank?} # same
attribute :email, unless_value: proc {|email, context| context[:show_emails]} # using context
attribute :email, unless_value: CustomPolicy.method(:hide_email?) # You can provide own callable object
end
Defined Under Namespace
Modules: AttributeInstanceMethods, AttributeNormalizerInstanceMethods, CheckAttributeParamsInstanceMethods, DataBuilderClassMethods, ObjectSerializerInstanceMethods, PlanPointInstanceMethods Classes: CheckOptIf, CheckOptIfValue, CheckOptUnless, CheckOptUnlessValue, KeywordConditionResolver
Constant Summary collapse
- KEY_SKIPPED =
This value must be returned to identify that serialization key was skipped
:_key_skipped_with_serega_if_plugin
Class Method Summary collapse
-
.after_load_plugin(serializer_class, **opts) ⇒ void
Adds config options and runs other callbacks after plugin was loaded.
-
.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.
Adds config options and runs other callbacks after plugin was loaded
83 84 85 |
# File 'lib/serega/plugins/if/if.rb', line 83 def self.after_load_plugin(serializer_class, **opts) serializer_class.config.attribute_keys << :if << :if_value << :unless << :unless_value end |
.load_plugin(serializer_class, **_opts) ⇒ void
This method returns an undefined value.
Applies plugin code to specific serializer
61 62 63 64 65 66 67 68 69 70 71 72 73 |
# File 'lib/serega/plugins/if/if.rb', line 61 def self.load_plugin(serializer_class, **_opts) require_relative "validations/check_opt_if" require_relative "validations/check_opt_if_value" require_relative "validations/check_opt_unless" require_relative "validations/check_opt_unless_value" serializer_class::SeregaAttribute.include(AttributeInstanceMethods) serializer_class::SeregaAttributeNormalizer.include(AttributeNormalizerInstanceMethods) serializer_class::SeregaPlanPoint.include(PlanPointInstanceMethods) serializer_class::CheckAttributeParams.include(CheckAttributeParamsInstanceMethods) serializer_class::SeregaObjectSerializer.include(ObjectSerializerInstanceMethods) serializer_class::SeregaDataBuilder.extend(DataBuilderClassMethods) end |
.plugin_name ⇒ Symbol
Returns Plugin name.
49 50 51 |
# File 'lib/serega/plugins/if/if.rb', line 49 def self.plugin_name :if end |