rails_optimizer 0.1.8 → 0.1.9
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/Gemfile.lock +1 -1
- data/lib/rails_optimizer.rb +6 -10
- data/lib/rails_optimizer/association.rb +23 -0
- data/lib/rails_optimizer/belongs_to.rb +7 -24
- data/lib/rails_optimizer/has_one.rb +35 -0
- data/lib/rails_optimizer/version.rb +1 -1
- metadata +4 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: db80c5f0e72c9cb288873b9f318e5dc3baafdd67
|
4
|
+
data.tar.gz: 0a101ae1ad6cb41b9243aebbd8760ed61c74ce7b
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 76a6df566e037c10afd6c2b79567ae9bb1fcebbc440a3cbad10ea66cf5cb0512faf3f53ba1aab51049736c6af69588eaf2646400f3ebaf9cb10bb01d850690b4
|
7
|
+
data.tar.gz: 8fec9fdae029505fdfd1d4814ea618a751649d69f643619310236704df2081a668ad809bcce459c5f8c1be88419993e5fc10910df63364e67915da800b05c408
|
data/Gemfile.lock
CHANGED
data/lib/rails_optimizer.rb
CHANGED
@@ -1,5 +1,7 @@
|
|
1
1
|
require "rails_optimizer/version"
|
2
|
+
require "rails_optimizer/association"
|
2
3
|
require "rails_optimizer/belongs_to"
|
4
|
+
require "rails_optimizer/has_one"
|
3
5
|
require "rails_optimizer/reflection"
|
4
6
|
|
5
7
|
module RailsOptimizer
|
@@ -17,14 +19,7 @@ module RailsOptimizer
|
|
17
19
|
|
18
20
|
def self.has_one(name, scope = nil, **options)
|
19
21
|
super
|
20
|
-
|
21
|
-
fk = self.class.foreign_key(name).to_sym
|
22
|
-
if args.empty?
|
23
|
-
name.to_s.classify.constantize.select("*").scoped(scope).find_by(fk => id)
|
24
|
-
else
|
25
|
-
name.to_s.classify.constantize.select(:id, *args).scoped(scope).find_by(fk => id)
|
26
|
-
end
|
27
|
-
end
|
22
|
+
RailsOptimizer::HasOne._define(self,name)
|
28
23
|
end
|
29
24
|
|
30
25
|
def self.belongs_to(name, scope = nil, **options)
|
@@ -32,8 +27,9 @@ module RailsOptimizer
|
|
32
27
|
RailsOptimizer::BelongsTo._define(self, name)
|
33
28
|
end
|
34
29
|
|
35
|
-
def self.execute(&
|
36
|
-
|
30
|
+
def self.execute(&method)
|
31
|
+
return all unless block_given?
|
32
|
+
class_eval(&method)
|
37
33
|
end
|
38
34
|
|
39
35
|
end
|
@@ -0,0 +1,23 @@
|
|
1
|
+
module RailsOptimizer
|
2
|
+
class Association
|
3
|
+
attr_accessor :klass, :name, :args, :reflection, :owner
|
4
|
+
|
5
|
+
def initialize(owner, name, *args)
|
6
|
+
@owner = owner
|
7
|
+
@name = name.to_s
|
8
|
+
@args = *args
|
9
|
+
end
|
10
|
+
|
11
|
+
def reflection
|
12
|
+
@reflection ||= RailsOptimizer::Reflection.new(owner, name)
|
13
|
+
end
|
14
|
+
|
15
|
+
def klass
|
16
|
+
reflection.klass
|
17
|
+
end
|
18
|
+
|
19
|
+
def reflection_scope
|
20
|
+
reflection.scope
|
21
|
+
end
|
22
|
+
end
|
23
|
+
end
|
@@ -1,12 +1,5 @@
|
|
1
1
|
module RailsOptimizer
|
2
|
-
class BelongsTo
|
3
|
-
attr_accessor :klass, :name, :args, :reflection, :owner
|
4
|
-
|
5
|
-
def initialize(owner, name, *args)
|
6
|
-
@owner = owner
|
7
|
-
@name = name.to_s
|
8
|
-
@args = *args
|
9
|
-
end
|
2
|
+
class BelongsTo < Association
|
10
3
|
|
11
4
|
def self._define(owner, name )
|
12
5
|
owner.__send__(:define_method, name) do |*args|
|
@@ -20,25 +13,15 @@ module RailsOptimizer
|
|
20
13
|
else
|
21
14
|
klass.select(*args)
|
22
15
|
end
|
23
|
-
.execute
|
24
|
-
.execute
|
16
|
+
.execute(&reflection_scope)
|
17
|
+
.execute(&finded)
|
25
18
|
end
|
26
19
|
|
27
20
|
private
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
def klass
|
33
|
-
reflection.klass
|
34
|
-
end
|
35
|
-
|
36
|
-
def reflection_scope
|
37
|
-
reflection.scope
|
38
|
-
end
|
39
|
-
|
40
|
-
def finded(obj)
|
41
|
-
obj.find( owner.read_attribute(foreign_key) )
|
21
|
+
|
22
|
+
def finded
|
23
|
+
id = owner.read_attribute(foreign_key)
|
24
|
+
proc { send(:find, id) }
|
42
25
|
end
|
43
26
|
|
44
27
|
def foreign_key
|
@@ -0,0 +1,35 @@
|
|
1
|
+
module RailsOptimizer
|
2
|
+
class HasOne < Association
|
3
|
+
|
4
|
+
|
5
|
+
def self._define(owner, name )
|
6
|
+
owner.__send__(:define_method, name) do |*args|
|
7
|
+
return RailsOptimizer::HasOne.new(self, name, *args).get_target
|
8
|
+
end
|
9
|
+
end
|
10
|
+
|
11
|
+
def get_target
|
12
|
+
if args.empty?
|
13
|
+
klass
|
14
|
+
else
|
15
|
+
klass.select(*args)
|
16
|
+
end.execute(&reflection_scope).execute(&finded)
|
17
|
+
end
|
18
|
+
|
19
|
+
private
|
20
|
+
|
21
|
+
def finded
|
22
|
+
finder = {reflection.foreign_key.to_sym => owner.id}
|
23
|
+
proc { send(:find_by, **finder) }
|
24
|
+
end
|
25
|
+
end
|
26
|
+
|
27
|
+
define_method name.to_s do |*args|
|
28
|
+
fk = self.class.foreign_key(name).to_sym
|
29
|
+
if args.empty?
|
30
|
+
name.to_s.classify.constantize.select("*").scoped(scope).find_by(fk => id)
|
31
|
+
else
|
32
|
+
name.to_s.classify.constantize.select(:id, *args).scoped(scope).find_by(fk => id)
|
33
|
+
end
|
34
|
+
end
|
35
|
+
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: rails_optimizer
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.1.
|
4
|
+
version: 0.1.9
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Facundo A. Díaz Martínez
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date: 2021-01-
|
11
|
+
date: 2021-01-10 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: activerecord
|
@@ -57,7 +57,9 @@ files:
|
|
57
57
|
- bin/console
|
58
58
|
- bin/setup
|
59
59
|
- lib/rails_optimizer.rb
|
60
|
+
- lib/rails_optimizer/association.rb
|
60
61
|
- lib/rails_optimizer/belongs_to.rb
|
62
|
+
- lib/rails_optimizer/has_one.rb
|
61
63
|
- lib/rails_optimizer/reflection.rb
|
62
64
|
- lib/rails_optimizer/version.rb
|
63
65
|
- rails_optimizer.gemspec
|