Class: Correlate::Correlation
- Inherits:
-
Object
- Object
- Correlate::Correlation
- Defined in:
- lib/correlate/correlation.rb
Instance Attribute Summary collapse
-
#id_method ⇒ Object
Return the method name used for determining the href value of the correlation.
-
#load_via ⇒ Object
writeonly
Class method on the target class used when loading single instances (defaults to
get
for CouchRest docs, andfind
for ActiveRecord models). -
#name ⇒ Object
Name of the correlation.
-
#rel ⇒ Object
Return the
rel
used for this correlation. -
#required ⇒ Object
For
a
relationships specify if required. -
#requires ⇒ Object
For
some
relationships specify how many instances are required. -
#source ⇒ Object
Class owning the correlation.
-
#target ⇒ Object
Class the relationship targets.
-
#type ⇒ Object
Type of relationship.
-
#view ⇒ Object
Name of a view used to load documents from ActiveRecord’s side.
Instance Method Summary collapse
-
#bidirectional? ⇒ Boolean
Do we have a mutual correlation (ie defined in both classes).
-
#bulk_correlate(objects) ⇒ Object
Correlate the objects from their rel/href values.
-
#correlate(object) ⇒ Object
Correlate the object from its rel/href values.
-
#direction ⇒ Hash
Conveniently show the ‘direction’ of the correlation in terms of classes.
-
#matches?(obj) ⇒ Boolean
Determine if this correlation can be used for the provided object.
-
#opposite ⇒ Object
Determine the ‘reverse correlation’ from the target class.
- #source_class ⇒ Object
- #target_class ⇒ Object
Instance Attribute Details
#id_method ⇒ Object
Return the method name used for determining the href value of the correlation. Defaults to :id
.
19 20 21 |
# File 'lib/correlate/correlation.rb', line 19 def id_method @id_method end |
#load_via=(value) ⇒ Object
Class method on the target class used when loading single instances (defaults to get
for CouchRest docs, and find
for ActiveRecord models)
29 30 31 |
# File 'lib/correlate/correlation.rb', line 29 def load_via=(value) @load_via = value end |
#name ⇒ Object
Name of the correlation
7 8 9 |
# File 'lib/correlate/correlation.rb', line 7 def name @name end |
#rel ⇒ Object
Return the rel
used for this correlation. Uses #name if #rel is nil
16 17 18 |
# File 'lib/correlate/correlation.rb', line 16 def rel @rel end |
#required ⇒ Object
For a
relationships specify if required
25 26 27 |
# File 'lib/correlate/correlation.rb', line 25 def required @required end |
#requires ⇒ Object
For some
relationships specify how many instances are required
22 23 24 |
# File 'lib/correlate/correlation.rb', line 22 def requires @requires end |
#source ⇒ Object
Class owning the correlation
4 5 6 |
# File 'lib/correlate/correlation.rb', line 4 def source @source end |
#target ⇒ Object
Class the relationship targets
13 14 15 |
# File 'lib/correlate/correlation.rb', line 13 def target @target end |
#type ⇒ Object
Type of relationship
10 11 12 |
# File 'lib/correlate/correlation.rb', line 10 def type @type end |
#view ⇒ Object
Name of a view used to load documents from ActiveRecord’s side
32 33 34 |
# File 'lib/correlate/correlation.rb', line 32 def view @view end |
Instance Method Details
#bidirectional? ⇒ Boolean
Do we have a mutual correlation (ie defined in both classes)
78 79 80 |
# File 'lib/correlate/correlation.rb', line 78 def bidirectional? target_class.included_modules.include?( Correlate ) && !opposite.nil? end |
#bulk_correlate(objects) ⇒ Object
Correlate the objects from their rel/href values
71 72 73 74 75 |
# File 'lib/correlate/correlation.rb', line 71 def bulk_correlate( objects ) results = objects.inject([]) do |results, obj| results << target_class.send( load_via, obj['href'] ) end.compact end |
#correlate(object) ⇒ Object
Correlate the object from its rel/href values.
58 59 60 61 62 63 64 65 66 67 68 |
# File 'lib/correlate/correlation.rb', line 58 def correlate( object ) if direction == { :active_record => :couchdb } raise ArgumentError, "#{target} doesn't correlate with #{source}" unless bidirectional? return target_class.by_rel :key => [ opposite.rel, object.send( opposite.id_method ) ] end return target_class.send( load_via, object['href'] ) end |
#direction ⇒ Hash
Conveniently show the ‘direction’ of the correlation in terms of classes
89 90 91 92 93 94 |
# File 'lib/correlate/correlation.rb', line 89 def direction f = source_class.ancestors.include?( CouchRest::ExtendedDocument ) ? :couchdb : :active_record t = target_class.ancestors.include?( CouchRest::ExtendedDocument ) ? :couchdb : :active_record { f => t } end |
#matches?(obj) ⇒ Boolean
Determine if this correlation can be used for the provided object
35 36 37 38 39 40 41 42 43 44 |
# File 'lib/correlate/correlation.rb', line 35 def matches?( obj ) case obj when CouchRest::ExtendedDocument target == obj['couchrest-type'] when Hash obj['rel'] == rel else target == obj.class.name end end |
#opposite ⇒ Object
Determine the ‘reverse correlation’ from the target class
83 84 85 |
# File 'lib/correlate/correlation.rb', line 83 def opposite target_class.correlations.detect { |c| c.target == source.name } end |
#source_class ⇒ Object
104 105 106 |
# File 'lib/correlate/correlation.rb', line 104 def source_class source end |
#target_class ⇒ Object
96 97 98 99 100 101 102 |
# File 'lib/correlate/correlation.rb', line 96 def target_class target.split('::').inject( Object ) do |parent, klass| raise "Class #{klass} not found" if !parent.const_defined?( klass.to_sym ) parent.const_get( klass.to_sym ) end end |