Class: JoinCollection
- Inherits:
-
Object
- Object
- JoinCollection
- Defined in:
- lib/join_collection.rb
Defined Under Namespace
Classes: Doc
Constant Summary collapse
- VERSION =
"0.2.0"
Instance Attribute Summary collapse
-
#join_type ⇒ Object
Returns the value of attribute join_type.
-
#plural_target ⇒ Object
Returns the value of attribute plural_target.
-
#singular_target ⇒ Object
Returns the value of attribute singular_target.
-
#source_objects ⇒ Object
readonly
Returns the value of attribute source_objects.
Instance Method Summary collapse
-
#initialize(collection) ⇒ JoinCollection
constructor
A new instance of JoinCollection.
- #join_many(target, target_class, options) ⇒ Object
- #join_one(target, target_class, options) ⇒ Object
- #join_to(target, target_class, options) ⇒ Object
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_type ⇒ Object
Returns the value of attribute join_type.
10 11 12 |
# File 'lib/join_collection.rb', line 10 def join_type @join_type end |
#plural_target ⇒ Object
Returns the value of attribute plural_target.
10 11 12 |
# File 'lib/join_collection.rb', line 10 def plural_target @plural_target end |
#singular_target ⇒ Object
Returns the value of attribute singular_target.
10 11 12 |
# File 'lib/join_collection.rb', line 10 def singular_target @singular_target end |
#source_objects ⇒ Object (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, ) self.join_type = :join_many unless self.join_type == :join_one fk, pk, delegate_if, delegate_fields = (target, ) 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, ) self.join_type = :join_one join_many(target, target_class, ) 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, ) self.join_type = :join_to fk, pk, delegate_if, delegate_fields = (target, ) 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 |