Class: Dart::DirectAssociation

Inherits:
Association show all
Defined in:
lib/dart/core/direct_association.rb

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

#model_class, #name, #scope

Instance Method Summary collapse

Methods inherited from Association

#set_name!, #to_one?

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: ==

Returns:

  • (Boolean)


22
23
24
# File 'lib/dart/core/direct_association.rb', line 22

def eql?(other)
  ATTRIBUTES.all? { |f| send(f) == other.send(f) }
end

#hashObject



27
28
29
# File 'lib/dart/core/direct_association.rb', line 27

def hash
  ATTRIBUTES.map { |a| send(a) }.hash
end

#to_sObject



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