class MetaModel::Record::Association

Attributes

dependent[R]
major_model[RW]
name[R]
relation[R]
secondary_model[RW]
type[R]

Public Class Methods

new(name, major_model, secondary_model, relation, args) click to toggle source
# File lib/metamodel/record/association.rb, line 11
def initialize(name, major_model, secondary_model, relation, args)
  dependent = args[:dependent] || :nullify

  @name            = name.to_s.camelize :lower
  @relation        = relation
  @dependent       = dependent
  @major_model     = major_model
  @secondary_model = secondary_model

  validate_association
end

Public Instance Methods

belongs_to?() click to toggle source
# File lib/metamodel/record/association.rb, line 90
def belongs_to?
  @relation == :belongs_to
end
class_name() click to toggle source
# File lib/metamodel/record/association.rb, line 23
def class_name
  "#{major_model.name}#{secondary_model.name}Association".camelize
end
debug_description() click to toggle source
# File lib/metamodel/record/association.rb, line 104
def debug_description
  "#{major_model.name}.#{relation}.#{secondary_model.name}.#{dependent}"
end
expect_constraint?(constraint) click to toggle source
# File lib/metamodel/record/association.rb, line 43
def expect_constraint?(constraint)
  result = true
  result &= self.major_model == constraint.secondary_model
  result &= self.secondary_model == constraint.major_model

  result &= case [self.relation, constraint.relation]
    when [:has_one, :belongs_to], [:belongs_to, :has_one] then true
    when [:belongs_to, :has_many] then
      return false if self.dependent == :destroy
      return true
    when [:has_many, :belongs_to] then
      return false if constraint.dependent == :destroy
      return true
    when [:has_many, :has_many] then
      return true
    else false
  end
  result
end
has_many?() click to toggle source
# File lib/metamodel/record/association.rb, line 86
def has_many?
  @relation == :has_many
end
has_one?() click to toggle source

@!group Relation

# File lib/metamodel/record/association.rb, line 82
def has_one?
  @relation == :has_one
end
hash_value() click to toggle source
# File lib/metamodel/record/association.rb, line 39
def hash_value
  self.hash.to_s(16)
end
is_active?() click to toggle source
# File lib/metamodel/record/association.rb, line 94
def is_active?
  has_one? || has_many?
end
major_model_id() click to toggle source
# File lib/metamodel/record/association.rb, line 31
def major_model_id
  major_model.foreign_id
end
reverse_class_name() click to toggle source
# File lib/metamodel/record/association.rb, line 27
def reverse_class_name
  "#{secondary_model.name}#{major_model.name}Association".camelize
end
secondary_model_id() click to toggle source
# File lib/metamodel/record/association.rb, line 35
def secondary_model_id
  secondary_model.foreign_id
end
validate_association() click to toggle source

@!group Validation

# File lib/metamodel/record/association.rb, line 67
def validate_association
  validate_dependent(@dependent)
end
validate_dependent(dependent) click to toggle source
# File lib/metamodel/record/association.rb, line 71
def validate_dependent(dependent)
  supported_dependent_options = [:nullify, :destroy]
  raise Informative, "Unknown dependent option #{dependent}, \
    MetaModel only supports #{supported_dependent_options} now" \
    unless supported_dependent_options.include? dependent
end