Class: JoinCollection

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

Defined Under Namespace

Classes: Doc

Constant Summary collapse

VERSION =
"0.2.0"

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(collection) ⇒ JoinCollection

Returns a new instance of JoinCollection.



12
13
14
15
16
17
18
# File 'lib/join_collection.rb', line 12

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.



10
11
12
# File 'lib/join_collection.rb', line 10

def join_type
  @join_type
end

#plural_targetObject

Returns the value of attribute plural_target.



10
11
12
# File 'lib/join_collection.rb', line 10

def plural_target
  @plural_target
end

#singular_targetObject

Returns the value of attribute singular_target.



10
11
12
# File 'lib/join_collection.rb', line 10

def singular_target
  @singular_target
end

#source_objectsObject (readonly)

Returns the value of attribute source_objects.



9
10
11
# File 'lib/join_collection.rb', line 9

def source_objects
  @source_objects
end

Instance Method Details

#join_many(target, target_class, options) ⇒ Object



37
38
39
40
41
42
43
44
45
46
47
# File 'lib/join_collection.rb', line 37

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



32
33
34
35
# File 'lib/join_collection.rb', line 32

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



20
21
22
23
24
25
26
27
28
29
30
# File 'lib/join_collection.rb', line 20

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