cloned 0.1.0 → 0.1.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/.gitignore +1 -0
- data/.rubocop.yml +10 -0
- data/README.md +3 -2
- data/cloned.gemspec +1 -0
- data/lib/cloned.rb +1 -0
- data/lib/cloned/base.rb +10 -5
- data/lib/cloned/destination_proxy.rb +16 -0
- data/lib/cloned/strategy.rb +2 -6
- data/lib/cloned/version.rb +1 -1
- metadata +18 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: abe02254b04e1312e29e831b62f27a1a1fb396bc
|
4
|
+
data.tar.gz: 4571a6152e7e7c873a6ebef6523d6f4c95519611
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 80f45b2ff43c7f8ef610e840457f8c3817098953555b30ddfd5992ed14b8825ae32bb05974d81d18c61e2605b0398a4225a03db96b8c532f2454dc03c9c01679
|
7
|
+
data.tar.gz: 0c468d1b95a9de4aa72f528759d14042f13e1a754a223738b4c45f3ef5f70507d08f452411b101ebee516bcb3c5989c6ba809c56948086d2995b10d2660c813c
|
data/.gitignore
CHANGED
data/.rubocop.yml
ADDED
@@ -0,0 +1,10 @@
|
|
1
|
+
# This configuration was generated by
|
2
|
+
# `rubocop --auto-gen-config`
|
3
|
+
# on 2017-10-29 22:49:50 +0300 using RuboCop version 0.46.0.
|
4
|
+
# The point is for the user to remove these configuration records
|
5
|
+
# one by one as the offenses are removed from the code base.
|
6
|
+
# Note that changes in the inspected code, or installation of new
|
7
|
+
# versions of RuboCop, may require this file to be generated again.
|
8
|
+
|
9
|
+
Metrics/LineLength:
|
10
|
+
Max: 120
|
data/README.md
CHANGED
@@ -50,12 +50,13 @@ end
|
|
50
50
|
Then you able to build or create clones:
|
51
51
|
|
52
52
|
```ruby
|
53
|
-
CompanyCopyStrategy.make(Company.first, Account.last.companies)
|
53
|
+
CompanyCopyStrategy.make(target: Company.first, destination: Account.last.companies)
|
54
54
|
```
|
55
55
|
or
|
56
56
|
```ruby
|
57
|
-
CompanyCopyStrategy.
|
57
|
+
CompanyCopyStrategy.make(target: Company.first)
|
58
58
|
```
|
59
|
+
Cloning destination can be has_many association or nil.
|
59
60
|
|
60
61
|
## Contributing
|
61
62
|
|
data/cloned.gemspec
CHANGED
data/lib/cloned.rb
CHANGED
data/lib/cloned/base.rb
CHANGED
@@ -5,7 +5,7 @@ module Cloned
|
|
5
5
|
attr_reader :copy, :target, :destination, :options
|
6
6
|
delegate :strategy, to: :class
|
7
7
|
|
8
|
-
def initialize(target
|
8
|
+
def initialize(target:, destination: nil, **options)
|
9
9
|
@target = target
|
10
10
|
@destination = destination
|
11
11
|
@options = options
|
@@ -53,9 +53,14 @@ module Cloned
|
|
53
53
|
private
|
54
54
|
|
55
55
|
def copy_association(target_association:, destination:, **options)
|
56
|
-
|
57
|
-
|
58
|
-
|
56
|
+
if target_association.respond_to?(:proxy_association)
|
57
|
+
copier = strategy.find_copier(target_association.proxy_association.klass)
|
58
|
+
target_association.each do |target_item|
|
59
|
+
copier.new(target: target_item, destination: destination, **options.merge(skip_transaction: true)).make
|
60
|
+
end
|
61
|
+
else
|
62
|
+
copier = strategy.find_copier(target_association.class)
|
63
|
+
copier.new(target: target_association, destination: destination, **options.merge(skip_transaction: true)).make
|
59
64
|
end
|
60
65
|
end
|
61
66
|
|
@@ -63,7 +68,7 @@ module Cloned
|
|
63
68
|
self.class.associations.each do |association_id, options|
|
64
69
|
copy_association(
|
65
70
|
target_association: target.public_send(association_id),
|
66
|
-
destination:
|
71
|
+
destination: DestinationProxy.new(clon, association_id),
|
67
72
|
**options)
|
68
73
|
end
|
69
74
|
end
|
@@ -0,0 +1,16 @@
|
|
1
|
+
module Cloned
|
2
|
+
class DestinationProxy
|
3
|
+
def initialize(owner, association)
|
4
|
+
@owner = owner
|
5
|
+
@association = association
|
6
|
+
end
|
7
|
+
|
8
|
+
def concat(clon)
|
9
|
+
if @owner.class.reflections[@association.to_s].is_a?(ActiveRecord::Reflection::HasManyReflection)
|
10
|
+
@owner.public_send(@association).concat(clon)
|
11
|
+
else
|
12
|
+
@owner.public_send("#{@association}=", clon)
|
13
|
+
end
|
14
|
+
end
|
15
|
+
end
|
16
|
+
end
|
data/lib/cloned/strategy.rb
CHANGED
@@ -15,12 +15,8 @@ class Cloned::Strategy
|
|
15
15
|
cloners_map[klass.name] || Cloned::Base
|
16
16
|
end
|
17
17
|
|
18
|
-
def make(target
|
19
|
-
find_copier(target.class).new(target, destination, options).make
|
20
|
-
end
|
21
|
-
|
22
|
-
def create(target, destination, options = {})
|
23
|
-
make(target, destination, options.merge(force: true))
|
18
|
+
def make(target:, destination: nil, **options)
|
19
|
+
find_copier(target.class).new(target: target, destination: destination, **options).make
|
24
20
|
end
|
25
21
|
end
|
26
22
|
end
|
data/lib/cloned/version.rb
CHANGED
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: cloned
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.1.
|
4
|
+
version: 0.1.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Askar Zinurov
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date: 2017-10-
|
11
|
+
date: 2017-10-30 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: activerecord
|
@@ -66,6 +66,20 @@ dependencies:
|
|
66
66
|
- - "~>"
|
67
67
|
- !ruby/object:Gem::Version
|
68
68
|
version: '3.0'
|
69
|
+
- !ruby/object:Gem::Dependency
|
70
|
+
name: sqlite3
|
71
|
+
requirement: !ruby/object:Gem::Requirement
|
72
|
+
requirements:
|
73
|
+
- - ">="
|
74
|
+
- !ruby/object:Gem::Version
|
75
|
+
version: '0'
|
76
|
+
type: :development
|
77
|
+
prerelease: false
|
78
|
+
version_requirements: !ruby/object:Gem::Requirement
|
79
|
+
requirements:
|
80
|
+
- - ">="
|
81
|
+
- !ruby/object:Gem::Version
|
82
|
+
version: '0'
|
69
83
|
description: Simple lib for cloning rails models and associations.
|
70
84
|
email:
|
71
85
|
- mail@asktim.ru
|
@@ -75,6 +89,7 @@ extra_rdoc_files: []
|
|
75
89
|
files:
|
76
90
|
- ".gitignore"
|
77
91
|
- ".rspec"
|
92
|
+
- ".rubocop.yml"
|
78
93
|
- ".travis.yml"
|
79
94
|
- Gemfile
|
80
95
|
- LICENSE.txt
|
@@ -85,6 +100,7 @@ files:
|
|
85
100
|
- cloned.gemspec
|
86
101
|
- lib/cloned.rb
|
87
102
|
- lib/cloned/base.rb
|
103
|
+
- lib/cloned/destination_proxy.rb
|
88
104
|
- lib/cloned/dsl.rb
|
89
105
|
- lib/cloned/strategy.rb
|
90
106
|
- lib/cloned/version.rb
|