Module: Surrealist
- Defined in:
- lib/surrealist.rb,
lib/surrealist/copier.rb,
lib/surrealist/helper.rb,
lib/surrealist/builder.rb,
lib/surrealist/carrier.rb,
lib/surrealist/version.rb,
lib/surrealist/wrapper.rb,
lib/surrealist/hash_utils.rb,
lib/surrealist/serializer.rb,
lib/surrealist/type_helper.rb,
lib/surrealist/vars_helper.rb,
lib/surrealist/string_utils.rb,
lib/surrealist/class_methods.rb,
lib/surrealist/schema_definer.rb,
lib/surrealist/value_assigner.rb,
lib/surrealist/exception_raiser.rb,
lib/surrealist/instance_methods.rb
Overview
Main module that provides the json_schema
class method and surrealize
instance method.
Defined Under Namespace
Modules: ClassMethods, Copier, ExceptionRaiser, HashUtils, Helper, InstanceMethods, SchemaDefiner, StringUtils, TypeHelper, ValueAssigner, VarsHelper, Wrapper Classes: Builder, Carrier, InvalidCollectionError, InvalidNestingLevel, InvalidSchemaDelegation, InvalidSchemaError, InvalidTypeError, Serializer, UndefinedMethodError, UnknownRootError, UnknownSchemaError, UnknownTagError
Constant Summary collapse
- DEFAULT_NESTING_LEVEL =
Default namespaces nesting level
666
- VERSION =
Defines the version of Surrealist
'2.0.0'
Class Method Summary collapse
-
.build_schema(instance:, **args) ⇒ Hash
Builds hash from schema provided in the object’s class and type-checks the values.
-
.config ⇒ Hash
Reads current default serialization arguments.
-
.configure(hash = nil, &_block) ⇒ Object
Sets default serialization arguments with a block.
- .included(base) ⇒ Object
-
.surrealize(instance:, **args) ⇒ String
Dumps the object’s methods corresponding to the schema provided in the object’s class and type-checks the values.
-
.surrealize_collection(collection, **args) ⇒ JSON | Hash
Iterates over a collection of Surrealist Objects and maps surrealize to each object.
Class Method Details
.build_schema(instance:, **args) ⇒ Hash
Builds hash from schema provided in the object’s class and type-checks the values.
127 128 129 130 131 132 133 134 135 136 137 138 139 |
# File 'lib/surrealist.rb', line 127 def build_schema(instance:, **args) schema = Surrealist::VarsHelper.find_schema(instance.class) Surrealist::ExceptionRaiser.raise_unknown_schema!(instance) if schema.nil? parameters = config ? config.merge(**args) : args # TODO: Refactor (something pipeline-like would do here, perhaps a builder of some sort) carrier = Surrealist::Carrier.call(**parameters) copied_schema = Surrealist::Copier.deep_copy(schema) built_schema = Builder.new(carrier, copied_schema, instance).call wrapped_schema = Surrealist::Wrapper.wrap(built_schema, carrier, klass: instance.class.name) carrier.camelize ? Surrealist::HashUtils.camelize_hash(wrapped_schema) : wrapped_schema end |
.config ⇒ Hash
Reads current default serialization arguments.
145 146 147 |
# File 'lib/surrealist.rb', line 145 def config @default_args || Surrealist::HashUtils::EMPTY_HASH end |
.configure(hash = nil, &_block) ⇒ Object
Sets default serialization arguments with a block
159 160 161 162 163 164 165 166 167 |
# File 'lib/surrealist.rb', line 159 def configure(hash = nil, &_block) if block_given? carrier = Surrealist::Carrier.new yield(carrier) @default_args = carrier.parameters else @default_args = hash.nil? ? Surrealist::HashUtils::EMPTY_HASH : hash end end |
.included(base) ⇒ Object
30 31 32 33 |
# File 'lib/surrealist.rb', line 30 def included(base) base.extend(Surrealist::ClassMethods) base.include(Surrealist::InstanceMethods) end |
.surrealize(instance:, **args) ⇒ String
Dumps the object’s methods corresponding to the schema provided in the object’s class and type-checks the values.
82 83 84 |
# File 'lib/surrealist.rb', line 82 def surrealize(instance:, **args) Oj.dump(build_schema(instance: instance, **args), mode: :compat) end |
.surrealize_collection(collection, **args) ⇒ JSON | Hash
Iterates over a collection of Surrealist Objects and maps surrealize to each object.
58 59 60 61 62 63 64 65 66 |
# File 'lib/surrealist.rb', line 58 def surrealize_collection(collection, **args) Surrealist::ExceptionRaiser.raise_invalid_collection! unless Helper.collection?(collection) result = collection.map do |object| Helper.surrealist?(object.class) ? __build_schema(object, **args) : object end args[:raw] ? result : Oj.dump(result, mode: :compat) end |