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

#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

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



45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/serega/attribute.rb', line 45

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)



61
62
63
# File 'lib/serega/attribute.rb', line 61

def relation?
  !@serializer.nil?
end

#serializerSerega?

Shows specified serializer class

Returns:

  • (Serega, nil)

    Attribute serializer if exists



67
68
69
70
71
72
# File 'lib/serega/attribute.rb', line 67

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



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

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)


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

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