Sha256: d879a29f903ddfb816e27eea26692b8c838d234c2cd43633173c72130cbf1301

Contents?: true

Size: 1.58 KB

Versions: 12

Compression:

Stored size: 1.58 KB

Contents

module Foobara
  class Entity < Model
    class CannotConvertRecordWithoutPrimaryKeyToJsonError < StandardError; end

    include Concerns::Callbacks
    include Concerns::Associations
    include Concerns::Transactions
    include Concerns::Queries
    include Concerns::Types
    include Concerns::Attributes
    include Concerns::PrimaryKey
    include Concerns::Persistence
    include Concerns::Initialization
    include Concerns::Reflection
    include Concerns::AttributeHelpers

    class << self
      prepend NewPrepend

      def full_entity_name
        full_model_name
      end

      def entity_name
        model_name
      end

      def allowed_subclass_opts
        [:primary_key, *super]
      end
    end

    foobara_delegate :full_entity_name, :entity_name, to: :class

    def dup
      # TODO: Maybe raise instead?
      self
    end

    def ==(other)
      # Should both records be required to be persisted to be considered equal when having matching primary keys?
      # For now we will consider them equal but it could make sense to consider them not equal.
      equal?(other) || (self.class == other.class && primary_key && primary_key == other.primary_key)
    end

    def hash
      (primary_key || object_id).hash
    end

    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
  end
end

Version data entries

12 entries across 12 versions & 1 rubygems

Version Path
foobara-0.0.26 projects/entity/src/entity.rb
foobara-0.0.25 projects/entity/src/entity.rb
foobara-0.0.24 projects/entity/src/entity.rb
foobara-0.0.23 projects/entity/src/entity.rb
foobara-0.0.22 projects/entity/src/entity.rb
foobara-0.0.21 projects/entity/src/entity.rb
foobara-0.0.20 projects/entity/src/entity.rb
foobara-0.0.19 projects/entity/src/entity.rb
foobara-0.0.18 projects/entity/src/entity.rb
foobara-0.0.17 projects/entity/src/entity.rb
foobara-0.0.16 projects/entity/src/entity.rb
foobara-0.0.15 projects/entity/src/entity.rb