Class: Vorpal::AssociationConfig

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

Overview

Object associations:

  • All object associations are uni-directional

  • The end that holds the association is the ‘Parent’ and the end that is referred to is the ‘Child’ or ‘Children’

Relational associations:

  • 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.



84
85
86
87
88
89
# File 'lib/vorpal/configs.rb', line 84

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.



77
78
79
# File 'lib/vorpal/configs.rb', line 77

def fk
  @fk
end

#local_class_configObject (readonly)

Returns the value of attribute local_class_config.



77
78
79
# File 'lib/vorpal/configs.rb', line 77

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.



82
83
84
# File 'lib/vorpal/configs.rb', line 82

def local_end_config
  @local_end_config
end

#remote_class_configsObject (readonly)

Returns the value of attribute remote_class_configs.



77
78
79
# File 'lib/vorpal/configs.rb', line 77

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.



82
83
84
# File 'lib/vorpal/configs.rb', line 82

def remote_end_config
  @remote_end_config
end

Instance Method Details

#add_remote_class_config(remote_class_configs) ⇒ Object



100
101
102
103
104
# File 'lib/vorpal/configs.rb', line 100

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



95
96
97
98
# File 'lib/vorpal/configs.rb', line 95

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



91
92
93
# File 'lib/vorpal/configs.rb', line 91

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

#foreign_key_info(remote_class_config) ⇒ Object



124
125
126
# File 'lib/vorpal/configs.rb', line 124

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

#polymorphic?Boolean

Returns:

  • (Boolean)


115
116
117
# File 'lib/vorpal/configs.rb', line 115

def polymorphic?
  !@fk_type.nil?
end

#remote_class_config(local_db_object) ⇒ Object



106
107
108
109
110
111
112
113
# File 'lib/vorpal/configs.rb', line 106

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



119
120
121
122
# File 'lib/vorpal/configs.rb', line 119

def set_foreign_key(local_db_object, remote_object)
  local_class_config.set_attribute(local_db_object, @fk, remote_object.try(:id))
  local_class_config.set_attribute(local_db_object, @fk_type, remote_object.class.name) if polymorphic?
end