forked from kennethkalmer/correlate
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
'some' relationships from ActiveRecord to CouchDB implemented
- Loading branch information
1 parent
d3c8870
commit 76614fe
Showing
11 changed files
with
350 additions
and
85 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
module Correlate | ||
module Relationships | ||
class ActiveRecord | ||
|
||
autoload :CollectionProxy, 'correlate/relationships/active_record/collection_proxy' | ||
|
||
class << self | ||
|
||
def configure!( klass, &block ) | ||
|
||
# Setup our conveniences | ||
relationships = new( klass ) | ||
relationships.instance_eval( &block ) | ||
end | ||
|
||
end | ||
|
||
def initialize( klass ) | ||
@klass = klass | ||
end | ||
|
||
def some( *args ) | ||
name = args.shift | ||
opts = args.empty? ? {} : args.last | ||
opts[:source] = @klass | ||
|
||
correlation = Correlate::Relationships.build_correlation( name, :some, opts ) | ||
@klass.correlations << correlation | ||
|
||
@klass.class_eval <<-EOF, __FILE__, __LINE__ | ||
def #{name}( raw = false ) | ||
local_correlation = self.class.correlations.detect { |c| c.name == :#{name} } | ||
Correlate::Relationships::ActiveRecord::CollectionProxy.new self, local_correlation | ||
end | ||
EOF | ||
end | ||
|
||
def a( *args ) | ||
name = args.shift | ||
opts = args.empty? ? {} : args.last | ||
opts[:source] = @klass | ||
|
||
@klass.correlations << Correlate::Relationships.build_correlation( name, :a, opts ) | ||
|
||
@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.links.correlation_for_object( correlation ).correlate( correlation ) unless raw | ||
correlation | ||
end | ||
EOF | ||
end | ||
|
||
end | ||
end | ||
end |
29 changes: 29 additions & 0 deletions
29
lib/correlate/relationships/active_record/collection_proxy.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
module Correlate | ||
module Relationships | ||
class ActiveRecord | ||
class CollectionProxy | ||
|
||
instance_methods.each { |m| undef_method m unless m =~ /(^__|^send$|^object_id$)/ } | ||
|
||
def initialize( source, correlation ) | ||
@source = source | ||
@correlation = correlation | ||
@collection = correlation.correlate( source ) | ||
end | ||
|
||
def <<( object ) | ||
correlation = @correlation.opposite | ||
|
||
object.send( "#{correlation.name}=", @source ) | ||
object.save | ||
end | ||
|
||
protected | ||
|
||
def method_missing( name, *args, &block ) | ||
@collection.send( name, *args, &block ) | ||
end | ||
end | ||
end | ||
end | ||
end |
Oops, something went wrong.