Class: JoinCollection

Inherits:
Object
  • Object
show all
Defined in:
lib/join_collection.rb

Defined Under Namespace

Classes: Doc

Constant Summary collapse

VERSION =
"0.3.0"

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(collection) ⇒ JoinCollection

Returns a new instance of JoinCollection.



16
17
18
19
20
21
22
# File 'lib/join_collection.rb', line 16

def initialize(collection)
  if collection.first.is_a?(Hash)
    @source_objects = collection.map {|x| Doc.new(x)}
  else
    @source_objects = collection
  end
end

Instance Attribute Details

#join_typeObject

Returns the value of attribute join_type.



14
15
16
# File 'lib/join_collection.rb', line 14

def join_type
  @join_type
end

#plural_targetObject

Returns the value of attribute plural_target.



14
15
16
# File 'lib/join_collection.rb', line 14

def plural_target
  @plural_target
end

#singular_targetObject

Returns the value of attribute singular_target.



14
15
16
# File 'lib/join_collection.rb', line 14

def singular_target
  @singular_target
end

#source_objectsObject (readonly)

Returns the value of attribute source_objects.



13
14
15
# File 'lib/join_collection.rb', line 13

def source_objects
  @source_objects
end

Instance Method Details

#join_many(target, target_class, options) ⇒ Object



41
42
43
44
45
46
47
48
49
50
51
# File 'lib/join_collection.rb', line 41

def join_many(target, target_class, options)
  self.join_type = :join_many unless self.join_type == :join_one
  fk, pk, delegate_if, delegate_fields = extract_options(target, options)

  source_pks     = source_objects.map(&pk).compact
  target_objects = target_class.where(fk.in => source_pks).to_a

  mapper = target_objects.group_by(&fk)
  mapper.default = []
  join_data(mapper, pk, delegate_if, delegate_fields)
end

#join_one(target, target_class, options) ⇒ Object



36
37
38
39
# File 'lib/join_collection.rb', line 36

def join_one(target, target_class, options)
  self.join_type = :join_one
  join_many(target, target_class, options)
end

#join_to(target, target_class, options) ⇒ Object



24
25
26
27
28
29
30
31
32
33
34
# File 'lib/join_collection.rb', line 24

def join_to(target, target_class, options)
  self.join_type = :join_to
  fk, pk, delegate_if, delegate_fields = extract_options(target, options)

  source_fks     = source_objects.map(&fk).compact
  target_objects = target_class.where(pk.in => source_fks).to_a

  mapper = target_objects.group_by(&pk)
  mapper.default = []
  join_data(mapper, fk, delegate_if, delegate_fields)
end