Sha256: b20b084aa81d0f5cbdae94dd3dc05a09af6044692792b2c2f0c37764d9eb2ac8

Contents?: true

Size: 1.46 KB

Versions: 17

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

17 entries across 17 versions & 1 rubygems

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