Class: Correlate::Relationships::CouchRest

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

Overview

Configure relationships between CouchRest::ExtendedDocument classes, as well as between CouchRest::ExtendedDocument & ActiveRecord::Base classes.

Using this correlation in a CouchRest::ExtendedDocument creates a links property that is used for storing the correlations between this document and others.

It also creates a rel view for the class that emits two keys: rel & href, which is used for bulk lookups and loading of documents.

Notes

To use the validations provided by Correlate you need to include CouchRest::Validation into your classes.

See Also:

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(klass) ⇒ CouchRest

Returns a new instance of CouchRest.



46
47
48
# File 'lib/correlate/relationships/couchrest.rb', line 46

def initialize( klass )
  @klass = klass
end

Class Method Details

.configure!(klass, &block) ⇒ Object



25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/correlate/relationships/couchrest.rb', line 25

def configure!( klass, &block )

  # Add our property
  klass.property :links, :type => 'Correlate::Links', :default => Correlate::Links.new( klass )

  # Setup our conveniences
  relationships = new( klass )
  relationships.instance_eval( &block )
  relationships.build_validators
  relationships.build_views

  # Make sure our links array is properly casted
  klass.class_eval <<-EOF, __FILE__, __LINE__
    def links=( array )
      self[:links] = Correlate::Links.new( self.class, array )
    end
  EOF
end

Instance Method Details

#a(name, options = {}) ⇒ Object

Specify that this document will have a single document of said relationship.

Examples:

Defining correlations and using them

class OtherDocument < CouchRest::ExtendedDocument
  include Correlate

  related_to do
    a :some_document, :class => 'SomeDocument'
  end
end

doc = OtherDocument.new
doc.some_document # => nil

some_doc = SomeDocument.get( '885387e892e63f4b6d31dbc877533099' )
doc.some_document = some_doc

doc.some_document # => [ <SomeDocument#12121212> ]

Parameters:

  • name (Symbol)

    of the relationship

  • options (Hash) (defaults to: {})

    for the relationship

Options Hash (options):

  • :class (String)

    the class of the related document/model

  • :rel (String) — default: name

    the value of the rel key in the related hash

  • :required (Fixnum) — default: false

    whether required or not

  • :id_method (Symbol) — default: :id

    name of a method use to retrieve the ‘foreign key’ value from documents added to the relationship

  • :load_via (Symbol) — default: :get/:find

    name of the class method used to retreive related documents (defaults to :get for CouchRest::ExtendedDocument, :find for ActiveRecord::Base)



124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
# File 'lib/correlate/relationships/couchrest.rb', line 124

def a( name, options = {} )
  options[:source] = @klass

  @klass.correlations << Correlate::Relationships.build_correlation( name, :a, options )

  @klass.class_eval <<-EOF, __FILE__, __LINE__
    def #{name}=( object )
      self.links.replace( object )
    end

    def #{name}( raw = false )
      correlation = self.links.rel( '#{name}' ).first
      return if correlation.nil?

      correlation = self.class.correlation_for( correlation ).correlate( correlation ) unless raw

      correlation
    end
  EOF
end

#build_validatorsObject



146
147
148
149
150
151
152
153
# File 'lib/correlate/relationships/couchrest.rb', line 146

def build_validators
  if @klass.included_modules.include?( ::CouchRest::Validation )

    fields = [ :links ]
    opts = @klass.opts_from_validator_args( fields )
    @klass.add_validator_to_context( opts, fields, Correlate::Validator )
  end
end

#build_viewsObject



156
157
158
159
160
161
162
163
164
165
166
167
# File 'lib/correlate/relationships/couchrest.rb', line 156

def build_views
  @klass.view_by :rel,
    :map => <<-MAP
      function( doc ) {
        if( doc['couchrest-type'] == '#{@klass}' ) {
          doc.links.forEach(function(link) {
            emit([ link.rel, link.href ], 1);
          });
        }
      }
    MAP
end

#some(name, options = {}) ⇒ Object

Specify that this document will have multiple documents of said relationship.

Examples:

Defining correlations and using them

class SomeDocument < CouchRest::ExtendedDocument
  include Correlate

  related_to do
    some :other_documents, :class => 'OtherDocument, :rel => 'other_document'
  end
end

doc = SomeDocument.new
doc.other_documents # => []

other_doc = OtherDocument.get( '885387e892e63f4b6d31dbc877533099' )
doc.other_documents << other_doc

doc.other_documents # => [ <OtherDocument#12121212> ]

Parameters:

  • name (Symbol)

    of the relationship

  • options (Hash) (defaults to: {})

    for the relationship

Options Hash (options):

  • :class (String)

    the class of the related document/model

  • :rel (String)

    the value of the rel key in the related hash

  • :requires (Fixnum) — default: nil

    a number of documents required

  • :id_method (Symbol) — default: :id

    name of a method use to retrieve the ‘foreign key’ value from documents added to the relationship

  • :load_via (Symbol) — default: :get/:find

    name of the class method used to retreive related documents (defaults to :get for CouchRest::ExtendedDocument, :find for ActiveRecord::Base)



76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
# File 'lib/correlate/relationships/couchrest.rb', line 76

def some( name, options = {} )
  options[:source] = @klass

  @klass.correlations << Correlate::Relationships.build_correlation( name, :some, options )

  @klass.class_eval <<-EOF, __FILE__, __LINE__
    def #{name}( raw = false )
      correlations = self.links.rel( '#{name}' )

      #correlations.map! do |c|
      #  self.links.correlation_for_object( c ).correlate( c )
      #end unless raw
      unless raw
        c = self.class.correlation_for( correlations.first )
        correlations = c.bulk_correlate( correlations ) unless c.nil?
      end

      correlations
    end
  EOF
end