Module: Serega::Attribute::AttributeInstanceMethods

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

Overview

Stores Attribute instance methods

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#blockProc (readonly)

Returns Attribute originally added block.

Returns:

  • (Proc)

    Attribute originally added block



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

def block
  @block
end

#nameSymbol (readonly)

Returns Attribute name.

Returns:

  • (Symbol)

    Attribute name



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

def name
  @name
end

#optsHash (readonly)

Returns Attribute options.

Returns:

  • (Hash)

    Attribute options



16
17
18
# File 'lib/serega/attribute.rb', line 16

def opts
  @opts
end

Instance Method Details

#hideBoolean?

Returns Attribute initial :hide option value.

Returns:

  • (Boolean, nil)

    Attribute initial :hide option value



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

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.



35
36
37
38
39
40
41
# File 'lib/serega/attribute.rb', line 35

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

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

#keySymbol

Returns Object method name to will be used to get attribute value unless block provided.

Returns:

  • (Symbol)

    Object method name to will be used to get attribute value unless block provided



44
45
46
# File 'lib/serega/attribute.rb', line 44

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

#manyBoolean?

Returns Attribute initialization :many option.

Returns:

  • (Boolean, nil)

    Attribute initialization :many option



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

def many
  opts[:many]
end

#relation?Boolean

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

Returns:

  • (Boolean)

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



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

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

#serializerSerega?

Returns Attribute serializer if exists.

Returns:

  • (Serega, nil)

    Attribute serializer if exists



64
65
66
67
68
69
70
71
72
73
74
# File 'lib/serega/attribute.rb', line 64

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



91
92
93
# File 'lib/serega/attribute.rb', line 91

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

#value_blockProc

Returns Proc to find attribute value.

Returns:

  • (Proc)

    Proc to find attribute value



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

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

  @value_block = block || opts[:value] || const_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)


104
105
106
107
108
109
110
111
# File 'lib/serega/attribute.rb', line 104

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