associationist 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/lib/associationist.rb +11 -0
- data/lib/associationist/associations/preloader/singular_association.rb +26 -0
- data/lib/associationist/associations/singular_association.rb +27 -0
- data/lib/associationist/builder/singular_association.rb +33 -0
- data/lib/associationist/config.rb +29 -0
- data/lib/associationist/mixin.rb +19 -0
- data/lib/associationist/reflection/singular_reflection.rb +17 -0
- data/lib/associationist/version.rb +3 -0
- metadata +128 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: b0bc8ea37a255d5c211067c307e3d3631837eadc
|
4
|
+
data.tar.gz: 592aed6acd9b5384034cfe8dcd7576b363517696
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 48e98734a36878f10ddc314047e60755e12e0d73a619032cccabc29297dbfa8aeae63981460a63b194c19a8f8f2b9e7fe614646d5cdf78b83cfaffb6099fa228
|
7
|
+
data.tar.gz: 0d7b9212cff0d47d04d2f4828efccdf35bc80dc1fc28d7009e5c645358bdae3673adf255087067638e992756464507d961782e1705542cf2143120fa1b28c8b8
|
@@ -0,0 +1,11 @@
|
|
1
|
+
require 'active_record'
|
2
|
+
|
3
|
+
Dir.glob("#{File.dirname(__FILE__)}/**/*.rb").each do |file|
|
4
|
+
require file
|
5
|
+
end
|
6
|
+
|
7
|
+
module Associationist
|
8
|
+
def self.preload records, associations
|
9
|
+
ActiveRecord::Associations::Preloader.new.preload records, associations
|
10
|
+
end
|
11
|
+
end
|
@@ -0,0 +1,26 @@
|
|
1
|
+
module Associationist
|
2
|
+
module Associations
|
3
|
+
module Preloader
|
4
|
+
class SingularAssociation < ActiveRecord::Associations::Preloader::SingularAssociation
|
5
|
+
def associated_records_by_owner preloader
|
6
|
+
reflection.config.preloader_proc.call(owners).map{|k, v| [k, [v]]}
|
7
|
+
end
|
8
|
+
|
9
|
+
end
|
10
|
+
|
11
|
+
end
|
12
|
+
end
|
13
|
+
|
14
|
+
module ActiveRecordPreloaderPatch
|
15
|
+
def preloader_for(reflection, owners, rhs_klass)
|
16
|
+
config = reflection.options[:associationist]
|
17
|
+
if config
|
18
|
+
Associationist::Associations::Preloader::SingularAssociation
|
19
|
+
else
|
20
|
+
super
|
21
|
+
end
|
22
|
+
end
|
23
|
+
end
|
24
|
+
|
25
|
+
ActiveRecord::Associations::Preloader.prepend ActiveRecordPreloaderPatch
|
26
|
+
end
|
@@ -0,0 +1,27 @@
|
|
1
|
+
module Associationist
|
2
|
+
module Associations
|
3
|
+
class SingularAssociation < ::ActiveRecord::Associations::SingularAssociation
|
4
|
+
def scope
|
5
|
+
raise NotImplementedError
|
6
|
+
end
|
7
|
+
|
8
|
+
def find_target
|
9
|
+
reflection.config.loader_proc.call(owner)
|
10
|
+
end
|
11
|
+
|
12
|
+
def find_target?
|
13
|
+
!loaded? && !owner.new_record?
|
14
|
+
end
|
15
|
+
|
16
|
+
def klass
|
17
|
+
Object
|
18
|
+
end
|
19
|
+
|
20
|
+
def force_reload_reader
|
21
|
+
reload
|
22
|
+
target
|
23
|
+
end
|
24
|
+
|
25
|
+
end
|
26
|
+
end
|
27
|
+
end
|
@@ -0,0 +1,33 @@
|
|
1
|
+
module Associationist
|
2
|
+
module Builder
|
3
|
+
class SingularAssociation < ::ActiveRecord::Associations::Builder::SingularAssociation
|
4
|
+
def self.valid_options(options)
|
5
|
+
super + [:associationist]
|
6
|
+
end
|
7
|
+
|
8
|
+
def self.define_accessors(model, reflection)
|
9
|
+
super
|
10
|
+
mixin = model.generated_association_methods
|
11
|
+
name = reflection.name
|
12
|
+
|
13
|
+
define_constructors(mixin, name) if reflection.constructable?
|
14
|
+
|
15
|
+
mixin.class_eval <<-CODE, __FILE__, __LINE__ + 1
|
16
|
+
def reload_#{name}
|
17
|
+
association(:#{name}).force_reload_reader
|
18
|
+
end
|
19
|
+
CODE
|
20
|
+
end
|
21
|
+
|
22
|
+
def self.create_reflection(model, name, scope, options, extension = nil)
|
23
|
+
raise ArgumentError, "association names must be a Symbol" unless name.kind_of?(Symbol)
|
24
|
+
|
25
|
+
validate_options(options)
|
26
|
+
|
27
|
+
scope = build_scope(scope, extension)
|
28
|
+
Reflection::SingularReflection.new(name, scope, options, model)
|
29
|
+
end
|
30
|
+
|
31
|
+
end
|
32
|
+
end
|
33
|
+
end
|
@@ -0,0 +1,29 @@
|
|
1
|
+
module Associationist
|
2
|
+
class Config
|
3
|
+
def initialize config
|
4
|
+
@config = config
|
5
|
+
end
|
6
|
+
|
7
|
+
def name
|
8
|
+
@config[:name]
|
9
|
+
end
|
10
|
+
|
11
|
+
def loader_proc
|
12
|
+
if @config[:loader]
|
13
|
+
@config[:loader]
|
14
|
+
elsif preloader_proc
|
15
|
+
-> record {
|
16
|
+
(preloader_proc.call [record])[record]
|
17
|
+
}
|
18
|
+
end
|
19
|
+
end
|
20
|
+
|
21
|
+
def preloader_proc
|
22
|
+
@config[:preloader]
|
23
|
+
end
|
24
|
+
|
25
|
+
def type
|
26
|
+
@config[:type]
|
27
|
+
end
|
28
|
+
end
|
29
|
+
end
|
@@ -0,0 +1,19 @@
|
|
1
|
+
module Associationist
|
2
|
+
class Mixin < Module
|
3
|
+
def initialize raw_config
|
4
|
+
@raw_config = raw_config
|
5
|
+
end
|
6
|
+
|
7
|
+
def included base
|
8
|
+
config = Config.new @raw_config
|
9
|
+
reflection_options = {associationist: config}
|
10
|
+
|
11
|
+
reflection = Builder::SingularAssociation.build(base, config.name, nil, reflection_options)
|
12
|
+
::ActiveRecord::Reflection.add_reflection base, config.name, reflection
|
13
|
+
end
|
14
|
+
|
15
|
+
def inspect
|
16
|
+
"#<Associationist::Mixin @name=#{@raw_config[:name].inspect}>"
|
17
|
+
end
|
18
|
+
end
|
19
|
+
end
|
@@ -0,0 +1,17 @@
|
|
1
|
+
module Associationist
|
2
|
+
module Reflection
|
3
|
+
class SingularReflection < ::ActiveRecord::Reflection::HasOneReflection
|
4
|
+
def association_class
|
5
|
+
Associations::SingularAssociation
|
6
|
+
end
|
7
|
+
|
8
|
+
def check_eager_loadable!
|
9
|
+
|
10
|
+
end
|
11
|
+
|
12
|
+
def config
|
13
|
+
options[:associationist]
|
14
|
+
end
|
15
|
+
end
|
16
|
+
end
|
17
|
+
end
|
metadata
ADDED
@@ -0,0 +1,128 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: associationist
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- CicholGricenchos
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2018-04-02 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: activerecord
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - ">="
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '5.0'
|
20
|
+
- - "<"
|
21
|
+
- !ruby/object:Gem::Version
|
22
|
+
version: '6.0'
|
23
|
+
type: :runtime
|
24
|
+
prerelease: false
|
25
|
+
version_requirements: !ruby/object:Gem::Requirement
|
26
|
+
requirements:
|
27
|
+
- - ">="
|
28
|
+
- !ruby/object:Gem::Version
|
29
|
+
version: '5.0'
|
30
|
+
- - "<"
|
31
|
+
- !ruby/object:Gem::Version
|
32
|
+
version: '6.0'
|
33
|
+
- !ruby/object:Gem::Dependency
|
34
|
+
name: pry
|
35
|
+
requirement: !ruby/object:Gem::Requirement
|
36
|
+
requirements:
|
37
|
+
- - ">="
|
38
|
+
- !ruby/object:Gem::Version
|
39
|
+
version: '0'
|
40
|
+
type: :development
|
41
|
+
prerelease: false
|
42
|
+
version_requirements: !ruby/object:Gem::Requirement
|
43
|
+
requirements:
|
44
|
+
- - ">="
|
45
|
+
- !ruby/object:Gem::Version
|
46
|
+
version: '0'
|
47
|
+
- !ruby/object:Gem::Dependency
|
48
|
+
name: sqlite3
|
49
|
+
requirement: !ruby/object:Gem::Requirement
|
50
|
+
requirements:
|
51
|
+
- - ">="
|
52
|
+
- !ruby/object:Gem::Version
|
53
|
+
version: '0'
|
54
|
+
type: :development
|
55
|
+
prerelease: false
|
56
|
+
version_requirements: !ruby/object:Gem::Requirement
|
57
|
+
requirements:
|
58
|
+
- - ">="
|
59
|
+
- !ruby/object:Gem::Version
|
60
|
+
version: '0'
|
61
|
+
- !ruby/object:Gem::Dependency
|
62
|
+
name: database_cleaner
|
63
|
+
requirement: !ruby/object:Gem::Requirement
|
64
|
+
requirements:
|
65
|
+
- - ">="
|
66
|
+
- !ruby/object:Gem::Version
|
67
|
+
version: '0'
|
68
|
+
type: :development
|
69
|
+
prerelease: false
|
70
|
+
version_requirements: !ruby/object:Gem::Requirement
|
71
|
+
requirements:
|
72
|
+
- - ">="
|
73
|
+
- !ruby/object:Gem::Version
|
74
|
+
version: '0'
|
75
|
+
- !ruby/object:Gem::Dependency
|
76
|
+
name: rake
|
77
|
+
requirement: !ruby/object:Gem::Requirement
|
78
|
+
requirements:
|
79
|
+
- - ">="
|
80
|
+
- !ruby/object:Gem::Version
|
81
|
+
version: '0'
|
82
|
+
type: :development
|
83
|
+
prerelease: false
|
84
|
+
version_requirements: !ruby/object:Gem::Requirement
|
85
|
+
requirements:
|
86
|
+
- - ">="
|
87
|
+
- !ruby/object:Gem::Version
|
88
|
+
version: '0'
|
89
|
+
description: ''
|
90
|
+
email:
|
91
|
+
- cichol@live.cn
|
92
|
+
executables: []
|
93
|
+
extensions: []
|
94
|
+
extra_rdoc_files: []
|
95
|
+
files:
|
96
|
+
- lib/associationist.rb
|
97
|
+
- lib/associationist/associations/preloader/singular_association.rb
|
98
|
+
- lib/associationist/associations/singular_association.rb
|
99
|
+
- lib/associationist/builder/singular_association.rb
|
100
|
+
- lib/associationist/config.rb
|
101
|
+
- lib/associationist/mixin.rb
|
102
|
+
- lib/associationist/reflection/singular_reflection.rb
|
103
|
+
- lib/associationist/version.rb
|
104
|
+
homepage: https://github.com/CicholGricenchos/associationist
|
105
|
+
licenses:
|
106
|
+
- MIT
|
107
|
+
metadata: {}
|
108
|
+
post_install_message:
|
109
|
+
rdoc_options: []
|
110
|
+
require_paths:
|
111
|
+
- lib
|
112
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
113
|
+
requirements:
|
114
|
+
- - ">="
|
115
|
+
- !ruby/object:Gem::Version
|
116
|
+
version: '0'
|
117
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
118
|
+
requirements:
|
119
|
+
- - ">="
|
120
|
+
- !ruby/object:Gem::Version
|
121
|
+
version: '0'
|
122
|
+
requirements: []
|
123
|
+
rubyforge_project:
|
124
|
+
rubygems_version: 2.5.2.1
|
125
|
+
signing_key:
|
126
|
+
specification_version: 4
|
127
|
+
summary: ''
|
128
|
+
test_files: []
|