Module: NoBrainer::Document::Association::Core::Metadata
- Extended by:
- ActiveSupport::Concern
- Included in:
- BelongsTo::Metadata, HasMany::Metadata, HasManyThrough::Metadata
- Defined in:
- lib/no_brainer/document/association/core.rb
Instance Attribute Summary collapse
-
#options ⇒ Object
Returns the value of attribute options.
-
#owner_model ⇒ Object
Returns the value of attribute owner_model.
-
#target_name ⇒ Object
Returns the value of attribute target_name.
Instance Method Summary collapse
- #add_callback_for(what) ⇒ Object
- #association_model ⇒ Object
- #delegate(method_src, method_dst, options = {}) ⇒ Object
- #get_model_by_name(model_name) ⇒ Object
- #hook ⇒ Object
- #initialize(owner_model, target_name, options = {}) ⇒ Object
- #new(owner) ⇒ Object
Instance Attribute Details
#options ⇒ Object
Returns the value of attribute options.
7 8 9 |
# File 'lib/no_brainer/document/association/core.rb', line 7 def @options end |
#owner_model ⇒ Object
Returns the value of attribute owner_model.
7 8 9 |
# File 'lib/no_brainer/document/association/core.rb', line 7 def owner_model @owner_model end |
#target_name ⇒ Object
Returns the value of attribute target_name.
7 8 9 |
# File 'lib/no_brainer/document/association/core.rb', line 7 def target_name @target_name end |
Instance Method Details
#add_callback_for(what) ⇒ Object
40 41 42 43 44 45 46 47 48 |
# File 'lib/no_brainer/document/association/core.rb', line 40 def add_callback_for(what) instance_eval <<-RUBY, __FILE__, __LINE__ + 1 if !@added_#{what} metadata = self owner_model.#{what} { associations[metadata].#{what}_callback } @added_#{what} = true end RUBY end |
#association_model ⇒ Object
15 16 17 |
# File 'lib/no_brainer/document/association/core.rb', line 15 def association_model @association_model ||= self.class.name.deconstantize.constantize end |
#delegate(method_src, method_dst, options = {}) ⇒ Object
23 24 25 26 27 28 29 30 31 32 |
# File 'lib/no_brainer/document/association/core.rb', line 23 def delegate(method_src, method_dst, ={}) = self owner_model.inject_in_layer :associations do define_method(method_src) do |*args, &block| super(*args, &block) if [:call_super] target = [:to] == :self ? self : associations[] target.__send__(method_dst, *args, &block) end end end |
#get_model_by_name(model_name) ⇒ Object
50 51 52 53 54 55 56 57 58 59 60 |
# File 'lib/no_brainer/document/association/core.rb', line 50 def get_model_by_name(model_name) return model_name if model_name.is_a?(Module) model_name = model_name.to_s current_module = NoBrainer.rails6? ? @owner_model.module_parent : @owner_model.parent return model_name.constantize if current_module == Object return model_name.constantize if model_name =~ /^::/ return model_name.constantize if !current_module.const_defined?(model_name) current_module.const_get(model_name) end |
#hook ⇒ Object
34 35 36 37 38 |
# File 'lib/no_brainer/document/association/core.rb', line 34 def hook .assert_valid_keys(*self.class.const_get(:VALID_OPTIONS)) delegate("#{target_name}=", "#{'polymorphic_' if [:polymorphic]}write".to_sym) delegate("#{target_name}", "#{'polymorphic_' if [:polymorphic]}read".to_sym) end |
#initialize(owner_model, target_name, options = {}) ⇒ Object
9 10 11 12 13 |
# File 'lib/no_brainer/document/association/core.rb', line 9 def initialize(owner_model, target_name, ={}) @owner_model = owner_model @target_name = target_name @options = end |
#new(owner) ⇒ Object
19 20 21 |
# File 'lib/no_brainer/document/association/core.rb', line 19 def new(owner) association_model.new(self, owner) end |