Sha256: cc0d5c71d1e0dd37fa84741d62ef678f4d8906f038661229cb630102b8d956a2

Contents?: true

Size: 1.08 KB

Versions: 12

Compression:

Stored size: 1.08 KB

Contents

require 'active_support'

class LHS::Record

  module Includes
    extend ActiveSupport::Concern

    included do
      cattr_accessor :including, :referencing
    end

    module ClassMethods
      def includes(*args)
        class_clone_factory(args).tap do |class_clone|
          class_clone.including = unfold_args(args)
        end
      end

      def references(*args)
        class_clone_factory(args).tap do |class_clone|
          class_clone.referencing = unfold_args(args)
        end
      end

      def without_including
        class_clone_factory(rand.to_s.gsub(/\D/, '')).tap do |class_clone|
          class_clone.including = nil
        end
      end

      private

      def unfold_args(args)
        args.size == 1 ? args[0] : args
      end

      def class_clone_factory(args)
        name = "#{self}#{args.object_id}"
        constant = Object.const_set(name.demodulize, self.dup) # rubocop:disable Style/RedundantSelf
        class_clone = constant
        class_clone.endpoints = endpoints
        class_clone.mapping = mapping
        class_clone
      end
    end
  end
end

Version data entries

12 entries across 12 versions & 1 rubygems

Version Path
lhs-6.7.2 lib/lhs/concerns/record/includes.rb
lhs-6.7.1 lib/lhs/concerns/record/includes.rb
lhs-6.7.0 lib/lhs/concerns/record/includes.rb
lhs-6.6.2 lib/lhs/concerns/record/includes.rb
lhs-6.6.1 lib/lhs/concerns/record/includes.rb
lhs-6.6.0 lib/lhs/concerns/record/includes.rb
lhs-6.5.0 lib/lhs/concerns/record/includes.rb
lhs-6.4.0 lib/lhs/concerns/record/includes.rb
lhs-6.3.1 lib/lhs/concerns/record/includes.rb
lhs-6.3.0 lib/lhs/concerns/record/includes.rb
lhs-6.2.0 lib/lhs/concerns/record/includes.rb
lhs-6.1.0 lib/lhs/concerns/record/includes.rb