모듈: Mongoid::Association::접근자

확장자:
ActiveSupport::Concern
포함 항목:
Mongoid::Association
다음에 정의됨:
lib/mongoid/association/accessors.rb

개요

이 모듈에는 게터 및 세터를 통해 연결에 액세스하는 것과 관련된 모든 동작과 새 연결을 생성하기 위해 빌더 에 위임하는 방법이 포함되어 있습니다.

클래스 메서드 요약 접기

인스턴스 메서드 요약 접기

클래스 메서드 세부 정보

.describe_builder!(association) ⇒ 클래스

Defines a builder method for an embeds_one association. This is defined as #build_name.

예시:

Person.define_builder!(association)

매개변수:

반환합니다:

  • (클래스)

    클래스를 설정하다 중입니다.



387
388
389
390
391
392
393
394
395
396
397
398
399
400
# 파일 'lib/mongoid/association/accessors.rb', 줄 387

def self.describe_builder!(연관 관계)
  이름 = 연관 관계.이름
  연관 관계.inverse_class. do |class|
    class.re_define_method("build_#{name}") do |*args|
      속성, _options = parse_args(*args)
      문서 = 공장.빌드(연관 관계.관계 클래스, 속성)
      _build do
        자식 = send("#{name}=", 문서)
        자식.run_callbacks(:build)
        자식
      end
    end
  end
end

.describe_creator!(association) ⇒ 클래스

Defines a creator method for an embeds_one association. This is defined as #create_name. After the object is built it will immediately save.

예시:

Person.define_creator!(association)

매개변수:

반환합니다:

  • (클래스)

    클래스를 설정하다 중입니다.



412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
# 파일 'lib/mongoid/association/accessors.rb', 줄 412

def self.describe_creator!(연관 관계)
  이름 = 연관 관계.이름
  연관 관계.inverse_class. do |class|
    class.re_define_method("create_#{name}") do |*args|
      속성, _options = parse_args(*args)
      문서 = 공장.빌드(연관 관계.class, 속성)
      doc = _assigning do
        send("#{name}=", 문서)
      end
      doc.저장
      저장 만약 new_record? && 연관 관계.Stores_foreign_key?
      doc
    end
  end
end

.define_existence_check!(association) ⇒ Class

Adds the existence check for associations.

예시:

Add the existence check.

Person.define_existence_check!(association)

Check if an association exists.

person = Person.new
person.has_game?
person.game?

매개변수:

반환합니다:

  • (클래스)

    The model being set up.



277
278
279
280
281
282
283
284
285
286
287
# 파일 'lib/mongoid/association/accessors.rb', 줄 277

