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.
-
#through ⇒ Object
Returns the value of attribute through.
Validation collapse
- #validate_association ⇒ Object
- #validate_dependent(dependent) ⇒ Object
- #validate_through(through) ⇒ Object
Relation collapse
Instance Method Summary collapse
- #expect_constraint?(constraint) ⇒ Boolean
-
#initialize(name, major_model, secondary_model, relation, args) ⇒ Association
constructor
A new instance of Association.
- #secondary_model_instance ⇒ Object
Constructor Details
#initialize(name, major_model, secondary_model, relation, args) ⇒ Association
Returns a new instance of Association.
12 13 14 15 16 17 18 19 20 21 22 23 24 |
# File 'lib/metamodel/record/association.rb', line 12 def initialize(name, major_model, secondary_model, relation, args) through = args[:through] dependent = args[:dependent] || :nullify @name = name.to_s.camelize :lower @relation = relation @through = through.to_s.camelize.singularize unless through.nil? @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 |
#through ⇒ Object
Returns the value of attribute through.
10 11 12 |
# File 'lib/metamodel/record/association.rb', line 10 def through @through 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
91 92 93 |
# File 'lib/metamodel/record/association.rb', line 91 def belongs_to? @relation == :belongs_to end |
#debug_description ⇒ Object
101 102 103 104 105 106 107 |
# File 'lib/metamodel/record/association.rb', line 101 def debug_description if through "#{major_model.name}.#{relation}.#{secondary_model.name}.through.#{through.name}.#{dependent}" else "#{major_model.name}.#{relation}.#{secondary_model.name}.#{dependent}" end end |
#expect_constraint?(constraint) ⇒ Boolean
26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 |
# File 'lib/metamodel/record/association.rb', line 26 def expect_constraint?(constraint) result = true result &= self.major_model == constraint.secondary_model result &= self.secondary_model == constraint.major_model result &= self.through == constraint.through 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 unless constraint.through return false when [:has_many, :belongs_to] then return false if constraint.dependent == :destroy return true unless self.through return false when [:has_many, :has_many] then self.through == constraint.through else false end result end |
#has_many? ⇒ Boolean
87 88 89 |
# File 'lib/metamodel/record/association.rb', line 87 def has_many? @relation == :has_many end |
#has_one? ⇒ Boolean
83 84 85 |
# File 'lib/metamodel/record/association.rb', line 83 def has_one? @relation == :has_one end |
#secondary_model_instance ⇒ Object
49 50 51 52 53 54 55 |
# File 'lib/metamodel/record/association.rb', line 49 def secondary_model_instance case relation when :has_many, :has_one then "#{secondary_model.name}.find(privateId)" when :belongs_to then "#{secondary_model.name}.find(#{secondary_model.foreign_id})" else "" end end |
#validate_association ⇒ Object
61 62 63 64 |
# File 'lib/metamodel/record/association.rb', line 61 def validate_association validate_dependent(@dependent) validate_through(@through) end |
#validate_dependent(dependent) ⇒ Object
66 67 68 69 70 71 |
# File 'lib/metamodel/record/association.rb', line 66 def validate_dependent(dependent) = [:nullify, :destroy] raise Informative, "Unknown dependent option #{dependent}, \ MetaModel only supports #{supported_dependent_options} now" \ unless .include? dependent end |
#validate_through(through) ⇒ Object
73 74 75 76 |
# File 'lib/metamodel/record/association.rb', line 73 def validate_through(through) raise Informative, "belongs_to can't coexist with through." \ if !!through && @relation == :belongs_to end |