module Surrealist::VarsHelper
Module for finding and setting hash into vars
Constants
- CLASS_VARIABLE
Class variable name that is set by
SchemaDefiner
- DEFAULT_TAG
Tag for default behaviour in multiple serializers
- INSTANCE_VARIABLE
Instance variable name that is set by
SchemaDefiner
- PARENT_VARIABLE
Instance's parent instance variable name that is set by
SchemaDefiner
- ROM_REGEXP
Regexp to resolve ROM structure
- SERIALIZER_CLASSES
Instance variable that keeps serializer classes
Public Class Methods
Sets a serializer for class
@param [Class] self_class class of object that points to serializer @param [Class] serializer_class class of serializer @param [Symbol] tag a tag associated with serializer
# File lib/surrealist/vars_helper.rb, line 64 def add_serializer(self_class, serializer_class, tag: nil) tag ||= DEFAULT_TAG hash = self_class.instance_variable_get(SERIALIZER_CLASSES) || {} hash[tag.to_sym] = serializer_class self_class.instance_variable_set(SERIALIZER_CLASSES, hash) end
Find the schema
@param [Class] klass Class that included Surrealist
@return [Hash] Found hash
# File lib/surrealist/vars_helper.rb, line 25 def find_schema(klass) if use_class_var?(klass) klass.class_variable_get(CLASS_VARIABLE) if klass.class_variable_defined?(CLASS_VARIABLE) else klass.instance_variable_get(INSTANCE_VARIABLE) end end
Checks if there is a serializer defined for a given class and returns it
@param [Class] klass a class to check @param [Symbol] tag a tag associated with serializer
@return [Class | nil]
# File lib/surrealist/vars_helper.rb, line 51 def find_serializer(klass, tag: nil) tag ||= DEFAULT_TAG hash = klass.instance_variable_get(SERIALIZER_CLASSES) serializer = hash&.fetch(tag.to_sym, nil) Surrealist::ExceptionRaiser.raise_unknown_tag!(tag) if serializer.nil? && tag != DEFAULT_TAG serializer end
Setting schema into var
@param [Class] klass Class that included Surrealist
@param [Hash] hash Schema hash
# File lib/surrealist/vars_helper.rb, line 37 def set_schema(klass, hash) if use_class_var?(klass) klass.class_variable_set(CLASS_VARIABLE, hash) else klass.instance_variable_set(INSTANCE_VARIABLE, hash) end end
Private Class Methods
# File lib/surrealist/vars_helper.rb, line 73 def use_class_var?(klass) klass.name =~ ROM_REGEXP end