Class: Correlate::Correlation

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#id_methodObject

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

#nameObject

Name of the correlation



7
8
9
# File 'lib/correlate/correlation.rb', line 7

def name
  @name
end

#relObject

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

#requiredObject

For a relationships specify if required



25
26
27
# File 'lib/correlate/correlation.rb', line 25

def required
  @required
end

#requiresObject

For some relationships specify how many instances are required



22
23
24
# File 'lib/correlate/correlation.rb', line 22

def requires
  @requires
end

#sourceObject

Class owning the correlation



4
5
6
# File 'lib/correlate/correlation.rb', line 4

def source
  @source
end

#targetObject

Class the relationship targets



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

def target
  @target
end

#typeObject

Type of relationship



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

def type
  @type
end

#viewObject

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)

Returns:

  • (Boolean)


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

#directionHash

Conveniently show the ‘direction’ of the correlation in terms of classes

Returns:

  • (Hash)

    { :couchdb => :activerecord } or vice versa or couch to couch.



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

Returns:

  • (Boolean)


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

#oppositeObject

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_classObject



104
105
106
# File 'lib/correlate/correlation.rb', line 104

def source_class
  source
end

#target_classObject



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