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

Instance Method Details

#defined_schemaObject

A DSL method to return the defined schema.

Examples:

DSL usage example

class Person
  include Surrealist

  json_schema do
    { name: String }
  end

  def name
    'Parent'
  end
end

Person.defined_schema
# => { name: String }


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

Examples:

DSL usage example

class Host
  include Surrealist

  json_schema do
    { name: String }
  end

  def name
    'Parent'
  end
end

class Guest < Host
  delegate_surrealization_to Host

  def name
    'Child'
  end
end

Guest.new.surrealize
# => "{\"name\":\"Child\"}"
# For more examples see README

Parameters:

  • klass (Class)

Raises:

  • (TypeError)


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.

Examples:

DSL usage example

class User
  include Surrealist

  json_schema do
    {
      foo: String,
      bar: Integer,
    }
  end

  def foo; 'A string'; end
  def bar; 42; end
end

User.new.surrealize
# => "{\"foo\":\"A string\",\"bar\":42}"
# For more examples see README

Schema with nested structure

class Person
  include Surrealist

  json_schema do
    {
      foo: String,
      nested: {
        bar: Integer,
      }
    }
  end

  def foo; 'A string'; end
  def bar; 42; end
end

Person.new.surrealize
# => "{\"foo\":\"A string\",\"nested\":{\"bar\":42}}"
# For more examples see README

Parameters:

  • _block (Proc)

    that contains hash defining the schema



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.

Parameters:

  • klass (Class)

    a class that should inherit form Surrealist::Serializer

Raises:

  • ArgumentError if Surrealist::Serializer is not found in the ancestors chain



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