Class: ActiveAny::Associations::HasManyAssociation

Inherits:
Association
  • Object
show all
Defined in:
lib/active_any/associations/has_many_association.rb

Instance Attribute Summary

Attributes inherited from Association

#inversed, #loaded, #owner, #reflection, #target

Instance Method Summary collapse

Methods inherited from Association

#association_scope, #find_from_target?, #initialize, #klass, #load_target, #loaded!, #loaded?, #reload, #reset_scope, #scope, #set_inverse_instance, #target_scope

Constructor Details

This class inherits a constructor from ActiveAny::Associations::Association

Instance Method Details

#empty?Boolean

Returns:

  • (Boolean)


31
32
33
# File 'lib/active_any/associations/has_many_association.rb', line 31

def empty?
  size.zero?
end

#find_targetObject



43
44
45
# File 'lib/active_any/associations/has_many_association.rb', line 43

def find_target
  scope.to_a
end

#include?(record) ⇒ Boolean

Returns:

  • (Boolean)


35
36
37
38
39
40
41
# File 'lib/active_any/associations/has_many_association.rb', line 35

def include?(record)
  if record.is_a?(klass)
    target.include?(record)
  else
    false
  end
end

#readerObject



6
7
8
9
10
11
12
# File 'lib/active_any/associations/has_many_association.rb', line 6

def reader
  # TODO: implement
  # reload if stale_target?

  @proxy ||= CollectionProxy.create(owner, self)
  @proxy.reset_scope
end

#resetObject



18
19
20
21
# File 'lib/active_any/associations/has_many_association.rb', line 18

def reset
  super
  @target = []
end

#sizeObject



23
24
25
26
27
28
29
# File 'lib/active_any/associations/has_many_association.rb', line 23

def size
  if !find_target? || loaded?
    target.size
  else
    scope.count
  end
end

#writer(_records) ⇒ Object

Raises:

  • (NotImplementedError.new)


14
15
16
# File 'lib/active_any/associations/has_many_association.rb', line 14

def writer(_records)
  raise NotImplementedError.new, 'writer is unimplemented'
end