Class: Dart::DirectAssociation
- Inherits:
-
Association
- Object
- Association
- Dart::DirectAssociation
- Defined in:
- lib/dart/core/direct_association.rb
Direct Known Subclasses
ManyToOneAssociation, OneToManyAssociation, OneToOneAssociation
Constant Summary collapse
- ATTRIBUTES =
[:child_table, :foreign_key, :parent_table, :primary_key]
Constants inherited from Association
Association::MANY_TO_MANY_TYPE, Association::MANY_TO_ONE_TYPE, Association::ONE_TO_MANY_TYPE, Association::ONE_TO_ONE_TYPE
Instance Attribute Summary
Attributes inherited from Association
Instance Method Summary collapse
- #eql?(other) ⇒ Boolean (also: #==)
- #hash ⇒ Object
-
#initialize(*args) ⇒ DirectAssociation
constructor
Constructs a DirectAssociation from the given attributes, which must correspond to ATTRIBUTES defined above given either as separate arguments, a Hash, or an object that responds to methods corresponding to ATTRIBUTES.
- #to_s ⇒ Object
Methods inherited from Association
Constructor Details
#initialize(*args) ⇒ DirectAssociation
Constructs a DirectAssociation from the given attributes, which must correspond to ATTRIBUTES defined above given either as separate arguments, a Hash, or an object that responds to methods corresponding to ATTRIBUTES
8 9 10 11 12 13 14 15 16 17 18 19 20 |
# File 'lib/dart/core/direct_association.rb', line 8 def initialize(*args) if args.length == 1 obj_or_hash = args.first obj = obj_or_hash.is_a?(Hash) ? OpenStruct.new(obj_or_hash) : obj_or_hash else obj = OpenStruct.new ATTRIBUTES.each_with_index {|a, i| obj.send("#{a}=", args[i])} end @child_table = obj.child_table.to_s.freeze or raise "No child table in #{obj_or_hash}" @foreign_key = obj.foreign_key.to_s.freeze or raise "No foreign_key in #{obj_or_hash}" @parent_table = obj.parent_table.to_s.freeze or raise "No parent_table in #{obj_or_hash}" @primary_key = obj.primary_key.to_s.freeze or raise "No primary_key in #{obj_or_hash}" end |
Instance Method Details
#eql?(other) ⇒ Boolean Also known as: ==
22 23 24 |
# File 'lib/dart/core/direct_association.rb', line 22 def eql?(other) ATTRIBUTES.all? { |f| send(f) == other.send(f) } end |
#hash ⇒ Object
27 28 29 |
# File 'lib/dart/core/direct_association.rb', line 27 def hash ATTRIBUTES.map { |a| send(a) }.hash end |
#to_s ⇒ Object
31 32 33 |
# File 'lib/dart/core/direct_association.rb', line 31 def to_s "#{self.class} #{ATTRIBUTES.map { |a| "#{a}: #{send(a)}" }.join(', ')}" end |