-
Notifications
You must be signed in to change notification settings - Fork 24
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Move all persistence bindings to adapters
- Loading branch information
1 parent
f1f2240
commit 56eaf84
Showing
27 changed files
with
406 additions
and
126 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
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
26 changes: 0 additions & 26 deletions
26
lib/active_data/model/associations/collection/referenced.rb
This file was deleted.
Oops, something went wrong.
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
62 changes: 62 additions & 0 deletions
62
lib/active_data/model/associations/persistence_adapters/active_record.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,62 @@ | ||
require 'active_data/model/associations/persistence_adapters/active_record/referenced_proxy' | ||
|
||
module ActiveData | ||
module Model | ||
module Associations | ||
module PersistenceAdapters | ||
class ActiveRecord < Base | ||
TYPES = { | ||
integer: Integer, | ||
float: Float, | ||
decimal: BigDecimal, | ||
datetime: Time, | ||
timestamp: Time, | ||
time: Time, | ||
date: Date, | ||
text: String, | ||
string: String, | ||
binary: String, | ||
boolean: Boolean | ||
}.freeze | ||
|
||
alias_method :data_type, :data_source | ||
|
||
def build(attributes) | ||
data_source.new(attributes) | ||
end | ||
|
||
def scope(owner, source) | ||
scope = data_source.unscoped | ||
|
||
if scope_proc | ||
scope = if scope_proc.arity.zero? | ||
scope.instance_exec(&scope_proc) | ||
else | ||
scope.instance_exec(owner, &scope_proc) | ||
end | ||
end | ||
|
||
scope.where(primary_key => source) | ||
end | ||
|
||
def identify(object) | ||
object[primary_key] | ||
end | ||
|
||
def primary_key | ||
@primary_key ||= :id | ||
end | ||
|
||
def primary_key_type | ||
column = data_source.columns_hash[primary_key.to_s] | ||
TYPES[column.type] | ||
end | ||
|
||
def referenced_proxy(association) | ||
ReferencedProxy.new(association) | ||
end | ||
end | ||
end | ||
end | ||
end | ||
end |
31 changes: 31 additions & 0 deletions
31
lib/active_data/model/associations/persistence_adapters/active_record/referenced_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,31 @@ | ||
module ActiveData | ||
module Model | ||
module Associations | ||
module PersistenceAdapters | ||
class ActiveRecord < Base | ||
class ReferencedProxy < ActiveData::Model::Associations::Collection::Proxy | ||
# You can't create data directly through ActiveRecord::Relation | ||
METHODS_EXCLUDED_FROM_DELEGATION = %w(build create create!).map(&:to_sym).freeze | ||
|
||
attr_reader :association | ||
delegate :scope, to: :@association | ||
|
||
def method_missing(method, *args, &block) | ||
delegate_to_scope?(method) ? scope.send(method, *args, &block) : super | ||
end | ||
|
||
def respond_to_missing?(method, include_private = false) | ||
delegate_to_scope?(method) || super | ||
end | ||
|
||
private | ||
|
||
def delegate_to_scope?(method) | ||
METHODS_EXCLUDED_FROM_DELEGATION.exclude?(method) && scope.respond_to?(method) | ||
end | ||
end | ||
end | ||
end | ||
end | ||
end | ||
end |
49 changes: 49 additions & 0 deletions
49
lib/active_data/model/associations/persistence_adapters/base.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,49 @@ | ||
module ActiveData | ||
module Model | ||
module Associations | ||
module PersistenceAdapters | ||
class Base | ||
attr_reader :data_source, :primary_key, :scope_proc | ||
|
||
def initialize(data_source, primary_key, scope_proc = nil) | ||
@data_source = data_source | ||
@primary_key = primary_key | ||
@scope_proc = scope_proc | ||
end | ||
|
||
def build(_attributes) | ||
raise NotImplementedError, 'Should be implemented in inhereted adapter. Build new instance of data object by attributes' | ||
end | ||
|
||
def scope(_owner, _source) | ||
raise NotImplementedError, 'Should be implemented in inhereted adapter. Better to be Enumerable' | ||
end | ||
|
||
def find_one(owner, identificator) | ||
scope(owner, identificator).first | ||
end | ||
|
||
def find_all(owner, identificators) | ||
scope(owner, identificators).to_a | ||
end | ||
|
||
def identify(_object) | ||
raise NotImplementedError, 'Should be implemented in inhereted adapter. Field to be used as primary_key for object' | ||
end | ||
|
||
def data_type | ||
raise NotImplementedError, 'Should be implemented in inhereted adapter. Type of data object for type_check' | ||
end | ||
|
||
def primary_key_type | ||
raise NotImplementedError, 'Should be implemented in inhereted adapter. Ruby data type' | ||
end | ||
|
||
def referenced_proxy | ||
raise NotImplementedError, 'Should be implemented in inhereted adapter. Object to manage proxying of methods to scope.' | ||
end | ||
end | ||
end | ||
end | ||
end | ||
end |
11 changes: 11 additions & 0 deletions
11
lib/active_data/model/associations/reference_association.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,11 @@ | ||
module ActiveData | ||
module Model | ||
module Associations | ||
class ReferenceAssociation < Base | ||
def scope(source = read_source) | ||
reflection.persistence_adapter.scope(owner, source) | ||
end | ||
end | ||
end | ||
end | ||
end |
Oops, something went wrong.