quick_clone 1.0.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: 77350ad282a65d00823b3f10f55bad5d68e9d21a
4
+ data.tar.gz: 39d09969f837b9df69af9d1747bff6e2238d1464
5
+ SHA512:
6
+ metadata.gz: d132e3349cd63862657f1ab4c93d6b736d23d98ba402da36046004aee3e6952c16ed84a05c4eee9de180ebba54ba1316fe59baafeeebeb849a08a2c31185c445
7
+ data.tar.gz: cfed8cab952b45fd71636563af3895565ba23d61d944c2234cae83d7ca75efabd6f2b21d7a133c73afdd65c37f426d5ced359a3ab461c7920df1fb5055fe3fdf
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,3 @@
1
+ --format documentation
2
+ --color
3
+ -r spec_helper
data/.travis.yml ADDED
@@ -0,0 +1,5 @@
1
+ sudo: false
2
+ language: ruby
3
+ rvm:
4
+ - 2.4.2
5
+ before_install: gem install bundler
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in quick_clone.gemspec
4
+ gemspec
data/README.md ADDED
@@ -0,0 +1,56 @@
1
+ # QuickClone [![Build Status](https://travis-ci.org/d3sw/quick_clone.svg?branch=master)](https://travis-ci.org/d3sw/quick_clone) [![Coverage Status](https://coveralls.io/repos/github/d3sw/quick_clone/badge.svg?branch=master)](https://coveralls.io/github/d3sw/quick_clone?branch=master) [![Code Climate](https://codeclimate.com/github/d3sw/quick_clone/badges/gpa.svg)](https://codeclimate.com/github/d3sw/quick_clone)
2
+
3
+ Quickly clone an ActiveRecord object by providing a filter of attributes names.
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ ```ruby
10
+ gem 'quick_clone'
11
+ ```
12
+
13
+ And then execute:
14
+
15
+ $ bundle
16
+
17
+ Or install it yourself as:
18
+
19
+ $ gem install quick_clone
20
+
21
+ ## Usage
22
+
23
+ Provide a simple way of cloning ActiveRecord Records
24
+
25
+ It will return an in memory object, you will need to save
26
+ the record yourself.
27
+
28
+ You supply two things to `clone_from` method
29
+
30
+ `Clonable.clone_from(original_object, filter)`
31
+
32
+ The above is a convenience method for:
33
+
34
+ `Clonable.new(original_object, filter).clone`
35
+
36
+ * Original object is just the object where you will copy properties from
37
+
38
+ * Filter is an array of properties you want to copy from the original_object
39
+ you can list the properties as symbols or strings.
40
+
41
+ If one of the items on that list is a hash, the key has to be the name
42
+ of the association, the value has to be a list of values that will be
43
+ copied from all the items of the association.
44
+
45
+ It handles different association types, belongs_to, has_many, and has_one
46
+
47
+ ## Development
48
+
49
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
50
+
51
+ To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
52
+
53
+ ## Contributing
54
+
55
+ Bug reports and pull requests are welcome on GitHub at https://github.com/d3dw/quick_clone.
56
+
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 "active_record/quick_clone"
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
@@ -0,0 +1,63 @@
1
+ module QuickClone
2
+ class Cloner
3
+ ATTRIBUTE_CLASSES = %w(String Symbol).freeze
4
+
5
+ ABSOLUTE_EXCLUDE = [
6
+ :id,
7
+ :created_at,
8
+ :updated_at
9
+ ].freeze
10
+
11
+ SINGULAR_ASSOCIATIONS = [
12
+ :has_one,
13
+ :belongs_to
14
+ ].freeze
15
+
16
+ def self.clone_from(original_object, filter)
17
+ new(original_object, filter).clone
18
+ end
19
+
20
+ def initialize(original_object, filter)
21
+ @filter = filter
22
+
23
+ @original_object = original_object
24
+ @new_object = original_object.class.new
25
+ end
26
+
27
+ def clone
28
+ determine_clone(@original_object, @new_object, @filter)
29
+ @new_object
30
+ end
31
+
32
+ private
33
+
34
+ def determine_clone(original_object, new_object, filter)
35
+ filter.each do |field|
36
+ if ATTRIBUTE_CLASSES.include? field.class.name
37
+ next if ABSOLUTE_EXCLUDE.include?(field.to_sym)
38
+ copy_attribute(original_object, new_object, field)
39
+ elsif field.is_a? Hash
40
+ field.each do |association_name, association_filter|
41
+ copy_association(original_object, new_object, association_name, association_filter)
42
+ end
43
+ end
44
+ end
45
+ end
46
+
47
+ def copy_attribute(original_object, new_object, field)
48
+ new_object.send("#{field}=", original_object.send(field))
49
+ end
50
+
51
+ def copy_association(original_object, new_object, association_name, association_filter)
52
+ if SINGULAR_ASSOCIATIONS.include?(original_object.class.reflect_on_association(association_name).macro)
53
+ determine_clone(original_object.send(association_name),
54
+ new_object.send("build_#{association_name}"),
55
+ association_filter)
56
+ else
57
+ original_object.send(association_name).each do |association_original_object|
58
+ determine_clone(association_original_object, new_object.send(association_name).build, association_filter)
59
+ end
60
+ end
61
+ end
62
+ end
63
+ end
@@ -0,0 +1,3 @@
1
+ module QuickClone
2
+ VERSION = "1.0.0".freeze
3
+ end
@@ -0,0 +1,2 @@
1
+ require 'quick_clone/version'
2
+ require 'quick_clone/cloner'
@@ -0,0 +1,27 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'quick_clone/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "quick_clone"
8
+ spec.version = QuickClone::VERSION
9
+ spec.authors = ["Adan Alvarado"]
10
+ spec.email = ["adan.alvarado7@gmail.com"]
11
+ spec.summary = %q{Provide a simple way of cloning ActiveRecord Records}
12
+ spec.homepage = "https://github.com/aalvarado/quick_clone"
13
+
14
+ spec.files = `git ls-files -z`.split("\x0").reject do |f|
15
+ f.match(%r{^(test|spec|features)/})
16
+ end
17
+ spec.require_paths = ["lib"]
18
+
19
+ spec.add_dependency "activerecord", ">= 4.0", "< 6.0"
20
+
21
+ spec.add_development_dependency "coveralls"
22
+ spec.add_development_dependency "bundler", "~> 1.14"
23
+ spec.add_development_dependency "pry"
24
+ spec.add_development_dependency "rake", "~> 10.0"
25
+ spec.add_development_dependency "rspec", "~> 3.0"
26
+ spec.add_development_dependency "sqlite3", "~> 1.3"
27
+ end
metadata ADDED
@@ -0,0 +1,159 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: quick_clone
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.0
5
+ platform: ruby
6
+ authors:
7
+ - Adan Alvarado
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2017-10-09 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.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: '4.0'
30
+ - - "<"
31
+ - !ruby/object:Gem::Version
32
+ version: '6.0'
33
+ - !ruby/object:Gem::Dependency
34
+ name: coveralls
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: bundler
49
+ requirement: !ruby/object:Gem::Requirement
50
+ requirements:
51
+ - - "~>"
52
+ - !ruby/object:Gem::Version
53
+ version: '1.14'
54
+ type: :development
55
+ prerelease: false
56
+ version_requirements: !ruby/object:Gem::Requirement
57
+ requirements:
58
+ - - "~>"
59
+ - !ruby/object:Gem::Version
60
+ version: '1.14'
61
+ - !ruby/object:Gem::Dependency
62
+ name: pry
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: '10.0'
82
+ type: :development
83
+ prerelease: false
84
+ version_requirements: !ruby/object:Gem::Requirement
85
+ requirements:
86
+ - - "~>"
87
+ - !ruby/object:Gem::Version
88
+ version: '10.0'
89
+ - !ruby/object:Gem::Dependency
90
+ name: rspec
91
+ requirement: !ruby/object:Gem::Requirement
92
+ requirements:
93
+ - - "~>"
94
+ - !ruby/object:Gem::Version
95
+ version: '3.0'
96
+ type: :development
97
+ prerelease: false
98
+ version_requirements: !ruby/object:Gem::Requirement
99
+ requirements:
100
+ - - "~>"
101
+ - !ruby/object:Gem::Version
102
+ version: '3.0'
103
+ - !ruby/object:Gem::Dependency
104
+ name: sqlite3
105
+ requirement: !ruby/object:Gem::Requirement
106
+ requirements:
107
+ - - "~>"
108
+ - !ruby/object:Gem::Version
109
+ version: '1.3'
110
+ type: :development
111
+ prerelease: false
112
+ version_requirements: !ruby/object:Gem::Requirement
113
+ requirements:
114
+ - - "~>"
115
+ - !ruby/object:Gem::Version
116
+ version: '1.3'
117
+ description:
118
+ email:
119
+ - adan.alvarado7@gmail.com
120
+ executables: []
121
+ extensions: []
122
+ extra_rdoc_files: []
123
+ files:
124
+ - ".gitignore"
125
+ - ".rspec"
126
+ - ".travis.yml"
127
+ - Gemfile
128
+ - README.md
129
+ - Rakefile
130
+ - bin/console
131
+ - bin/setup
132
+ - lib/quick_clone.rb
133
+ - lib/quick_clone/cloner.rb
134
+ - lib/quick_clone/version.rb
135
+ - quick_clone.gemspec
136
+ homepage: https://github.com/aalvarado/quick_clone
137
+ licenses: []
138
+ metadata: {}
139
+ post_install_message:
140
+ rdoc_options: []
141
+ require_paths:
142
+ - lib
143
+ required_ruby_version: !ruby/object:Gem::Requirement
144
+ requirements:
145
+ - - ">="
146
+ - !ruby/object:Gem::Version
147
+ version: '0'
148
+ required_rubygems_version: !ruby/object:Gem::Requirement
149
+ requirements:
150
+ - - ">="
151
+ - !ruby/object:Gem::Version
152
+ version: '0'
153
+ requirements: []
154
+ rubyforge_project:
155
+ rubygems_version: 2.5.2
156
+ signing_key:
157
+ specification_version: 4
158
+ summary: Provide a simple way of cloning ActiveRecord Records
159
+ test_files: []