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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 7753f2da570340b1d3a81fcb3ee5ca20ce29f35b
4
- data.tar.gz: 17a9f319c880d8634d81db7a09df98e9e499acf2
3
+ metadata.gz: abe02254b04e1312e29e831b62f27a1a1fb396bc
4
+ data.tar.gz: 4571a6152e7e7c873a6ebef6523d6f4c95519611
5
5
  SHA512:
6
- metadata.gz: b65842c27b89bd71c553d4437e54785404a23154f3c18cd72c9b1169c28ed43d8d233df54f2fa7dd25609d119eb303750c0cceda13e563f714f3e95c32266da4
7
- data.tar.gz: 1f9c2a962636e9effa00358ddcd012ca70c739f4e628b99506b7440922cb5be67280a04e18c7a3e4cad6e0687747b5634232139762b193afd29c0ec03e62ddb4
6
+ metadata.gz: 80f45b2ff43c7f8ef610e840457f8c3817098953555b30ddfd5992ed14b8825ae32bb05974d81d18c61e2605b0398a4225a03db96b8c532f2454dc03c9c01679
7
+ data.tar.gz: 0c468d1b95a9de4aa72f528759d14042f13e1a754a223738b4c45f3ef5f70507d08f452411b101ebee516bcb3c5989c6ba809c56948086d2995b10d2660c813c
data/.gitignore CHANGED
@@ -6,6 +6,7 @@
6
6
  /doc/
7
7
  /pkg/
8
8
  /spec/reports/
9
+ /spec/debug.log
9
10
  /tmp/
10
11
 
11
12
  # rspec failure tracking
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.create(Company.first, Account.last.companies)
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
@@ -34,4 +34,5 @@ Gem::Specification.new do |spec|
34
34
  spec.add_development_dependency "bundler", "~> 1.15"
35
35
  spec.add_development_dependency "rake", "~> 10.0"
36
36
  spec.add_development_dependency "rspec", "~> 3.0"
37
+ spec.add_development_dependency "sqlite3"
37
38
  end
data/lib/cloned.rb CHANGED
@@ -1,4 +1,5 @@
1
1
  require 'cloned/version'
2
+ require 'cloned/destination_proxy'
2
3
  require 'cloned/base'
3
4
  require 'cloned/strategy'
4
5
 
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, destination, options = {})
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
- copier = strategy.find_copier(target_association.proxy_association.klass)
57
- target_association.each do |target_item|
58
- copier.new(target_item, destination, options.merge(skip_transaction: true)).make
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: clon.public_send(association_id),
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
@@ -15,12 +15,8 @@ class Cloned::Strategy
15
15
  cloners_map[klass.name] || Cloned::Base
16
16
  end
17
17
 
18
- def make(target, destination, options = {})
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
@@ -1,3 +1,3 @@
1
1
  module Cloned
2
- VERSION = "0.1.0"
2
+ VERSION = "0.1.1"
3
3
  end
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.0
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-27 00:00:00.000000000 Z
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