def self.정의_존재_확인!(연관 관계)
  이름 = 연관 관계.이름
  연관 관계.inverse_class. do |class|
    class.module_eval <<-END, __FILE__, __LINE__ + 1
        def #{이름}?
          without_autobuild { !__send__(:#{이름}).blank? }
        end
        alias :has_#{이름}? :#{이름}?
    END
  end
end

.define_getter!(association) ⇒ Class

Defines the getter for the association. Nothing too special here: just return the instance variable for the association if it exists or build the thing.

예시:

Set up the getter for the association.

Person.define_getter!(association)

매개변수:

반환합니다:

  • (클래스)

    클래스를 설정하다 중입니다.



299
300
301
302
303
304
305
306
307
308
# 파일 'lib/mongoid/association/accessors.rb', 줄 299

def self.정의_게터!(연관 관계)
  이름 = 연관 관계.이름
  연관 관계.inverse_class. do |class|
    class.re_define_method(이름) do |다시 로드 = 거짓|
      value = get_relation(이름, 연관 관계, nil, 다시 로드)
      value = send("build_#{name}") 만약 value.nil? && 연관 관계.자동 구축? && !without_autobuild?
      value
    end
  end
end

.define_ids_getter!(association) ⇒ Class

Defines the getter for the ids of documents in the association. Should be specify only for referenced many associations.

예시:

Set up the ids getter for the association.

Person.define_ids_getter!(association)

매개변수:

반환합니다:

  • (클래스)

    클래스를 설정하다 중입니다.



319
320
321
322
323
324
325
326
# 파일 'lib/mongoid/association/accessors.rb', 줄 319

def self.describe_ids_getter!(연관 관계)
  ids_method = "#{association.name.to_s.singularize}_ids"
  연관 관계.inverse_class. do |class|
    class.re_define_method(ids_method) do
      send(연관 관계.이름).뽑다(:_id)
    end
  end
end

.define_ids_setter!(association) ⇒ Object

Defines the setter method that allows you to set documents in this association by their ids. The defined setter, finds documents with given ids and invokes regular association setter with found documents. Ids setters should be defined only for referenced many associations.

@param [ Mongoid::Association::Relatable ] association The association for the association.

@return [ Class ] The class being set up.

예시:

Set up the id_setter for the association.

Person.define_ids_setter!(association)


368
369
370
371
372
373
374
375
376
# 파일 'lib/mongoid/association/accessors.rb', 줄 368

def self.describe_ids_setter!(연관 관계)
  ids_method = "#{association.name.to_s.singularize}_ids="
  연관 관계.inverse_class.aliased_associations[ids_method.자르다] = 연관 관계.이름.to_s
  연관 관계.inverse_class. do |class|
    class.re_define_method(ids_method) do |ID|
      send(연관 관계.세터, 연관 관계.관계 클래스.찾기(ID.거부(&:blank?)))
    end
  end
end

.define_setter!(association) ⇒ Class

Defines the setter for the association. This does a few things based on some conditions. If there is an existing association, a target substitution will take place, otherwise a new association will be created with the supplied target.

예시:

Set up the setter for the association.

Person.define_setter!(association)

매개변수:

반환합니다:

  • (클래스)

    클래스를 설정하다 중입니다.



339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
# 파일 'lib/mongoid/association/accessors.rb', 줄 339

def self.define_setter!(연관 관계)
  이름 = 연관 관계.이름
  연관 관계.inverse_class. do |class|
    class.re_define_method("#{name}=") do |객체|
      without_autobuild do
        만약 value = get_relation(이름, 연관 관계, 객체)
          value = __build__(이름, value, 연관 관계) 하지 않는 한 value.response_to?(:substitute)

          set_relation(이름, value.대체(객체.대체 가능))
        other
          __build__(이름, 객체.대체 가능, 연관 관계)
        end
      end
    end
  end
end

인스턴스 메서드 세부 정보

#__build__(name, 객체,association,selected_fields = nil) ⇒ Proxy

관련 문서를 빌드하고 문서가 nil이 아닌 경우 연관 관계를 생성한 다음, 이 문서에 연관 관계를 설정합니다.

예시:

연관 관계를 구축합니다.

person.__build__(:addresses, { :_id => 1 }, association)

매개변수:

  • 이름 (string | 기호)

    연관 관계의 이름입니다.

  • 객체 (해시 | BSON::ObjectId)

    사용할 ID 또는 속성입니다.

  • 연관 관계 (Mongoid::Association::Relatable)

    연결 메타데이터입니다.

  • selected_fields (해시) (기본값: nil)

    #only를 통해 조회된 필드입니다. selected_fields를 지정하면 여기에 나열되지 않은 필드는 빌드된 문서 에서 액세스할 수 없습니다.

반환합니다:



25
26
27
28
# 파일 'lib/mongoid/association/accessors.rb', 줄 25

def __build__(이름, 객체, 연관 관계, selected_fields = nil)
  관계 = create_relation(객체, 연관 관계, selected_fields)
  set_relation(이름, 관계)
end

#create_relation(객체,association,selected_fields = nil) ⇒ 프록시

객체 및 연결 메타데이터 에서 연결을 만듭니다.

예시:

연관 관계를 생성합니다.

person.create_relation(document, association)

매개변수:

  • 객체 (문서 | 배열<문서>)

    연결 대상입니다.

  • 연관 관계 (Mongoid::Association::Relatable)

    연결 메타데이터입니다.

  • selected_fields (해시) (기본값: nil)

    #only를 통해 조회된 필드입니다. Selected_fields를 지정하면 해당 항목에 나열되지 않은 필드는 생성된 연결 문서 에서 액세스할 수 없습니다.

반환합니다:



42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# 파일 'lib/mongoid/association/accessors.rb', 줄 42

def create_relation(객체, 연관 관계, selected_fields = nil)
   = @attributes[연관 관계.inverse_type]
  유형 =  ? 연관 관계.리졸버.Model_for() : nil
  대상 = 만약 t = 연관 관계.빌드(self, 객체, 유형, selected_fields)
             연관 관계.create_relation(self, t)
           end

  # 포함된 연관 관계에서만 이 작업을 수행하면 됩니다. 보류 중인 콜백
  # 문서를 구체화할 때만 추가됩니다.
  포함된 연결에 대한 #. 데이터베이스 에 대한 호출이 없습니다.
  # 참조된 연관 관계의 구성.
  만약 연관 관계.임베디드?
    배열(대상). do |doc|
      doc.try(:run_pending_callbacks)
    end
  end

  대상
end

#reset_relation_criteria(name) ⇒ 객체

연결 프록시 내부의 기준을 재설정합니다. 다대다 연관 관계에서 기본 ID 배열 을 동기화 된 상태로 유지하는 데 사용됩니다.

예시:

연결 기준을 재설정합니다.

person.reset_relation_criteria(:preferences)

매개변수:

  • 이름 (기호)

    연관 관계의 이름입니다.



69
70
71
72
73
# 파일 'lib/mongoid/association/accessors.rb', 줄 69

def reset_relation_criteria(이름)
  반환 하지 않는 한 instance_variable_defined?("@_#{name}")

  send(이름).reset_unloaded
end

#set_relation(이름, 관계) ⇒ 프록시

제공된 연결을 제공된 이름을 가진 클래스의 인스턴스 변수로 설정합니다. 코드를 깔끔하게 정리하기 위한 헬퍼로 사용됩니다.

예시:

문서 에 프록시를 설정합니다.

person.set(:addresses, addresses)

매개변수:

  • 이름 (string | 기호)

    연관 관계의 이름입니다.

  • 관계 (프록시)

    설정하다 연결입니다.

반환합니다:



85
86
87
# 파일 'lib/mongoid/association/accessors.rb', 줄 85

def set_relation(이름, 관계)
  instance_variable_set("@_#{name}", 관계)
end