Class: Correlate::Relationships::CouchRest
- Inherits:
-
Object
- Object
- Correlate::Relationships::CouchRest
- 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.
Class Method Summary collapse
Instance Method Summary collapse
-
#a(name, options = {}) ⇒ Object
Specify that this document will have a single document of said relationship.
- #build_validators ⇒ Object
- #build_views ⇒ Object
-
#initialize(klass) ⇒ CouchRest
constructor
A new instance of CouchRest.
-
#some(name, options = {}) ⇒ Object
Specify that this document will have multiple documents of said relationship.
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.
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, = {} ) [:source] = @klass @klass.correlations << Correlate::Relationships.build_correlation( name, :a, ) @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_validators ⇒ Object
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_views ⇒ Object
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.
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, = {} ) [:source] = @klass @klass.correlations << Correlate::Relationships.build_correlation( name, :some, ) @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 |