Sha256: 427e76989b9f37b0866f2810b5a3b742c037bcc817634a1316722a4a10fb5c9f

Contents?: true

Size: 1.54 KB

Versions: 14

Compression:

Stored size: 1.54 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

    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

14 entries across 14 versions & 1 rubygems

Version Path
foobara-0.0.14 projects/entity/src/entity.rb
foobara-0.0.13 projects/entity/src/entity.rb
foobara-0.0.12 projects/entity/src/entity.rb
foobara-0.0.11 projects/entity/src/entity.rb
foobara-0.0.10 projects/entity/src/entity.rb
foobara-0.0.9 projects/entity/src/entity.rb
foobara-0.0.8 projects/entity/src/entity.rb
foobara-0.0.7 projects/entity/src/entity.rb
foobara-0.0.6 projects/entity/src/entity.rb
foobara-0.0.5 projects/entity/src/entity.rb
foobara-0.0.4 projects/entity/src/entity.rb
foobara-0.0.3 projects/entity/src/entity.rb
foobara-0.0.2 projects/entity/src/entity.rb
foobara-0.0.1 projects/entity/src/entity.rb