Module: Serega::SeregaPlugins::Metadata::MetaAttribute::InstanceMethods

Included in:
Serega::SeregaPlugins::Metadata::MetaAttribute
Defined in:
lib/serega/plugins/metadata/meta_attribute.rb

Overview

Stores Attribute instance methods

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#blockProc? (readonly)

Returns Meta attribute originally added block.

Returns:

  • (Proc, nil)

    Meta attribute originally added block



24
25
26
# File 'lib/serega/plugins/metadata/meta_attribute.rb', line 24

def block
  @block
end

#nameSymbol (readonly)

Returns Meta attribute name.

Returns:

  • (Symbol)

    Meta attribute name



15
16
17
# File 'lib/serega/plugins/metadata/meta_attribute.rb', line 15

def name
  @name
end

#optsProc (readonly)

Returns Meta attribute options.

Returns:

  • (Proc)

    Meta attribute options



21
22
23
# File 'lib/serega/plugins/metadata/meta_attribute.rb', line 21

def opts
  @opts
end

#pathSymbol (readonly)

Returns Meta attribute full path.

Returns:

  • (Symbol)

    Meta attribute full path



18
19
20
# File 'lib/serega/plugins/metadata/meta_attribute.rb', line 18

def path
  @path
end

Instance Method Details

#hide?(value) ⇒ Boolean

Returns:

  • (Boolean)


63
64
65
# File 'lib/serega/plugins/metadata/meta_attribute.rb', line 63

def hide?(value)
  (!!opts[:hide_nil] && value.nil?) || (!!opts[:hide_empty] && (value.nil? || (value.respond_to?(:empty?) && value.empty?)))
end

#initialize(path:, opts:, block:) ⇒ Object

Initializes new meta attribute

Parameters:

  • path (Array<Symbol, String>)

    Path for metadata of attribute

  • opts (Hash)

    metadata attribute options

  • block (Proc)

    Proc that receives object(s) and context and finds value



35
36
37
38
39
40
41
42
43
44
45
# File 'lib/serega/plugins/metadata/meta_attribute.rb', line 35

def initialize(path:, opts:, block:)
  check(path, opts, block)

  @name = path.join(".").to_sym
  @path = SeregaUtils::EnumDeepDup.call(path)
  @opts = SeregaUtils::EnumDeepDup.call(opts)
  @block = block
  @normalized_block = normalize_block(opts[:value], opts[:const], block)
  @normalized_block_signature =
    SeregaUtils::MethodSignature.call(@normalized_block, pos_limit: 2, keyword_args: [])
end

#value(object, context) ⇒ Object

Finds attribute value

Parameters:

  • object (Object)

    Serialized object(s)

  • context (Hash, nil)

    Serialization context

Returns:

  • (Object)

    Serialized meta attribute value



55
56
57
58
59
60
61
# File 'lib/serega/plugins/metadata/meta_attribute.rb', line 55

def value(object, context)
  case normalized_block_signature
  when "0" then normalized_block.call
  when "1" then normalized_block.call(object)
  else normalized_block.call(object, context)
  end
end