Class: Serega::SeregaValidations::Attribute::CheckBlock

Inherits:
Object
  • Object
show all
Defined in:
lib/serega/validations/attribute/check_block.rb

Overview

Attribute block parameter validator

Constant Summary collapse

ERROR_MESSAGE =

Explains the changed attribute block behavior. Shown when the block looks like an old-style value block — it accepts parameters or defines no attributes.

"Attribute block now defines a nested serializer:" \
" it is executed in the context of a new serializer class and must define its attributes." \
" Defining the attribute value with a block is not supported anymore," \
" use the `value: <callable>` option instead."

Class Method Summary collapse

Class Method Details

.call(block) ⇒ void

This method returns an undefined value.

Checks block parameter provided with attribute.

The block defines attributes of a nested anonymous serializer, so it is executed in the context of that serializer and must accept no parameters.

Examples:

attribute :statistics, method: :itself do
  attribute :likes_count
  attribute :comments_count
end

Parameters:

  • block (Proc)

    Block that defines nested serializer attributes

Raises:

  • (SeregaError)

    SeregaError that block has invalid arguments



45
46
47
48
49
50
# File 'lib/serega/validations/attribute/check_block.rb', line 45

def call(block)
  return unless block

  signature = SeregaUtils::MethodSignature.call(block, pos_limit: 0)
  raise SeregaError, ERROR_MESSAGE unless signature == "0"
end