Sha256: 8154742ce9c6b3f03c7913036ad3b2bb17f79c439b5b875d0acd5c71688e707b

Contents?: true

Size: 1.75 KB

Versions: 20

Compression:

Stored size: 1.75 KB

Contents

# frozen_string_literal: true

# Module Mutils
module Mutils
  # Module SerializationCore
  module Serialization
    # Module Methods
    module Methods
      # Module Attributes
      module Attributes
        def attributes(*attributes_list)
          parse_attributes_methods(attributes_list, false)
        end

        def custom_methods(*attributes_list)
          parse_attributes_methods(attributes_list, true)
        end

        def parse_attributes_methods(list, is_method)
          self.attributes_to_serialize = {} if attributes_to_serialize.nil?
          list&.each do |attr|
            value = { method: is_method, always_include: true }
            attributes_to_serialize[attr] = value
          end
        end

        def attribute(method_name, options = {}, &proc)
          raise "if: should be a Proc object for attribute #{method_name}" if options[:if] && !options[:if].instance_of?(Proc)

          if proc.instance_of? Proc
            self.attributes_to_serialize_blocks = {} if attributes_to_serialize_blocks.nil?
            options[:block] = proc
            attributes_to_serialize_blocks[method_name] = options
          else
            add_single_attribute(method_name, options, false)
          end
        end

        def custom_method(method_name, options = {})
          add_single_attribute(method_name, options, true)
        end

        def add_single_attribute(method_name, options, is_method)
          self.attributes_to_serialize = {} if attributes_to_serialize.nil?
          always_include = options[:always_include].nil? ? false : options[:always_include]
          value = { method: is_method, always_include: always_include, if: options[:if] }
          attributes_to_serialize[method_name] = value
        end
      end
    end
  end
end

Version data entries

20 entries across 20 versions & 1 rubygems

Version Path
mutils-1.3.3 lib/mutils/serialization/methods/attributes.rb
mutils-1.3.2 lib/mutils/serialization/methods/attributes.rb
mutils-1.3.1 lib/mutils/serialization/methods/attributes.rb
mutils-1.3.0 lib/mutils/serialization/methods/attributes.rb
mutils-1.2.6 lib/mutils/serialization/methods/attributes.rb
mutils-1.2.5 lib/mutils/serialization/methods/attributes.rb
mutils-1.2.2 lib/mutils/serialization/methods/attributes.rb
mutils-1.2.1 lib/mutils/serialization/methods/attributes.rb
mutils-1.2.0 lib/mutils/serialization/methods/attributes.rb
mutils-1.1.8 lib/mutils/serialization/methods/attributes.rb
mutils-1.1.7 lib/mutils/serialization/methods/attributes.rb
mutils-1.1.6 lib/mutils/serialization/methods/attributes.rb
mutils-1.1.5 lib/mutils/serialization/methods/attributes.rb
mutils-1.1.4 lib/mutils/serialization/methods/attributes.rb
mutils-1.1.3 lib/mutils/serialization/methods/attributes.rb
mutils-1.1.2 lib/mutils/serialization/methods/attributes.rb
mutils-1.1.1 lib/mutils/serialization/methods/attributes.rb
mutils-1.1.0 lib/mutils/serialization/methods/attributes.rb
mutils-1.0.1 lib/mutils/serialization/methods/attributes.rb
mutils-1.0.0 lib/mutils/serialization/methods/attributes.rb