lib/esse/document.rb in esse-0.3.5 vs lib/esse/document.rb in esse-0.4.0.rc1

- old
+ new

@@ -1,14 +1,16 @@ # frozen_string_literal: true module Esse class Document + MUTATIONS_FALLBACK = {}.freeze + attr_reader :object, :options def initialize(object, **options) @object = object - @options = options + @options = options.freeze end # @return [String, Number] the document ID # @abstract Override this method to return the document ID def id @@ -82,23 +84,32 @@ def ignore_on_delete? id.nil? end - def ==(other) - other.is_a?(self.class) && ( - id == other.id && type == other.type && routing == other.routing && meta == other.meta && source == other.source - ) + def eql?(other, match_lazy_doc_header: false) + if match_lazy_doc_header + other.eql?(self) + else + other.is_a?(Esse::Document) && ( + id.to_s == other.id.to_s && type == other.type && routing == other.routing && meta == other.meta + ) + end end + alias_method :==, :eql? def doc_header { _id: id }.tap do |h| h[:_type] = type if type h[:routing] = routing if routing? end end + def document_for_partial_update(source) + DocumentForPartialUpdate.new(self, source: source) + end + def inspect attributes = %i[id routing source].map do |attr| value = send(attr) next unless value "#{attr}: #{value.inspect}" @@ -113,10 +124,12 @@ @__mutations__ ||= {} @__mutations__[key] = yield instance_variable_set(:@__mutated_source__, nil) end - protected + def mutations + @__mutations__ || MUTATIONS_FALLBACK + end def mutated_source return source unless @__mutations__ @__mutated_source__ ||= source.merge(@__mutations__)