Module: SimpleJsonapi::SerializerMethods
- Included in:
- ErrorSerializer, Serializer
- Defined in:
- lib/simple_jsonapi/helpers/serializer_methods.rb
Instance Method Summary collapse
- #definition ⇒ Definition::Resource, Definition::Error
-
#with(**data) ⇒ Object
Adds the provided data values to the serializer as instance variables for the duration of the block.
Instance Method Details
#definition ⇒ Definition::Resource, Definition::Error
15 16 17 |
# File 'lib/simple_jsonapi/helpers/serializer_methods.rb', line 15 def definition self.class.definition end |
#with(**data) ⇒ Object
Adds the provided data values to the serializer as instance variables for the duration of the block.
21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 |
# File 'lib/simple_jsonapi/helpers/serializer_methods.rb', line 21 def with(**data) ivar_data = data.transform_keys { |key| :"@#{key}" } existing_keys = ivar_data.each_key.select { |key| instance_variable_defined?(key) } if existing_keys.any? raise ArgumentError, "Cannot override existing instance variables #{existing_keys.to_sentence}." end begin ivar_data.each { |k, v| instance_variable_set(k, v) } yield ensure ivar_data.each_key { |k| remove_instance_variable(k) } end end |