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[:hide] option

Returns:

  • (Boolean, nil)

    Attribute :hide option value



55
56
57
# File 'lib/serega/attribute.rb', line 55

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[:many] option

Returns:

  • (Boolean, nil)

    Attribute :many option value



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

def many
  opts[:many]
end

#relation?Boolean

Shows whether attribute has specified serializer

Returns:

  • (Boolean)

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



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

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

#serializerSerega?

Shows specified serializer class

Returns:

  • (Serega, nil)

    Attribute serializer if exists



73
74
75
76
77
78
79
80
81
82
83
# File 'lib/serega/attribute.rb', line 73

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



106
107
108
# File 'lib/serega/attribute.rb', line 106

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

#value_blockProc

Returns final block that will be used to find attribute value

Returns:

  • (Proc)

    Proc to find attribute value



87
88
89
90
91
92
93
94
95
96
# File 'lib/serega/attribute.rb', line 87

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)


119
120
121
122
123
124
125
126
# File 'lib/serega/attribute.rb', line 119

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