Sha256: 63f7a8c666e0ab41b6466a81fc647212d395bf97bab7c5a603e60d47c4d8e1aa

Contents?: true

Size: 1.99 KB

Versions: 13

Compression:

Stored size: 1.99 KB

Contents

# frozen_string_literal: true

class Serega
  module SeregaPlugins
    module Metadata
      #
      # Stores Attribute data
      #
      class MetaAttribute
        #
        # Stores Attribute instance methods
        #
        module InstanceMethods
          # @return [Symbol] Meta attribute name
          attr_reader :name

          # @return [Symbol] Meta attribute full path
          attr_reader :path

          # @return [Proc] Meta attribute options
          attr_reader :opts

          # @return [Proc] Meta attribute originally added block
          attr_reader :block

          #
          # Initializes new meta attribute
          #
          # @param path [Array<Symbol, String>] Path for metadata of attribute
          #
          # @param opts [Hash] metadata attribute options
          #
          # @param block [Proc] Proc that receives object(s) and context and finds value
          #
          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
          end

          #
          # Finds attribute value
          #
          # @param object [Object] Serialized object(s)
          # @param context [Hash, nil] Serialization context
          #
          # @return [Object] Serialized meta attribute value
          #
          def value(object, context)
            block.call(object, context)
          end

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

          private

          def check(path, opts, block)
            CheckPath.call(path)
            CheckOpts.call(opts, self.class.serializer_class.config.metadata.attribute_keys)
            CheckBlock.call(block)
          end
        end

        extend Serega::SeregaHelpers::SerializerClassHelper
        include InstanceMethods
      end
    end
  end
end

Version data entries

13 entries across 13 versions & 1 rubygems

Version Path
serega-0.9.0 lib/serega/plugins/metadata/meta_attribute.rb
serega-0.8.3 lib/serega/plugins/metadata/meta_attribute.rb
serega-0.8.2 lib/serega/plugins/metadata/meta_attribute.rb
serega-0.8.1 lib/serega/plugins/metadata/meta_attribute.rb
serega-0.8.0 lib/serega/plugins/metadata/meta_attribute.rb
serega-0.7.0 lib/serega/plugins/metadata/meta_attribute.rb
serega-0.6.1 lib/serega/plugins/metadata/meta_attribute.rb
serega-0.6.0 lib/serega/plugins/metadata/meta_attribute.rb
serega-0.5.2 lib/serega/plugins/metadata/meta_attribute.rb
serega-0.5.1 lib/serega/plugins/metadata/meta_attribute.rb
serega-0.5.0 lib/serega/plugins/metadata/meta_attribute.rb
serega-0.4.0 lib/serega/plugins/metadata/meta_attribute.rb
serega-0.3.0 lib/serega/plugins/metadata/meta_attribute.rb