Module: Serega::SeregaAttribute::AttributeInstanceMethods

Included in:
Serega::SeregaAttribute
Defined in:
lib/serega/attribute.rb

Overview

Attribute instance methods

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#batch_loadersArray<Symbol> (readonly)

Batch loader names required to detect attribute value

Returns:

  • (Array<Symbol>)

    Batch loader names



42
43
44
# File 'lib/serega/attribute.rb', line 42

def batch_loaders
  @batch_loaders
end

#defaultObject? (readonly)

Attribute :default option

Returns:

  • (Object, nil)

    Attribute :default option



26
27
28
# File 'lib/serega/attribute.rb', line 26

def default
  @default
end

#hideBoolean? (readonly)

Attribute :hide option

Returns:

  • (Boolean, nil)

    Attribute :hide option



30
31
32
# File 'lib/serega/attribute.rb', line 30

def hide
  @hide
end

#initialsHash (readonly)

Attribute initial params

Returns:

  • (Hash)

    Attribute initial params



14
15
16
# File 'lib/serega/attribute.rb', line 14

def initials
  @initials
end

#manyBoolean? (readonly)

Attribute :many option

Returns:

  • (Boolean, nil)

    Attribute :many option



22
23
24
# File 'lib/serega/attribute.rb', line 22

def many
  @many
end

#nameSymbol (readonly)

Attribute name

Returns:

  • (Symbol)

    Attribute name



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

def name
  @name
end

#preloadsHash? (readonly)

Attribute :preloads option

Returns:

  • (Hash, nil)

    Attribute :preloads option



34
35
36
# File 'lib/serega/attribute.rb', line 34

def preloads
  @preloads
end

#preloads_pathArray? (readonly)

Attribute :preloads_path option

Returns:

  • (Array, nil)

    Attribute :preloads_path option



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

def preloads_path
  @preloads_path
end

Instance Method Details

#initialize(name:, opts: {}, block: nil) ⇒ Object

Initializes new attribute

Parameters:

  • name (Symbol, String)

    Name of attribute

  • opts (Hash) (defaults to: {})

    Attribute options

  • block (Proc) (defaults to: nil)

    Custom block to find attribute value

Options Hash (opts:):

  • :method (Symbol)

    Object method name to fetch attribute value

  • :delegate (Hash)

    Allows to fetch value from nested object

  • :hide (Boolean)

    Specify ‘true` to not serialize this attribute by default

  • :many (Boolean)

    Specifies has_many relationship. By default is detected via object.is_a?(Enumerable)

  • :value (Proc, #call)

    Custom block or callable to find attribute value

  • :serializer (Serega, Proc)

    Relationship serializer class. Use ‘proc { MySerializer }` if serializers have cross references



57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/serega/attribute.rb', line 57

def initialize(name:, opts: {}, block: nil)
  serializer_class = self.class.serializer_class
  serializer_class::CheckAttributeParams.new(name, opts, block).validate

  @initials = SeregaUtils::EnumDeepFreeze.call(
    name: name,
    opts: SeregaUtils::EnumDeepDup.call(opts),
    block: block
  )

  normalizer = serializer_class::SeregaAttributeNormalizer.new(initials)
  set_normalized_vars(normalizer)
end

#relation?Boolean

Shows whether attribute has specified serializer

Returns:

  • (Boolean)

    Checks if attribute is relationship (if :serializer option exists)



73
74
75
# File 'lib/serega/attribute.rb', line 73

def relation?
  !@serializer.nil?
end

#serializerSerega?

Shows specified serializer class

Returns:

  • (Serega, nil)

    Attribute serializer if exists



79
80
81
82
83
84
# File 'lib/serega/attribute.rb', line 79

def serializer
  serializer = @serializer
  return serializer if (serializer.is_a?(Class) && (serializer < Serega)) || !serializer

  @serializer = serializer.is_a?(String) ? Object.const_get(serializer, false) : serializer.call
end

#value(object, context, batches: nil) ⇒ Object

Finds attribute value

Parameters:

  • object (Object)

    Serialized object

  • context (Hash, nil)

    Serialization context

Returns:

  • (Object)

    Serialized attribute value



94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
# File 'lib/serega/attribute.rb', line 94

def value(object, context, batches: nil)
  # Signatires should match allowed signatures in CheckOptValue and CheckBlock
  result =
    case value_block_signature
    when "1" then value_block.call(object)
    when "1_ctx" then value_block.call(object, ctx: context)
    when "1_batches" then value_block.call(object, batches: batches)
    when "1_batches_ctx" then value_block.call(object, ctx: context, batches: batches)
    when "2" then value_block.call(object, context)
    when "2_batches_ctx" then value_block.call(object, context, ctx: context, batches: batches)
    else value_block.call # signature is "0" - no parameters
    end

  result.nil? ? default : result
end

#visible?(modifiers) ⇒ Boolean

Checks if attribute must be added to serialized response

Parameters:

  • modifiers (Hash)

    Serialization modifiers

Options Hash (modifiers):

  • :only (Hash)

    The only attributes to serialize

  • :except (Hash)

    Attributes to hide

  • :with (Hash)

    Hidden attributes to serialize additionally

Returns:

  • (Boolean)


120
121
122
123
124
125
126
127
128
129
130
131
# File 'lib/serega/attribute.rb', line 120

def visible?(modifiers)
  except = modifiers[:except] || FROZEN_EMPTY_HASH
  only = modifiers[:only] || FROZEN_EMPTY_HASH
  with = modifiers[:with] || FROZEN_EMPTY_HASH

  return false if except.member?(name) && except[name].empty?
  return true if only.member?(name)
  return true if with.member?(name)
  return false unless only.empty?

  !hide
end