<commit_before># encoding: utf-8
module Mongoid # :nodoc:
  module Relations #:nodoc:
    module Builders #:nodoc:
      module NestedAttributes #:nodoc:
        class One < NestedBuilder

          attr_accessor :destroy

          # Builds the relation depending on the attributes and the options
          # passed to the macro.
          #
          # This attempts to perform 3 operations, either one of an update of
          # the existing relation, a replacement of the relation with a new
          # document, or a removal of the relation.
          #
          # Example:
          #
          # <tt>one.build(person)</tt>
          #
          # Options:
          #
          # parent: The parent document of the relation.
          def build(parent)
            return if reject?(attributes)
            @existing = parent.send(metadata.name)
            if update?
              existing.attributes = attributes
            elsif replace?
              parent.send(metadata.setter, metadata.klass.new(attributes))
            elsif delete?
              parent.send(metadata.setter, nil)
            end
          end

          # Create the new builder for nested attributes on one-to-one
          # relations.
          #
          # Example:
          #
          # <tt>One.new(metadata, attributes, options)</tt>
          #
          # Options:
          #
          # metadata: The relation metadata
          # attributes: The attributes hash to attempt to set.
          # options: The options defined.
          #
          # Returns:
          #
          # A new builder.
          def initialize(metadata, attributes, options)
            @attributes = attributes.with_indifferent_access
            @metadata = metadata
            @options = options
            @destroy = @attributes.delete(:_destroy)
          end

          private

          # Is the id in the attribtues acceptable for allowing an update to
          # the existing relation?
          #
          # Example:
          #
          # <tt>acceptable_id?</tt>
          #
          # Returns:
          #
          # True if the id part of the logic will allow an update.
          def acceptable_id?
            id = convert_id(attributes[:id])
            existing.id == id || id.nil? || (existing.id != id && update_only?)
          end

          # Can the existing relation be deleted?
          #
          # Example:
          #
          # <tt>delete?</tt>
          #
          # Returns:<commit_msg>Fix nested relation updating<commit_after># encoding: utf-8
module Mongoid # :nodoc:
  module Relations #:nodoc:
    module Builders #:nodoc:
      module NestedAttributes #:nodoc:
        class One < NestedBuilder

          attr_accessor :destroy

          # Builds the relation depending on the attributes and the options
          # passed to the macro.
          #
          # This attempts to perform 3 operations, either one of an update of
          # the existing relation, a replacement of the relation with a new
          # document, or a removal of the relation.
          #
          # Example:
          #
          # <tt>one.build(person)</tt>
          #
          # Options:
          #
          # parent: The parent document of the relation.
          def build(parent)
            return if reject?(attributes)
            @existing = parent.send(metadata.name)
            if update?
              existing.attributes = attributes
            elsif replace?
              parent.send(metadata.setter, metadata.klass.new(attributes))
            elsif delete?
              parent.send(metadata.setter, nil)
            end
          end

          # Create the new builder for nested attributes on one-to-one
          # relations.
          #
          # Example:
          #
          # <tt>One.new(metadata, attributes, options)</tt>
          #
          # Options:
          #
          # metadata: The relation metadata
          # attributes: The attributes hash to attempt to set.
          # options: The options defined.
          #
          # Returns:
          #
          # A new builder.
          def initialize(metadata, attributes, options)
            @attributes = attributes.with_indifferent_access
            @metadata = metadata
            @options = options
            @destroy = @attributes.delete(:_destroy)
          end

          private

          # Is the id in the attribtues acceptable for allowing an update to
          # the existing relation?
          #
          # Example:
          #
          # <tt>acceptable_id?</tt>
          #
          # Returns:
          #
          # True if the id part of the logic will allow an update.
          def acceptable_id?
            id = convert_id(attributes[:id])
            existing.id.to_s == id || existing.id == id || id.nil? || (existing.id != id && update_only?)
          end

          # Can the existing relation be deleted?
          #
          # Example:
          #
          # <tt>delete?</tt>
          #
          # Returns:
{'fraction_non_alphanumeric': 0.0790670092903736, 'fraction_numerical': 0.000790670092903736, 'mean_word_length': 1.4049429657794676, 'pattern_counts': {'":': 0, '<': 21, '<?xml version=': 0, '>': 19, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '673528', 'n_tokens_mistral': 1234, 'n_tokens_neox': 1219, 'n_words': 392}