Class: Cuprum::Rails::Serializers::Json::PropertiesSerializer
- Inherits:
-
BaseSerializer
- Object
- BaseSerializer
- Cuprum::Rails::Serializers::Json::PropertiesSerializer
- Defined in:
- lib/cuprum/rails/serializers/json/properties_serializer.rb
Overview
Generates a JSON representation of the object’s properties.
Defined properties are inherited from the parent serializer. This allows you to extend existing serializers with additional functionality.
Direct Known Subclasses
Defined Under Namespace
Classes: AbstractSerializerError, SerializedProperty
Class Method Summary collapse
-
.properties ⇒ Object
private
Enumerates the properties defined for the class and its ancestors.
-
.property(name, scope: nil, serializer: nil) {|value| ... } ⇒ Object
Registers the property to be serialized.
Instance Method Summary collapse
-
#call(object, context:) ⇒ Hash<String, Object] a JSON-compatible representation of the object's properties.
Serializes the object’s properties as JSON.
Methods inherited from BaseSerializer
Class Method Details
.properties ⇒ Object
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Enumerates the properties defined for the class and its ancestors.
151 152 153 154 155 156 157 158 |
# File 'lib/cuprum/rails/serializers/json/properties_serializer.rb', line 151 def properties ancestors .select do |ancestor| ancestor < Cuprum::Rails::Serializers::Json::PropertiesSerializer end # rubocop:disable Style/MultilineBlockChain .reverse_each .reduce({}) { |hsh, ancestor| hsh.merge(ancestor.own_properties) } end |
.property(name, scope: nil, serializer: nil) {|value| ... } ⇒ Object
Registers the property to be serialized.
125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 |
# File 'lib/cuprum/rails/serializers/json/properties_serializer.rb', line 125 def property(name, scope: nil, serializer: nil, &block) # rubocop:disable Metrics/MethodLength require_concrete_class! validate_property_name!(name) validate_scope!(scope) validate_serializer!(serializer) unless scope || serializer || block_given? raise ArgumentError, 'must provide a scope, a serializer, or a mapping block' end prop_key = name.intern serialized = SerializedProperty.new( mapping: block || :itself.to_proc, name: name.to_s, scope: scope, serializer: serializer ) own_properties[prop_key] = serialized prop_key end |
Instance Method Details
#call(object, context:) ⇒ Hash<String, Object] a JSON-compatible representation of the object's properties.
Serializes the object’s properties as JSON.
235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 |
# File 'lib/cuprum/rails/serializers/json/properties_serializer.rb', line 235 def call(object, context:) # rubocop:disable Metrics/MethodLength return super(nil, context: context) if object.nil? self.class.properties.each_value.with_object({}) do |property, hsh| value = property.value_for(object) mapped = property.mapping.call(value) if property.mapping serialized = if property.serializer property.serializer.call(mapped, context: context) else super(mapped, context: context) end hsh[property.name] = serialized end end |