cloned 0.1.0

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 ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 7753f2da570340b1d3a81fcb3ee5ca20ce29f35b
4
+ data.tar.gz: 17a9f319c880d8634d81db7a09df98e9e499acf2
5
+ SHA512:
6
+ metadata.gz: b65842c27b89bd71c553d4437e54785404a23154f3c18cd72c9b1169c28ed43d8d233df54f2fa7dd25609d119eb303750c0cceda13e563f714f3e95c32266da4
7
+ data.tar.gz: 1f9c2a962636e9effa00358ddcd012ca70c739f4e628b99506b7440922cb5be67280a04e18c7a3e4cad6e0687747b5634232139762b193afd29c0ec03e62ddb4
data/.gitignore ADDED
@@ -0,0 +1,12 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
10
+
11
+ # rspec failure tracking
12
+ .rspec_status
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --format documentation
2
+ --color
data/.travis.yml ADDED
@@ -0,0 +1,5 @@
1
+ sudo: false
2
+ language: ruby
3
+ rvm:
4
+ - 2.3.5
5
+ before_install: gem install bundler -v 1.15.4
data/Gemfile ADDED
@@ -0,0 +1,6 @@
1
+ source "https://rubygems.org"
2
+
3
+ git_source(:github) {|repo_name| "https://github.com/#{repo_name}" }
4
+
5
+ # Specify your gem's dependencies in cloned.gemspec
6
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2017 Askar Zinurov
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,66 @@
1
+ # Cloned
2
+
3
+ Simple gem for copying trees of ActiveRecord models.
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ ```ruby
10
+ gem 'cloned'
11
+ ```
12
+
13
+ And then execute:
14
+
15
+ $ bundle
16
+
17
+ Or install it yourself as:
18
+
19
+ $ gem install cloned
20
+
21
+ ## Usage
22
+
23
+ First you need to define clonning stategy:
24
+
25
+ ```ruby
26
+ class CompanyCopyStrategy < Cloned::Strategy
27
+ declare :company do
28
+ before do |copied_company|
29
+ copied_company.copied_at = Time.zone.now
30
+ end
31
+
32
+ after do |copied_company|
33
+ copied_company.departments_count = copied_company.departments.size
34
+ end
35
+
36
+ nullify :created_at, :updated_at
37
+
38
+ association(:departments)
39
+ end
40
+
41
+ declare :department do
42
+ association :employees, if: ->(employee) { employee.salary > 2000 }
43
+ end
44
+
45
+ declare :employee do
46
+ nullify :last_sickleave, :vacation_at
47
+ end
48
+ end
49
+ ```
50
+ Then you able to build or create clones:
51
+
52
+ ```ruby
53
+ CompanyCopyStrategy.make(Company.first, Account.last.companies)
54
+ ```
55
+ or
56
+ ```ruby
57
+ CompanyCopyStrategy.create(Company.first, Account.last.companies)
58
+ ```
59
+
60
+ ## Contributing
61
+
62
+ Bug reports and pull requests are welcome on GitHub at https://github.com/AskarZinurov/cloned.
63
+
64
+ ## License
65
+
66
+ The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
data/Rakefile ADDED
@@ -0,0 +1,6 @@
1
+ require "bundler/gem_tasks"
2
+ require "rspec/core/rake_task"
3
+
4
+ RSpec::Core::RakeTask.new(:spec)
5
+
6
+ task :default => :spec
data/bin/console ADDED
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "cloned"
5
+
6
+ # You can add fixtures and/or initialization code here to make experimenting
7
+ # with your gem easier. You can also use a different console, if you like.
8
+
9
+ # (If you use this, don't forget to add pry to your Gemfile!)
10
+ # require "pry"
11
+ # Pry.start
12
+
13
+ require "irb"
14
+ IRB.start(__FILE__)
data/bin/setup ADDED
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+
8
+ # Do any other automated setup that you need to do here
data/cloned.gemspec ADDED
@@ -0,0 +1,37 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path("../lib", __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require "cloned/version"
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "cloned"
8
+ spec.version = Cloned::VERSION
9
+ spec.authors = ["Askar Zinurov"]
10
+ spec.email = ["mail@asktim.ru"]
11
+
12
+ spec.summary = %q{Rails models copier.}
13
+ spec.description = %q{Simple lib for cloning rails models and associations.}
14
+ spec.homepage = ""
15
+ spec.license = "MIT"
16
+
17
+ # Prevent pushing this gem to RubyGems.org. To allow pushes either set the 'allowed_push_host'
18
+ # to allow pushing to a single host or delete this section to allow pushing to any host.
19
+ if spec.respond_to?(:metadata)
20
+ spec.metadata["allowed_push_host"] = 'https://rubygems.org'
21
+ else
22
+ raise "RubyGems 2.0 or newer is required to protect against " \
23
+ "public gem pushes."
24
+ end
25
+
26
+ spec.files = `git ls-files -z`.split("\x0").reject do |f|
27
+ f.match(%r{^(test|spec|features)/})
28
+ end
29
+ spec.bindir = "exe"
30
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
31
+ spec.require_paths = ["lib"]
32
+
33
+ spec.add_dependency "activerecord", "~> 4.2"
34
+ spec.add_development_dependency "bundler", "~> 1.15"
35
+ spec.add_development_dependency "rake", "~> 10.0"
36
+ spec.add_development_dependency "rspec", "~> 3.0"
37
+ end
@@ -0,0 +1,110 @@
1
+ require 'cloned/dsl'
2
+
3
+ module Cloned
4
+ class Base
5
+ attr_reader :copy, :target, :destination, :options
6
+ delegate :strategy, to: :class
7
+
8
+ def initialize(target, destination, options = {})
9
+ @target = target
10
+ @destination = destination
11
+ @options = options
12
+ end
13
+
14
+ def make
15
+ if skip_transaction?
16
+ make_or_fail!
17
+ else
18
+ ActiveRecord::Base.transaction { make_or_fail! }
19
+ end
20
+ copy
21
+ end
22
+
23
+ def valid?
24
+ target.presence
25
+ end
26
+
27
+ protected
28
+
29
+ def skip_transaction?
30
+ options[:skip_transaction]
31
+ end
32
+
33
+ def force?
34
+ options[:force].presence
35
+ end
36
+
37
+ def optional_before(clon)
38
+ options[:before].call(clon) if options.key?(:before)
39
+ end
40
+
41
+ def optional_after(clon)
42
+ options[:after].call(clon) if options.key?(:after)
43
+ end
44
+
45
+ class << self
46
+ attr_accessor :strategy
47
+
48
+ include Cloned::DSL
49
+
50
+ delegate :find_copier, to: :strategy
51
+ end
52
+
53
+ private
54
+
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
59
+ end
60
+ end
61
+
62
+ def copy_associations(clon)
63
+ self.class.associations.each do |association_id, options|
64
+ copy_association(
65
+ target_association: target.public_send(association_id),
66
+ destination: clon.public_send(association_id),
67
+ **options)
68
+ end
69
+ end
70
+
71
+ def clearing_attributes
72
+ []
73
+ end
74
+
75
+ def before(clon)
76
+ optional_before(clon)
77
+ declared_before(clon) if respond_to?(:declared_before)
78
+ end
79
+
80
+ def after(clon)
81
+ optional_after(clon)
82
+ declared_after(clon) if respond_to?(:declared_after)
83
+ end
84
+
85
+ def prepare(clon)
86
+ clon.assign_attributes(Hash[clearing_attributes.map { |k| [k, nil] }])
87
+ clon
88
+ end
89
+
90
+ def validate!
91
+ raise 'Cloning context not valid!' unless valid?
92
+ end
93
+
94
+ def make_or_fail!
95
+ validate!
96
+ return if options.key?(:if) && !options[:if].call(target)
97
+ @copy = make_copy(target: target, destination: destination)
98
+ @copy.save! if force?
99
+ end
100
+
101
+ def make_copy(target:, destination:)
102
+ clon = prepare(target.dup)
103
+ before(clon)
104
+ destination.concat(clon) unless destination.nil?
105
+ copy_associations(clon)
106
+ after(clon)
107
+ clon
108
+ end
109
+ end
110
+ end
data/lib/cloned/dsl.rb ADDED
@@ -0,0 +1,27 @@
1
+ module Cloned
2
+ module DSL
3
+ attr_reader :associations
4
+
5
+ def before(&block)
6
+ define_method :declared_before, &block
7
+ end
8
+
9
+ def after(&block)
10
+ define_method :declared_after, &block
11
+ end
12
+
13
+ def nullify(*attributes)
14
+ define_method :clearing_attributes do
15
+ attributes
16
+ end
17
+ end
18
+
19
+ def association(association_id, options = {})
20
+ associations[association_id] = options
21
+ end
22
+
23
+ def associations
24
+ @associations ||= {}
25
+ end
26
+ end
27
+ end
@@ -0,0 +1,26 @@
1
+ class Cloned::Strategy
2
+ class << self
3
+ def cloners_map
4
+ @cloners_map ||= {}
5
+ end
6
+
7
+ def declare(cloner_id, options = {}, &block)
8
+ cloner_name = options[:class_name] || cloner_id.to_s.camelcase
9
+ cloner = Class.new(Cloned::Base, &block)
10
+ cloner.strategy = self
11
+ cloners_map[cloner_name] = cloner
12
+ end
13
+
14
+ def find_copier(klass)
15
+ cloners_map[klass.name] || Cloned::Base
16
+ end
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))
24
+ end
25
+ end
26
+ end
@@ -0,0 +1,3 @@
1
+ module Cloned
2
+ VERSION = "0.1.0"
3
+ end
data/lib/cloned.rb ADDED
@@ -0,0 +1,5 @@
1
+ require 'cloned/version'
2
+ require 'cloned/base'
3
+ require 'cloned/strategy'
4
+
5
+ module Cloned; end
metadata ADDED
@@ -0,0 +1,116 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: cloned
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Askar Zinurov
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2017-10-27 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: '4.2'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '4.2'
27
+ - !ruby/object:Gem::Dependency
28
+ name: bundler
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '1.15'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '1.15'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rake
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '10.0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '10.0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: rspec
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '3.0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '3.0'
69
+ description: Simple lib for cloning rails models and associations.
70
+ email:
71
+ - mail@asktim.ru
72
+ executables: []
73
+ extensions: []
74
+ extra_rdoc_files: []
75
+ files:
76
+ - ".gitignore"
77
+ - ".rspec"
78
+ - ".travis.yml"
79
+ - Gemfile
80
+ - LICENSE.txt
81
+ - README.md
82
+ - Rakefile
83
+ - bin/console
84
+ - bin/setup
85
+ - cloned.gemspec
86
+ - lib/cloned.rb
87
+ - lib/cloned/base.rb
88
+ - lib/cloned/dsl.rb
89
+ - lib/cloned/strategy.rb
90
+ - lib/cloned/version.rb
91
+ homepage: ''
92
+ licenses:
93
+ - MIT
94
+ metadata:
95
+ allowed_push_host: https://rubygems.org
96
+ post_install_message:
97
+ rdoc_options: []
98
+ require_paths:
99
+ - lib
100
+ required_ruby_version: !ruby/object:Gem::Requirement
101
+ requirements:
102
+ - - ">="
103
+ - !ruby/object:Gem::Version
104
+ version: '0'
105
+ required_rubygems_version: !ruby/object:Gem::Requirement
106
+ requirements:
107
+ - - ">="
108
+ - !ruby/object:Gem::Version
109
+ version: '0'
110
+ requirements: []
111
+ rubyforge_project:
112
+ rubygems_version: 2.5.2.1
113
+ signing_key:
114
+ specification_version: 4
115
+ summary: Rails models copier.
116
+ test_files: []