Class: JoinCollection
- Inherits:
-
Object
- Object
- JoinCollection
- Defined in:
- lib/join_collection.rb
Defined Under Namespace
Classes: Doc
Constant Summary collapse
- VERSION =
"0.3.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.
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_type ⇒ Object
Returns the value of attribute join_type.
14 15 16 |
# File 'lib/join_collection.rb', line 14 def join_type @join_type end |
#plural_target ⇒ Object
Returns the value of attribute plural_target.
14 15 16 |
# File 'lib/join_collection.rb', line 14 def plural_target @plural_target end |
#singular_target ⇒ Object
Returns the value of attribute singular_target.
14 15 16 |
# File 'lib/join_collection.rb', line 14 def singular_target @singular_target end |
#source_objects ⇒ Object (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, ) 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
36 37 38 39 |
# File 'lib/join_collection.rb', line 36 def join_one(target, target_class, ) self.join_type = :join_one join_many(target, target_class, ) 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, ) 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 |