모듈: Mongoid::Association::접근자
- 확장자:
- ActiveSupport::Concern
- 포함 항목:
- Mongoid::Association
- 다음에 정의됨:
- lib/mongoid/association/accessors.rb
개요
이 모듈에는 게터 및 세터를 통해 연결에 액세스하는 것과 관련된 모든 동작과 새 연결을 생성하기 위해 빌더 에 위임하는 방법이 포함되어 있습니다.
클래스 메서드 요약 접기
-
.describe_builder!(association) ⇒ 클래스
Defines a builder method for an embeds_one association.
-
.describe_creator!(association) ⇒ 클래스
Defines a creator method for an embeds_one association.
-
.define_existence_check!(association) ⇒ Class
Adds the existence check for associations.
-
.define_getter!(association) ⇒ Class
Defines the getter for the association.
-
.define_ids_getter!(association) ⇒ Class
Defines the getter for the ids of documents in the association.
-
.define_ids_setter!(association) ⇒ Object
Defines the setter method that allows you to set documents in this association by their ids.
-
.define_setter!(association) ⇒ Class
Defines the setter for the association.
인스턴스 메서드 요약 접기
-
#__build__(name, 객체,association,selected_fields = nil) ⇒ Proxy
관련 문서를 빌드하고 문서가 nil이 아닌 경우 연관 관계를 생성한 다음, 이 문서에 연관 관계를 설정합니다.
-
#create_relation(객체,association,selected_fields = nil) ⇒ 프록시
객체 및 연결 메타데이터 에서 연결을 만듭니다.
-
#reset_relation_criteria(name) ⇒ 객체
연결 프록시 내부의 기준을 재설정합니다.
-
#set_relation(이름, 관계) ⇒ 프록시
제공된 연결을 제공된 이름을 가진 클래스의 인스턴스 변수로 설정합니다.
클래스 메서드 세부 정보
.describe_builder!(association) ⇒ 클래스
Defines a builder method for an embeds_one association. This is defined as #build_name.
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| 속성, = 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.
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| 속성, = 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.
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.
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.
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.
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.
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이 아닌 경우 연관 관계를 생성한 다음, 이 문서에 연관 관계를 설정합니다.
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) ⇒ 프록시
객체 및 연결 메타데이터 에서 연결을 만듭니다.
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 배열 을 동기화 된 상태로 유지하는 데 사용됩니다.
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(이름, 관계) ⇒ 프록시
제공된 연결을 제공된 이름을 가진 클래스의 인스턴스 변수로 설정합니다. 코드를 깔끔하게 정리하기 위한 헬퍼로 사용됩니다.
85 86 87 |
# 파일 'lib/mongoid/association/accessors.rb', 줄 85 def set_relation(이름, 관계) instance_variable_set("@_#{name}", 관계) end |