Class: Barley::Serializer
- Inherits:
-
Object
- Object
- Barley::Serializer
- Defined in:
- lib/barley/serializer.rb
Class Attribute Summary collapse
-
.defined_attributes ⇒ Object
Returns the value of attribute defined_attributes.
Instance Attribute Summary collapse
-
#context ⇒ Object
Returns the value of attribute context.
-
#object ⇒ Object
Returns the value of attribute object.
Class Method Summary collapse
-
.attribute(key, key_name: nil, type: nil, &block) ⇒ Object
Defines a single attribute for the serializer.
-
.attributes(*keys) ⇒ Object
Defines attributes for the serializer.
-
.many(key, key_name: nil, serializer: nil, cache: false, &block) ⇒ Object
Defines a collection association for the serializer.
-
.one(key, key_name: nil, serializer: nil, cache: false, &block) ⇒ Object
Defines a single association for the serializer.
Instance Method Summary collapse
-
#clear_cache(key: cache_base_key) ⇒ Boolean
Clears the cache for the object.
-
#initialize(object, cache: false, root: false) ⇒ Serializer
constructor
A new instance of Serializer.
-
#serializable_hash ⇒ Hash
Serializes the object.
-
#with_context(**args) ⇒ Barley::Serializer
Sets the context object for the serializer.
Constructor Details
#initialize(object, cache: false, root: false) ⇒ Serializer
Returns a new instance of Serializer.
196 197 198 199 200 201 202 203 204 |
# File 'lib/barley/serializer.rb', line 196 def initialize(object, cache: false, root: false) @object = object @root = root @cache, @expires_in = if cache.is_a?(Hash) [true, cache[:expires_in]] else [cache, nil] end end |
Class Attribute Details
.defined_attributes ⇒ Object
Returns the value of attribute defined_attributes.
9 10 11 |
# File 'lib/barley/serializer.rb', line 9 def defined_attributes @defined_attributes end |
Instance Attribute Details
#context ⇒ Object
Returns the value of attribute context.
6 7 8 |
# File 'lib/barley/serializer.rb', line 6 def context @context end |
#object ⇒ Object
Returns the value of attribute object.
5 6 7 |
# File 'lib/barley/serializer.rb', line 5 def object @object end |
Class Method Details
.attribute(key, key_name: nil, type: nil, &block) ⇒ Object
Defines a single attribute for the serializer
Type checking is done with Dry::Types. If a type is not provided, the value is returned as is. Dry::Types can be used to coerce the value to the desired type and to check constraints.
76 77 78 79 80 81 82 83 84 |
# File 'lib/barley/serializer.rb', line 76 def attribute(key, key_name: nil, type: nil, &block) key_name ||= key define_method(key_name) do value = block ? instance_eval(&block) : object.send(key) type.nil? ? value : type[value] end self.defined_attributes = (defined_attributes || []) << key_name end |
.attributes(*keys) ⇒ Object
Defines attributes for the serializer
Accepts either a list of symbols or a hash of symbols and Dry::Types, or a mix of both
30 31 32 33 34 35 36 37 38 39 40 41 42 43 |
# File 'lib/barley/serializer.rb', line 30 def attributes(*keys) if keys.last.is_a?(Hash) keys.pop.each do |key, type| attribute(key, type: type) end end keys.each do |key| if key.is_a?(Hash) attribute(key.keys.first, type: key.values.first) else attribute(key) end end end |
.many(key, key_name: nil, serializer: nil, cache: false, &block) ⇒ Object
Defines a collection association for the serializer
169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 |
# File 'lib/barley/serializer.rb', line 169 def many(key, key_name: nil, serializer: nil, cache: false, &block) key_name ||= key if block serializer = Class.new(Barley::Serializer) do instance_eval(&block) end end define_method(key_name) do elements = object.send(key) return [] if elements.empty? el_serializer = serializer || elements.first.serializer.class elements.map { |element| el_serializer.new(element, cache: cache).serializable_hash }.reject(&:blank?) end self.defined_attributes = (defined_attributes || []) << key_name end |
.one(key, key_name: nil, serializer: nil, cache: false, &block) ⇒ Object
Defines a single association for the serializer
119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 |
# File 'lib/barley/serializer.rb', line 119 def one(key, key_name: nil, serializer: nil, cache: false, &block) key_name ||= key if block serializer = Class.new(Barley::Serializer) do instance_eval(&block) end end define_method(key_name) do element = object.send(key) return {} if element.nil? el_serializer = serializer || element.serializer.class el_serializer.new(element, cache: cache).serializable_hash end self.defined_attributes = (defined_attributes || []) << key_name end |
Instance Method Details
#clear_cache(key: cache_base_key) ⇒ Boolean
Clears the cache for the object
224 225 226 |
# File 'lib/barley/serializer.rb', line 224 def clear_cache(key: cache_base_key) Barley::Cache.delete(key) end |
#serializable_hash ⇒ Hash
Serializes the object
209 210 211 212 213 214 215 216 217 |
# File 'lib/barley/serializer.rb', line 209 def serializable_hash if @cache Barley::Cache.fetch(cache_base_key, expires_in: @expires_in) do _serializable_hash end else _serializable_hash end end |
#with_context(**args) ⇒ Barley::Serializer
Sets the context object for the serializer
The context object is a Struct built from the given arguments. It can be used to pass additional data to the serializer. The context object is accessible in the serializer with the ‘context` attribute.
241 242 243 244 245 |
# File 'lib/barley/serializer.rb', line 241 def with_context(**args) @context = Struct.new(*args.keys).new(*args.values) self end |