Sha256: 44e745d4b1ac0dd966d25527965507dbe0259e029f2bbce422f4fff159f0bdf0

Contents?: true

Size: 1.92 KB

Versions: 1

Compression:

Stored size: 1.92 KB

Contents

# frozen_string_literal: true

# Module Mutils
module Mutils
  module Serialization
    # Module SerializationCore
    module SerializationMethods
      extend ActiveSupport::Concern
      module ClassMethods
        def attributes(*attributes_list)
          self.attributes_to_serialize = {} if attributes_to_serialize.nil?
          attributes_list&.each { |attr| attributes_to_serialize[attr] = attr }
        end

        def custom_methods(*method_list)
          self.method_to_serialize = {} if method_to_serialize.nil?
          method_list&.each { |attr| method_to_serialize[attr] = attr }
        end

        def belongs_to(relationship_name, options = {})
          if options[:serializer].nil?
            raise "Serializer is Required for belongs_to :#{relationship_name}." \
                  "\nDefine it like:\nbelongs_to :#{relationship_name}, " \
                  'serializer: SERIALIZER_CLASS'
          end
          unless class_exists? options[:serializer]
            raise 'Serializer class not defined'
          end

          self.belongs_to_relationships = {} if belongs_to_relationships.nil?
          belongs_to_relationships[relationship_name] = options
        end

        alias has_one belongs_to

        def has_many(relationship_name, options = {})
          if options[:serializer].nil?
            raise "Serializer is Required for has_many :#{relationship_name}." \
                  "\nDefine it like:\nbelongs_to :#{relationship_name}, " \
                  'serializer: SERIALIZER_CLASS'
          end
          unless class_exists? options[:serializer]
            raise 'Serializer class not defined'
          end

          self.has_many_relationships = {} if has_many_relationships.nil?
          has_many_relationships[relationship_name] = options
        end

        def class_exists?(class_name)
          eval("defined?(#{class_name}) && #{class_name}.is_a?(Class)") == true
        end
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
mutils-0.2.25 lib/mutils/serialization/serialization_methods.rb