Module: Surrealist::ClassMethods
- Included in:
- Serializer
- Defined in:
- lib/surrealist/class_methods.rb
Overview
Class methods that are extended by the object.
Instance Method Summary collapse
-
#defined_schema ⇒ Object
A DSL method to return the defined schema.
-
#delegate_surrealization_to(klass) ⇒ Object
A DSL method to delegate schema in a declarative style.
-
#json_schema(&_block) ⇒ Object
A DSL method to define schema in a declarative style.
-
#surrealize_with(klass, tag: Surrealist::VarsHelper::DEFAULT_TAG) ⇒ Object
A DSL method for defining a class that holds serialization logic.
Instance Method Details
#defined_schema ⇒ Object
A DSL method to return the defined schema.
74 75 76 77 78 |
# File 'lib/surrealist/class_methods.rb', line 74 def defined_schema read_schema.tap do |schema| raise UnknownSchemaError if schema.nil? end end |
#delegate_surrealization_to(klass) ⇒ Object
A DSL method to delegate schema in a declarative style. Must reference a valid class that includes Surrealist
109 110 111 112 113 114 115 116 |
# File 'lib/surrealist/class_methods.rb', line 109 def delegate_surrealization_to(klass) raise TypeError, "Expected type of Class got #{klass.class} instead" unless klass.is_a?(Class) Surrealist::ExceptionRaiser.raise_invalid_schema_delegation! unless Helper.surrealist?(klass) hash = Surrealist::VarsHelper.find_schema(klass) Surrealist::VarsHelper.set_schema(self, hash) end |
#json_schema(&_block) ⇒ Object
A DSL method to define schema in a declarative style. Schema should be defined with a block that contains a hash. Every key of the schema should be either a name of a method of the surrealizable object (or it’s parents/mixins), or - in case value is a hash - a symbol: to build nested JSON structures. Every value of the hash should be a constant that represents a Ruby class, that will be used for type-checks.
54 55 56 |
# File 'lib/surrealist/class_methods.rb', line 54 def json_schema(&_block) SchemaDefiner.call(self, yield) end |
#surrealize_with(klass, tag: Surrealist::VarsHelper::DEFAULT_TAG) ⇒ Object
A DSL method for defining a class that holds serialization logic.
123 124 125 126 127 128 129 130 |
# File 'lib/surrealist/class_methods.rb', line 123 def surrealize_with(klass, tag: Surrealist::VarsHelper::DEFAULT_TAG) if klass < Surrealist::Serializer Surrealist::VarsHelper.add_serializer(self, klass, tag: tag) instance_variable_set(VarsHelper::PARENT_VARIABLE, klass.defined_schema) else raise ArgumentError, "#{klass} should be inherited from Surrealist::Serializer" end end |