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

#hideBoolean? (readonly)

Attribute :hide option

Returns:

  • (Boolean, nil)

    Attribute :hide option



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

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

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



41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/serega/attribute.rb', line 41

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)



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

def relation?
  !@serializer.nil?
end

#serializerSerega?

Shows specified serializer class

Returns:

  • (Serega, nil)

    Attribute serializer if exists



63
64
65
66
67
68
# File 'lib/serega/attribute.rb', line 63

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) ⇒ Object

Finds attribute value

Parameters:

  • object (Object)

    Serialized object

  • context (Hash, nil)

    Serialization context

Returns:

  • (Object)

    Serialized attribute value



78
79
80
# File 'lib/serega/attribute.rb', line 78

def value(object, context)
  value_block.call(object, context)
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)


92
93
94
95
96
97
98
99
100
101
102
103
# File 'lib/serega/attribute.rb', line 92

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