module NoBrainer::Document::Association::Core::Metadata

Attributes

options[RW]
owner_model[RW]
target_name[RW]

Public Class Methods

new(owner_model, target_name, options={}) click to toggle source
# File lib/no_brainer/document/association/core.rb, line 9
def initialize(owner_model, target_name, options={})
  @owner_model = owner_model
  @target_name = target_name
  @options = options
end

Public Instance Methods

add_callback_for(what) click to toggle source
# 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() click to toggle source
# 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={}) click to toggle source
Calls superclass method
# File lib/no_brainer/document/association/core.rb, line 23
def delegate(method_src, method_dst, options={})
  metadata = self
  owner_model.inject_in_layer :associations do
    define_method(method_src) do |*args, &block|
      super(*args, &block) if options[:call_super]
      target = options[:to] == :self ? self : associations[metadata]
      target.__send__(method_dst, *args, &block)
    end
  end
end
get_model_by_name(model_name) click to toggle source
# 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() click to toggle source
# File lib/no_brainer/document/association/core.rb, line 34
def hook
  options.assert_valid_keys(*self.class.const_get(:VALID_OPTIONS))
  delegate("#{target_name}=", "#{'polymorphic_' if options[:polymorphic]}write".to_sym)
  delegate("#{target_name}", "#{'polymorphic_' if options[:polymorphic]}read".to_sym)
end
new(owner) click to toggle source
# File lib/no_brainer/document/association/core.rb, line 19
def new(owner)
  association_model.new(self, owner)
end