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

Instance Method Summary collapse

Instance Attribute Details

#optionsObject

Returns the value of attribute options.



7
8
9
# File 'lib/no_brainer/document/association/core.rb', line 7

def options
  @options
end

#owner_modelObject

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_nameObject

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_modelObject



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, options={})
   = 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[]
      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

#hookObject



34
35
36
37
38
# 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

#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, options={})
  @owner_model = owner_model
  @target_name = target_name
  @options = 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