From 92c565a735a719851898386d7fbd933f1079a3cc Mon Sep 17 00:00:00 2001 From: Adan Alvarado Date: Fri, 6 Oct 2017 13:59:41 -0700 Subject: [PATCH] Change name --- Gemfile | 2 +- README.md | 8 +-- lib/active_record/quick_clone.rb | 7 -- lib/active_record/quick_clone/cloner.rb | 65 ------------------- lib/active_record/quick_clone/version.rb | 5 -- lib/quick_clone.rb | 2 + lib/quick_clone/cloner.rb | 63 ++++++++++++++++++ lib/quick_clone/version.rb | 3 + ...quick_clone.gemspec => quick_clone.gemspec | 9 +-- .../quick_clone/cloner_spec.rb | 2 +- spec/spec_helper.rb | 5 +- 11 files changed, 82 insertions(+), 89 deletions(-) delete mode 100644 lib/active_record/quick_clone.rb delete mode 100644 lib/active_record/quick_clone/cloner.rb delete mode 100644 lib/active_record/quick_clone/version.rb create mode 100644 lib/quick_clone.rb create mode 100644 lib/quick_clone/cloner.rb create mode 100644 lib/quick_clone/version.rb rename active_record-quick_clone.gemspec => quick_clone.gemspec (83%) rename spec/{active_record => }/quick_clone/cloner_spec.rb (98%) diff --git a/Gemfile b/Gemfile index 23d9fc2..cb70bdc 100644 --- a/Gemfile +++ b/Gemfile @@ -1,4 +1,4 @@ source 'https://rubygems.org' -# Specify your gem's dependencies in active_record-quick_clone.gemspec +# Specify your gem's dependencies in quick_clone.gemspec gemspec diff --git a/README.md b/README.md index 12f0524..67b6711 100644 --- a/README.md +++ b/README.md @@ -1,4 +1,4 @@ -# ActiveRecord::QuickClone [![Build Status](https://travis-ci.org/aalvarado/active_record-quick_clone.svg?branch=master)](https://travis-ci.org/aalvarado/active_record-quick_clone) +# 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) Quickly clone an ActiveRecord object by providing a filter of attributes names. @@ -7,7 +7,7 @@ Quickly clone an ActiveRecord object by providing a filter of attributes names. Add this line to your application's Gemfile: ```ruby -gem 'active_record-quick_clone' +gem 'quick_clone' ``` And then execute: @@ -16,7 +16,7 @@ And then execute: Or install it yourself as: - $ gem install active_record-quick_clone + $ gem install quick_clone ## Usage @@ -52,5 +52,5 @@ To install this gem onto your local machine, run `bundle exec rake install`. To ## Contributing -Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/active_record-quick_clone. +Bug reports and pull requests are welcome on GitHub at https://github.com/d3dw/quick_clone. diff --git a/lib/active_record/quick_clone.rb b/lib/active_record/quick_clone.rb deleted file mode 100644 index 2be51ed..0000000 --- a/lib/active_record/quick_clone.rb +++ /dev/null @@ -1,7 +0,0 @@ -require "active_record/quick_clone/version" -require "active_record/quick_clone/cloner" - -module ActiveRecord - module QuickClone - end -end diff --git a/lib/active_record/quick_clone/cloner.rb b/lib/active_record/quick_clone/cloner.rb deleted file mode 100644 index c595a2c..0000000 --- a/lib/active_record/quick_clone/cloner.rb +++ /dev/null @@ -1,65 +0,0 @@ -module ActiveRecord - module QuickClone - class Cloner - ATTRIBUTE_CLASSES = %w(String Symbol).freeze - - ABSOLUTE_EXCLUDE = [ - :id, - :created_at, - :updated_at - ].freeze - - SINGULAR_ASSOCIATIONS = [ - :has_one, - :belongs_to - ].freeze - - def self.clone_from(original_object, filter) - new(original_object, filter).clone - end - - def initialize(original_object, filter) - @filter = filter - - @original_object = original_object - @new_object = original_object.class.new - end - - def clone - determine_clone(@original_object, @new_object, @filter) - @new_object - end - - private - - def determine_clone(original_object, new_object, filter) - filter.each do |field| - if ATTRIBUTE_CLASSES.include? field.class.name - next if ABSOLUTE_EXCLUDE.include?(field.to_sym) - copy_attribute(original_object, new_object, field) - elsif field.is_a? Hash - field.each do |association_name, association_filter| - copy_association(original_object, new_object, association_name, association_filter) - end - end - end - end - - def copy_attribute(original_object, new_object, field) - new_object.send("#{field}=", original_object.send(field)) - end - - def copy_association(original_object, new_object, association_name, association_filter) - if SINGULAR_ASSOCIATIONS.include?(original_object.class.reflect_on_association(association_name).macro) - determine_clone(original_object.send(association_name), - new_object.send("build_#{association_name}"), - association_filter) - else - original_object.send(association_name).each do |association_original_object| - determine_clone(association_original_object, new_object.send(association_name).build, association_filter) - end - end - end - end - end -end diff --git a/lib/active_record/quick_clone/version.rb b/lib/active_record/quick_clone/version.rb deleted file mode 100644 index 4b7001c..0000000 --- a/lib/active_record/quick_clone/version.rb +++ /dev/null @@ -1,5 +0,0 @@ -module ActiveRecord - module QuickClone - VERSION = "0.1.0".freeze - end -end diff --git a/lib/quick_clone.rb b/lib/quick_clone.rb new file mode 100644 index 0000000..acc2000 --- /dev/null +++ b/lib/quick_clone.rb @@ -0,0 +1,2 @@ +require 'quick_clone/version' +require 'quick_clone/cloner' diff --git a/lib/quick_clone/cloner.rb b/lib/quick_clone/cloner.rb new file mode 100644 index 0000000..33df671 --- /dev/null +++ b/lib/quick_clone/cloner.rb @@ -0,0 +1,63 @@ +module QuickClone + class Cloner + ATTRIBUTE_CLASSES = %w(String Symbol).freeze + + ABSOLUTE_EXCLUDE = [ + :id, + :created_at, + :updated_at + ].freeze + + SINGULAR_ASSOCIATIONS = [ + :has_one, + :belongs_to + ].freeze + + def self.clone_from(original_object, filter) + new(original_object, filter).clone + end + + def initialize(original_object, filter) + @filter = filter + + @original_object = original_object + @new_object = original_object.class.new + end + + def clone + determine_clone(@original_object, @new_object, @filter) + @new_object + end + + private + + def determine_clone(original_object, new_object, filter) + filter.each do |field| + if ATTRIBUTE_CLASSES.include? field.class.name + next if ABSOLUTE_EXCLUDE.include?(field.to_sym) + copy_attribute(original_object, new_object, field) + elsif field.is_a? Hash + field.each do |association_name, association_filter| + copy_association(original_object, new_object, association_name, association_filter) + end + end + end + end + + def copy_attribute(original_object, new_object, field) + new_object.send("#{field}=", original_object.send(field)) + end + + def copy_association(original_object, new_object, association_name, association_filter) + if SINGULAR_ASSOCIATIONS.include?(original_object.class.reflect_on_association(association_name).macro) + determine_clone(original_object.send(association_name), + new_object.send("build_#{association_name}"), + association_filter) + else + original_object.send(association_name).each do |association_original_object| + determine_clone(association_original_object, new_object.send(association_name).build, association_filter) + end + end + end + end +end diff --git a/lib/quick_clone/version.rb b/lib/quick_clone/version.rb new file mode 100644 index 0000000..93689b8 --- /dev/null +++ b/lib/quick_clone/version.rb @@ -0,0 +1,3 @@ +module QuickClone + VERSION = "0.1.0".freeze +end diff --git a/active_record-quick_clone.gemspec b/quick_clone.gemspec similarity index 83% rename from active_record-quick_clone.gemspec rename to quick_clone.gemspec index 50e254c..3646716 100644 --- a/active_record-quick_clone.gemspec +++ b/quick_clone.gemspec @@ -1,15 +1,15 @@ # coding: utf-8 lib = File.expand_path('../lib', __FILE__) $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib) -require 'active_record/quick_clone/version' +require 'quick_clone/version' Gem::Specification.new do |spec| - spec.name = "active_record-quick_clone" - spec.version = ActiveRecord::QuickClone::VERSION + spec.name = "quick_clone" + spec.version = QuickClone::VERSION spec.authors = ["Adan Alvarado"] spec.email = ["adan.alvarado7@gmail.com"] spec.summary = %q{Provide a simple way of cloning ActiveRecord Records} - spec.homepage = "https://github.com/aalvarado/active_record-quick_clone" + spec.homepage = "https://github.com/aalvarado/quick_clone" # Prevent pushing this gem to RubyGems.org. To allow pushes either set the 'allowed_push_host' # to allow pushing to a single host or delete this section to allow pushing to any host. @@ -27,6 +27,7 @@ Gem::Specification.new do |spec| spec.add_dependency "activerecord", ">= 4.0", "< 6.0" + spec.add_development_dependency "coveralls" spec.add_development_dependency "bundler", "~> 1.14" spec.add_development_dependency "pry" spec.add_development_dependency "rake", "~> 10.0" diff --git a/spec/active_record/quick_clone/cloner_spec.rb b/spec/quick_clone/cloner_spec.rb similarity index 98% rename from spec/active_record/quick_clone/cloner_spec.rb rename to spec/quick_clone/cloner_spec.rb index 866761c..d7f8cc5 100644 --- a/spec/active_record/quick_clone/cloner_spec.rb +++ b/spec/quick_clone/cloner_spec.rb @@ -1,4 +1,4 @@ -describe ActiveRecord::QuickClone::Cloner do +describe QuickClone::Cloner do let(:user_name) { 'bitshifter' } let(:user_alias) { 'teamsleep' } let(:user) { User.create name: user_name, alias: user_alias } diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb index 573c151..daa822c 100644 --- a/spec/spec_helper.rb +++ b/spec/spec_helper.rb @@ -1,10 +1,11 @@ require 'bundler/setup' require 'active_record' -require 'active_record/quick_clone' +require 'quick_clone' require 'support/schema' require 'support/models' -require 'pry' +require 'coveralls' +Coveralls.wear! RSpec.configure do |config| # Enable flags like --only-failures and --next-failure