Sha256: 250b92326d859c573d1e12010812b573968af82be39973050f5d0fb9f23a9098

Contents?: true

Size: 1.46 KB

Versions: 3

Compression:

Stored size: 1.46 KB

Contents

# frozen_string_literal: true

# Module Mutils
module Mutils
  # Module SerializationCore
  module Serialization
    # Module Methods
    module Methods
      # Module Relations
      module Relations
        def relationship(relationship_name, options = {})
          raise "if: should be a Proc object for attribute #{relationship_name}" if options[:if] && !options[:if].instance_of?(Proc)

          options = prepare_options(relationship_name, options, __callee__)
          self.relationships = {} if relationships.nil?
          relationships[relationship_name] = options
        end

        alias belongs_to relationship
        alias has_many relationship
        alias has_one relationship

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

          options[:serializer] = class_name.to_s
          options[:always_include] = options[:always_include].nil? ? false : options[:always_include]
          options[:label] = Lib::Helper.instance.underscore options[:label] || relationship_name
          options
        end
      end
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
mutils-1.3.3 lib/mutils/serialization/methods/relations.rb
mutils-1.3.2 lib/mutils/serialization/methods/relations.rb
mutils-1.3.1 lib/mutils/serialization/methods/relations.rb