Class: Vorpal::Config::AssociationConfig

Inherits:
Object
  • Object
show all
Defined in:
lib/vorpal/config/association_config.rb

Overview

Object association terminology:

  • All object associations are uni-directional

  • The end that holds the association is the ‘Owner’ and the end that is referred to is the ‘Associate’ or ‘Associates’

Relational association terminology:

  • Local end: has FK

  • Remote end: has no FK

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(local_class_config, fk, fk_type) ⇒ AssociationConfig

Returns a new instance of AssociationConfig.



26
27
28
29
30
31
# File 'lib/vorpal/config/association_config.rb', line 26

def initialize(local_class_config, fk, fk_type)
  @local_class_config = local_class_config
  @remote_class_configs = {}
  @fk = fk
  @fk_type = fk_type
end

Instance Attribute Details

#fkObject (readonly)

Returns the value of attribute fk.



19
20
21
# File 'lib/vorpal/config/association_config.rb', line 19

def fk
  @fk
end

#local_class_configObject (readonly)

Returns the value of attribute local_class_config.



19
20
21
# File 'lib/vorpal/config/association_config.rb', line 19

def local_class_config
  @local_class_config
end

#local_end_configObject

Only one of these two attributes needs to be specified If one is specified, then the association is uni-directional. If both are specified, then the association is bi-directional.



24
25
26
# File 'lib/vorpal/config/association_config.rb', line 24

def local_end_config
  @local_end_config
end

#remote_class_configsObject (readonly)

Returns the value of attribute remote_class_configs.



19
20
21
# File 'lib/vorpal/config/association_config.rb', line 19

def remote_class_configs
  @remote_class_configs
end

#remote_end_configObject

Only one of these two attributes needs to be specified If one is specified, then the association is uni-directional. If both are specified, then the association is bi-directional.



24
25
26
# File 'lib/vorpal/config/association_config.rb', line 24

def remote_end_config
  @remote_end_config
end

Instance Method Details

#add_remote_class_config(remote_class_configs) ⇒ Object



42
43
44
45
46
# File 'lib/vorpal/config/association_config.rb', line 42

def add_remote_class_config(remote_class_configs)
  Array(remote_class_configs).each do |remote_class_config|
    @remote_class_configs[remote_class_config.domain_class.name] = remote_class_config
  end
end

#associate(local_object, remote_object) ⇒ Object



37
38
39
40
# File 'lib/vorpal/config/association_config.rb', line 37

def associate(local_object, remote_object)
  local_end_config.associate(local_object, remote_object) if local_end_config && local_object
  remote_end_config.associate(remote_object, local_object) if remote_end_config && remote_object
end

#fk_value(local_db_object) ⇒ Object



33
34
35
# File 'lib/vorpal/config/association_config.rb', line 33

def fk_value(local_db_object)
  local_db_object.send(fk)
end

#foreign_key_info(remote_class_config) ⇒ Object

Returns ForeignKeyInfo.

Returns:

  • ForeignKeyInfo



67
68
69
# File 'lib/vorpal/config/association_config.rb', line 67

def foreign_key_info(remote_class_config)
  ForeignKeyInfo.new(@fk, @fk_type, remote_class_config.domain_class.name, polymorphic?)
end

#polymorphic?Boolean

Returns:

  • (Boolean)


57
58
59
# File 'lib/vorpal/config/association_config.rb', line 57

def polymorphic?
  !@fk_type.nil?
end

#remote_class_config(local_db_object) ⇒ Object



48
49
50
51
52
53
54
55
# File 'lib/vorpal/config/association_config.rb', line 48

def remote_class_config(local_db_object)
  if polymorphic?
    fk_type_value = local_db_object.send(@fk_type)
    @remote_class_configs[fk_type_value]
  else
    @remote_class_configs.values.first
  end
end

#set_foreign_key(local_db_object, remote_object) ⇒ Object



61
62
63
64
# File 'lib/vorpal/config/association_config.rb', line 61

def set_foreign_key(local_db_object, remote_object)
  local_class_config.set_attribute(local_db_object, @fk, remote_object&.send(unique_key_name))
  local_class_config.set_attribute(local_db_object, @fk_type, remote_object.class.name) if polymorphic?
end

#unique_key_nameObject



71
72
73
# File 'lib/vorpal/config/association_config.rb', line 71

def unique_key_name
  (@local_end_config || @remote_end_config).unique_key_name
end

#validateObject



75
76
77
78
79
80
81
# File 'lib/vorpal/config/association_config.rb', line 75

def validate
  if @local_end_config && @remote_end_config
    if @local_end_config.unique_key_name != @remote_end_config.unique_key_name
      raise ConfigurationError.new("#{@local_end_config.pretty_name} and #{@remote_end_config.pretty_name} must have the same unique_key_name/primary_key")
    end
  end
end