Sha256: 36f2ff72b058a89bcce5513414cba4fa7d5797249158e05a58aa6362974d29d8

Contents?: true

Size: 1.96 KB

Versions: 8

Compression:

Stored size: 1.96 KB

Contents

module Foobara
  class DetachedEntity < Model
    module Concerns
      module Reflection
        class CannotConvertRecordWithoutPrimaryKeyToJsonError < StandardError; end

        include Concern

        def inspect
          "<#{entity_name}:#{primary_key}>"
        end

        def to_json(*_args)
          primary_key&.to_json || raise(
            CannotConvertRecordWithoutPrimaryKeyToJsonError,
            "Cannot call record.to_json on unless record has a primary key. " \
            "Consider instead calling record.attributes.to_json instead."
          )
        end

        module ClassMethods
          def depends_on
            associations.values.map(&:target_class).uniq
          end

          def deep_depends_on
            types = deep_associations.sort_by do |path, _type|
              [DataPath.new(path).path.size, path]
            end.map(&:last)

            types.map(&:target_class).uniq
          end

          def foobara_manifest(to_include: Set.new)
            associations = self.associations.map do |(path, type)|
              entity_class = type.target_class
              entity_name = entity_class.full_entity_name

              [path, entity_name]
            end.sort.to_h

            deep_associations = self.deep_associations.map do |(path, type)|
              entity_class = type.target_class
              entity_name = entity_class.full_entity_name

              [path, entity_name]
            end.sort.to_h

            super.merge(
              Util.remove_blank(
                depends_on: depends_on.map(&:full_entity_name),
                deep_depends_on: deep_depends_on.map(&:full_entity_name),
                associations:,
                deep_associations:,
                entity_name:,
                primary_key_attribute:,
                primary_key_type: attributes_type.declaration_data[:element_type_declarations][primary_key_attribute]
              )
            )
          end
        end
      end
    end
  end
end

Version data entries

8 entries across 8 versions & 1 rubygems

Version Path
foobara-0.0.38 projects/detached_entity/src/concerns/reflection.rb
foobara-0.0.37 projects/detached_entity/src/concerns/reflection.rb
foobara-0.0.36 projects/detached_entity/src/concerns/reflection.rb
foobara-0.0.35 projects/detached_entity/src/concerns/reflection.rb
foobara-0.0.34 projects/detached_entity/src/concerns/reflection.rb
foobara-0.0.33 projects/detached_entity/src/concerns/reflection.rb
foobara-0.0.32 projects/detached_entity/src/concerns/reflection.rb
foobara-0.0.31 projects/detached_entity/src/concerns/reflection.rb