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

#blockProc (readonly)

Returns attribute block

Returns:

  • (Proc)

    Attribute originally added block



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

def block
  @block
end

#nameSymbol (readonly)

Returns attribute name

Returns:

  • (Symbol)

    Attribute name



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

def name
  @name
end

#optsHash (readonly)

Returns attribute options

Returns:

  • (Hash)

    Attribute options



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

def opts
  @opts
end

Instance Method Details

#hideBoolean?

Shows current opts option

Patched in:

  • plugin :preloads (returns true by default if config option auto_hide_attribute_with_preloads is enabled)

Returns:

  • (Boolean, nil)

    Attribute :hide option value



59
60
61
# File 'lib/serega/attribute.rb', line 59

def hide
  opts[:hide]
end

#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)

    Proc that receives object and context and finds attribute value

Options Hash (opts:):

  • :key (Symbol)

    Object instance method name to get attribute value

  • :exposed (Boolean)

    Configures if we should serialize this attribute by default. (by default is true for regular attributes and false for relationships)

  • :many (Boolean)

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

  • :serializer (Serega, Proc)

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



38
39
40
41
42
43
44
# File 'lib/serega/attribute.rb', line 38

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

  @name = name.to_sym
  @opts = SeregaUtils::EnumDeepDup.call(opts)
  @block = block
end

#keySymbol

Method name that will be used to get attribute value

Returns:

  • (Symbol)

    key



49
50
51
# File 'lib/serega/attribute.rb', line 49

def key
  @key ||= opts.key?(:key) ? opts[:key].to_sym : name
end

#manyBoolean?

Shows current opts option

Returns:

  • (Boolean, nil)

    Attribute :many option value



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

def many
  opts[:many]
end

#relation?Boolean

Shows whether attribute has specified serializer

Returns:

  • (Boolean)

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



71
72
73
# File 'lib/serega/attribute.rb', line 71

def relation?
  !opts[:serializer].nil?
end

#serializerSerega?

Shows specified serializer class

Returns:

  • (Serega, nil)

    Attribute serializer if exists



77
78
79
80
81
82
83
84
85
86
87
# File 'lib/serega/attribute.rb', line 77

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

  serializer = opts[:serializer]
  @serializer =
    case serializer
    when String then Object.const_get(serializer, false)
    when Proc then serializer.call
    else serializer
    end
end

#value(object, context) ⇒ Object

Finds attribute value

Parameters:

  • object (Object)

    Serialized object

  • context (Hash, nil)

    Serialization context

Returns:

  • (Object)

    Serialized attribute value



114
115
116
# File 'lib/serega/attribute.rb', line 114

def value(object, context)
  value_block.call(object, context)
end

#value_blockProc

Returns final block used to find attribute value

Patched in:

  • plugin :formatters (wraps resulted block in formatter block and formats :const values)

Returns:

  • (Proc)

    Proc to find attribute value



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

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

  @value_block =
    block ||
    opts[:value] ||
    const_block ||
    delegate_block ||
    keyword_block
end

#visible?(except:, only:, with:) ⇒ Boolean

Checks if attribute must be added to serialized response

Parameters:

  • except (Hash)

    manually hidden attributes

  • only (Hash)

    manually enforced exposed attributes, other attributes are enforced to be hidden

  • with (Hash)

    manually enforced exposed attributes

Returns:

  • (Boolean)


127
128
129
130
131
132
133
134
# File 'lib/serega/attribute.rb', line 127

def visible?(except:, only:, with:)
  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