Class: Surrealist::Serializer
- Inherits:
-
Object
- Object
- Surrealist::Serializer
- Extended by:
- ClassMethods
- Defined in:
- lib/surrealist/serializer.rb
Overview
Abstract class to be inherited from
Class Method Summary collapse
-
.method_defined?(method) ⇒ Boolean
Only lookup for methods defined in Surrealist::Serializer subclasses to prevent invoke of Kernel methods.
- .private_method_defined?(method) ⇒ Boolean
-
.serializer_context(*array) ⇒ Object
(also: serializer_contexts)
Defines instance methods that read values from the context hash.
Instance Method Summary collapse
-
#build_schema(**args) ⇒ Object
Passes build_schema to Surrealist.
-
#initialize(object, **context) ⇒ Serializer
constructor
NOTE: #context will work only when using serializer explicitly, e.g ‘CatSerializer.new(Cat.new(3), food: CatFood.new)` And then food will be available inside serializer via `context`.
-
#surrealize(**args) ⇒ Object
Checks whether object is a collection or an instance and serializes it.
Methods included from ClassMethods
defined_schema, delegate_surrealization_to, json_schema, surrealize_with
Constructor Details
#initialize(object, **context) ⇒ Serializer
NOTE: #context will work only when using serializer explicitly,
e.g `CatSerializer.new(Cat.new(3), food: CatFood.new)`
And then food will be available inside serializer via `context[:food]`
76 77 78 79 |
# File 'lib/surrealist/serializer.rb', line 76 def initialize(object, **context) @object = wrap_hash_into_struct(object) @context = context end |
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(method, *args, &block) ⇒ Object (private)
Methods not found inside serializer will be invoked on the object
109 110 111 112 113 |
# File 'lib/surrealist/serializer.rb', line 109 def method_missing(method, *args, &block) return super unless object.respond_to?(method) object.public_send(method, *args, &block) end |
Class Method Details
.method_defined?(method) ⇒ Boolean
Only lookup for methods defined in Surrealist::Serializer subclasses
to prevent invoke of Kernel methods
58 59 60 61 62 63 |
# File 'lib/surrealist/serializer.rb', line 58 def method_defined?(method) return true if instance_methods(false).include?(method) return false if superclass == Surrealist::Serializer super end |
.private_method_defined?(method) ⇒ Boolean
65 66 67 68 69 70 |
# File 'lib/surrealist/serializer.rb', line 65 def private_method_defined?(method) return true if private_instance_methods(false).include?(method) return false if superclass == Surrealist::Serializer super end |
.serializer_context(*array) ⇒ Object Also known as: serializer_contexts
Defines instance methods that read values from the context hash.
41 42 43 44 45 46 47 |
# File 'lib/surrealist/serializer.rb', line 41 def serializer_context(*array) unless array.all? { |i| i.is_a? Symbol } raise ArgumentError, 'Please provide an array of symbols to `.serializer_context`' end array.each { |method| define_method(method) { context[method] } } end |
Instance Method Details
#build_schema(**args) ⇒ Object
Passes build_schema to Surrealist
91 92 93 94 95 96 97 |
# File 'lib/surrealist/serializer.rb', line 91 def build_schema(**args) if Helper.collection?(object) build_collection_schema(**args) else Surrealist.build_schema(instance: self, **args) end end |
#surrealize(**args) ⇒ Object
Checks whether object is a collection or an instance and serializes it
82 83 84 85 86 87 88 |
# File 'lib/surrealist/serializer.rb', line 82 def surrealize(**args) if Helper.collection?(object) Surrealist.surrealize_collection(object, **args.merge(context: context)) else Surrealist.surrealize(instance: self, **args) end end |