Class: Serega::SeregaPlugins::Metadata::MetaAttribute::CheckBlock

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

Overview

Validator for meta_attribute block parameter

Class Method Summary collapse

Class Method Details

.call(block) ⇒ void

This method returns an undefined value.

Checks block provided with attribute Block must have up to two arguments - object and context. It should not have any *rest or **key arguments

Examples:

without arguments

(:version) { CONSTANT_VERSION }

with one argument

metadata(:paging) { |scope| { { page: scope.page, per_page: scope.per_page, total_count: scope.total_count } }

with two arguments

metadata(:paging) { |scope, context| { { ... } if context[:with_paging] }

Parameters:

  • block (Proc)

    Block that returns serialized meta attribute value

Raises:

  • (SeregaError)

    SeregaError that block has invalid arguments



35
36
37
38
39
40
41
42
# File 'lib/serega/plugins/metadata/validations/check_block.rb', line 35

def call(block)
  raise SeregaError, "Block must be provided when defining meta attribute" unless block

  params = block.parameters
  return if (params.count <= 2) && params.all? { |par| ALLOWED_PARAM_TYPES.include?(par[0]) }

  raise SeregaError, "Block can have maximum 2 regular parameters (no **keyword or *array args)"
end