Module: Surrealist::VarsHelper

Defined in:
lib/surrealist/vars_helper.rb

Overview

Module for finding and setting hash into vars

Constant Summary collapse

INSTANCE_VARIABLE =

Instance variable name that is set by SchemaDefiner

'@__surrealist_schema'
PARENT_VARIABLE =

Instance’s parent instance variable name that is set by SchemaDefiner

'@__surrealist_schema_parent'
CLASS_VARIABLE =

Class variable name that is set by SchemaDefiner

'@@__surrealist_schema'
ROM_REGEXP =

Regexp to resolve ROM structure

/ROM::Struct/o.freeze
SERIALIZER_CLASSES =

Instance variable that keeps serializer classes

'@__surrealist_serializers'
DEFAULT_TAG =

Tag for default behaviour in multiple serializers

:default

Class Method Summary collapse

Class Method Details

.add_serializer(self_class, serializer_class, tag: nil) ⇒ Object

Sets a serializer for class

Parameters:

  • self_class (Class)

    class of object that points to serializer

  • serializer_class (Class)

    class of serializer

  • tag (Symbol) (defaults to: nil)

    a tag associated with serializer



64
65
66
67
68
69
# 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_schema(klass) ⇒ Hash

Find the schema

Parameters:

  • klass (Class)

    Class that included Surrealist

Returns:

  • (Hash)

    Found hash



25
26
27
28
29
30
31
# 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

.find_serializer(klass, tag: nil) ⇒ Class | nil

Checks if there is a serializer defined for a given class and returns it

Parameters:

  • klass (Class)

    a class to check

  • tag (Symbol) (defaults to: nil)

    a tag associated with serializer

Returns:

  • (Class | nil)


51
52
53
54
55
56
57
# 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

.set_schema(klass, hash) ⇒ Object

Setting schema into var

Parameters:

  • klass (Class)

    Class that included Surrealist

  • hash (Hash)

    Schema hash



37
38
39
40
41
42
43
# 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