Class: MetaModel::Record::Association
- Inherits:
-
Object
- Object
- MetaModel::Record::Association
- Defined in:
- lib/metamodel/record/association.rb
Relation collapse
-
#type ⇒ Object
readonly
Returns the value of attribute type.
Instance Attribute Summary collapse
-
#dependent ⇒ Object
readonly
Returns the value of attribute dependent.
-
#major_model ⇒ Object
Returns the value of attribute major_model.
-
#name ⇒ Object
readonly
Returns the value of attribute name.
-
#relation ⇒ Object
readonly
Returns the value of attribute relation.
-
#secondary_model ⇒ Object
Returns the value of attribute secondary_model.
Validation collapse
Relation collapse
Instance Method Summary collapse
- #class_name ⇒ Object
- #expect_constraint?(constraint) ⇒ Boolean
- #hash_value ⇒ Object
-
#initialize(name, major_model, secondary_model, relation, args) ⇒ Association
constructor
A new instance of Association.
- #major_model_id ⇒ Object
- #reverse_class_name ⇒ Object
- #secondary_model_id ⇒ Object
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
#dependent ⇒ Object (readonly)
Returns the value of attribute dependent.
7 8 9 |
# File 'lib/metamodel/record/association.rb', line 7 def dependent @dependent end |
#major_model ⇒ Object
Returns the value of attribute major_model.
8 9 10 |
# File 'lib/metamodel/record/association.rb', line 8 def major_model @major_model end |
#name ⇒ Object (readonly)
Returns the value of attribute name.
4 5 6 |
# File 'lib/metamodel/record/association.rb', line 4 def name @name end |
#relation ⇒ Object (readonly)
Returns the value of attribute relation.
6 7 8 |
# File 'lib/metamodel/record/association.rb', line 6 def relation @relation end |
#secondary_model ⇒ Object
Returns the value of attribute secondary_model.
9 10 11 |
# File 'lib/metamodel/record/association.rb', line 9 def secondary_model @secondary_model end |
#type ⇒ Object (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
90 91 92 |
# File 'lib/metamodel/record/association.rb', line 90 def belongs_to? @relation == :belongs_to end |
#class_name ⇒ Object
23 24 25 |
# File 'lib/metamodel/record/association.rb', line 23 def class_name "#{major_model.name}#{secondary_model.name}Association".camelize end |
#debug_description ⇒ Object
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
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
86 87 88 |
# File 'lib/metamodel/record/association.rb', line 86 def has_many? @relation == :has_many end |
#has_one? ⇒ Boolean
82 83 84 |
# File 'lib/metamodel/record/association.rb', line 82 def has_one? @relation == :has_one end |
#hash_value ⇒ Object
39 40 41 |
# File 'lib/metamodel/record/association.rb', line 39 def hash_value self.hash.to_s(16) end |
#major_model_id ⇒ Object
31 32 33 |
# File 'lib/metamodel/record/association.rb', line 31 def major_model_id major_model.foreign_id end |
#reverse_class_name ⇒ Object
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_id ⇒ Object
35 36 37 |
# File 'lib/metamodel/record/association.rb', line 35 def secondary_model_id secondary_model.foreign_id end |
#validate_association ⇒ Object
67 68 69 |
# File 'lib/metamodel/record/association.rb', line 67 def validate_association validate_dependent(@dependent) end |
#validate_dependent(dependent) ⇒ Object
71 72 73 74 75 76 |
# File 'lib/metamodel/record/association.rb', line 71 def validate_dependent(dependent) = [:nullify, :destroy] raise Informative, "Unknown dependent option #{dependent}, \ MetaModel only supports #{} now" \ unless .include? dependent end |