Class: MetaModel::Record::Association

Inherits:
Object
  • Object
show all
Defined in:
lib/metamodel/record/association.rb

Relation collapse

Instance Attribute Summary collapse

Validation collapse

Relation collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, major_model, secondary_model, relation, args) ⇒ Association

Returns a new instance of Association.



11
12
13
14
15
16
17
18
19
20
21
# 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

Instance Attribute Details

#dependentObject (readonly)

Returns the value of attribute dependent.



7
8
9
# File 'lib/metamodel/record/association.rb', line 7

def dependent
  @dependent
end

#major_modelObject

Returns the value of attribute major_model.



8
9
10
# File 'lib/metamodel/record/association.rb', line 8

def major_model
  @major_model
end

#nameObject (readonly)

Returns the value of attribute name.



4
5
6
# File 'lib/metamodel/record/association.rb', line 4

def name
  @name
end

#relationObject (readonly)

Returns the value of attribute relation.



6
7
8
# File 'lib/metamodel/record/association.rb', line 6

def relation
  @relation
end

#secondary_modelObject

Returns the value of attribute secondary_model.



9
10
11
# File 'lib/metamodel/record/association.rb', line 9

def secondary_model
  @secondary_model
end

#typeObject (readonly)

Returns the value of attribute type.



5
6
7
# File 'lib/metamodel/record/association.rb', line 5

def type
  @type
end

Instance Method Details

#belongs_to?Boolean

Returns:

  • (Boolean)


90
91
92
# File 'lib/metamodel/record/association.rb', line 90

def belongs_to?
  @relation == :belongs_to
end

#class_nameObject



23
24
25
# File 'lib/metamodel/record/association.rb', line 23

def class_name
  "#{major_model.name}#{secondary_model.name}Association".camelize
end

#debug_descriptionObject



100
101
102
# File 'lib/metamodel/record/association.rb', line 100

def debug_description
  "#{major_model.name}.#{relation}.#{secondary_model.name}.#{dependent}"
end

#expect_constraint?(constraint) ⇒ Boolean

Returns:

  • (Boolean)


43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# 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?Boolean

Returns:

  • (Boolean)


86
87
88
# File 'lib/metamodel/record/association.rb', line 86

def has_many?
  @relation == :has_many
end

#has_one?Boolean

Returns:

  • (Boolean)


82
83
84
# File 'lib/metamodel/record/association.rb', line 82

def has_one?
  @relation == :has_one
end

#hash_valueObject



39
40
41
# File 'lib/metamodel/record/association.rb', line 39

def hash_value
  self.hash.to_s(16)
end

#major_model_idObject



31
32
33
# File 'lib/metamodel/record/association.rb', line 31

def major_model_id
  major_model.foreign_id
end

#reverse_class_nameObject



27
28
29
# File 'lib/metamodel/record/association.rb', line 27

def reverse_class_name
  "#{secondary_model.name}#{major_model.name}Association".camelize
end

#secondary_model_idObject



35
36
37
# File 'lib/metamodel/record/association.rb', line 35

def secondary_model_id
  secondary_model.foreign_id
end

#validate_associationObject



67
68
69
# File 'lib/metamodel/record/association.rb', line 67

def validate_association
  validate_dependent(@dependent)
end

#validate_dependent(dependent) ⇒ Object

Raises:



71
72
73
74
75
76
# 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