Module: Serega::SeregaAttributeNormalizer::AttributeNormalizerInstanceMethods

Included in:
Serega::SeregaAttributeNormalizer
Defined in:
lib/serega/attribute_normalizer.rb

Overview

AttributeNormalizer instance methods

Constant Summary collapse

AUTO_BATCH_LOADER =

Identity loader that routes relation/preload attributes through the batch mechanism. Its result is never read — the attribute's value block still computes the value.

proc { |records| records.to_h { |record| [record, record] } }

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#init_blockHash (readonly)

Attribute initial params

Returns:

  • (Hash)

    Attribute initial params



18
19
20
# File 'lib/serega/attribute_normalizer.rb', line 18

def init_block
  @init_block
end

#init_nameHash (readonly)

Attribute initial params

Returns:

  • (Hash)

    Attribute initial params



18
19
20
# File 'lib/serega/attribute_normalizer.rb', line 18

def init_name
  @init_name
end

#init_optsHash (readonly)

Attribute initial params

Returns:

  • (Hash)

    Attribute initial params



18
19
20
# File 'lib/serega/attribute_normalizer.rb', line 18

def init_opts
  @init_opts
end

Instance Method Details

#batch_loadersArray<Symbol>

Shows specified batch loaders names

Returns:

  • (Array<Symbol>)

    specified serializer



129
130
131
# File 'lib/serega/attribute_normalizer.rb', line 129

def batch_loaders
  @batch_loaders ||= prepare_batch_loaders
end

#defaultObject

Shows the default attribute value. It is a value that replaces found nils.

When custom :default is not specified, we set empty array as default when many: true specified

Returns:

  • (Object)

    Attribute default value



109
110
111
112
113
# File 'lib/serega/attribute_normalizer.rb', line 109

def default
  return @default if instance_variable_defined?(:@default)

  @default = prepare_default
end

#hideBoolean?

Shows if attribute is specified to be hidden

Returns:

  • (Boolean, nil)

    if attribute must be hidden by default



75
76
77
78
79
# File 'lib/serega/attribute_normalizer.rb', line 75

def hide
  return @hide if instance_variable_defined?(:@hide)

  @hide = prepare_hide
end

#initialize(initials) ⇒ SeregaAttributeNormalizer

Instantiates attribute options normalizer

Parameters:

  • initials (Hash)

    new attribute options

Returns:



27
28
29
30
31
# File 'lib/serega/attribute_normalizer.rb', line 27

def initialize(initials)
  @init_name = initials[:name]
  @init_opts = initials[:opts]
  @init_block = initials[:block]
end

#manyBoolean?

Shows if attribute is specified to be a one-to-many relationship

Returns:

  • (Boolean, nil)

    if attribute is specified to be a one-to-many relationship



86
87
88
89
90
# File 'lib/serega/attribute_normalizer.rb', line 86

def many
  return @many if instance_variable_defined?(:@many)

  @many = prepare_many
end

#method_nameSymbol

Symbolized initial attribute method name

Returns:

  • (Symbol)

    Attribute normalized method name



47
48
49
# File 'lib/serega/attribute_normalizer.rb', line 47

def method_name
  @method_name ||= prepare_method_name
end

#nameSymbol

Symbolized initial attribute name

Returns:

  • (Symbol)

    Attribute normalized name



38
39
40
# File 'lib/serega/attribute_normalizer.rb', line 38

def name
  @name ||= prepare_name
end

#preloadsHash?

Shows normalized preloads for current attribute

Returns:

  • (Hash, nil)

    normalized preloads of current attribute



120
121
122
123
124
# File 'lib/serega/attribute_normalizer.rb', line 120

def preloads
  return @preloads if instance_variable_defined?(:@preloads)

  @preloads = prepare_preloads
end

#serializerSerega, ...

Shows specified attribute serializer

Returns:

  • (Serega, String, #callable, nil)

    specified serializer



96
97
98
99
100
# File 'lib/serega/attribute_normalizer.rb', line 96

def serializer
  return @serializer if instance_variable_defined?(:@serializer)

  @serializer = prepare_serializer
end

#value_block#call

Combines all options to return single block that will be used to find attribute value during serialization

Returns:

  • (#call)

    Attribute normalized callable value block



57
58
59
# File 'lib/serega/attribute_normalizer.rb', line 57

def value_block
  @value_block ||= prepare_value_block
end

#value_block_signatureString

Detects value block parameters signature

Returns:

  • (String)

    value block parameters signature



66
67
68
# File 'lib/serega/attribute_normalizer.rb', line 66

def value_block_signature
  @value_block_signature ||= SeregaUtils::MethodSignature.call(value_block, pos_limit: 2, keyword_args: [:ctx])
end