replication 0.3.2 → 0.4.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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 10c99c8155646cbb3c8e0d4de62067b4dab33800
4
- data.tar.gz: f28a8bc30bc608030b56dcc334a6618f8a0d6b73
3
+ metadata.gz: 279a019bb4a36a642ec8ff94bf06df30534116b8
4
+ data.tar.gz: 185300af2787edbd13d48d7f6b34cc9ada06a47c
5
5
  SHA512:
6
- metadata.gz: a5a573a0db20f953effa6111662401c3d31cd2eea897c9a9c3e9ba1c5a966b7c2da3d61ffa57fbac7b0dd0f10e76bc184f44f78386a295df6fcc1be765a55a18
7
- data.tar.gz: bf6386597539e4362e1ed12f9267f59f07e0b948064e09399aed5b3d71c14ef679d54e1002bfc26348729fbdf42bd94fb0fc1a59cd0545ec38a483e99c88d585
6
+ metadata.gz: 53c8cd4c11f0ce238c6d42bed9a1c89315001e9adcfa9d5ecda71cda5b12a277ba4eb3b558719073ed70cc841b45f1808769df2b48785c87a88686aaa7073959
7
+ data.tar.gz: dbfbc2ae4cfdf7de60b97cbf1cbe84e5fcecbb4d1280433636a95f966de4f327b59a7cac6ca943b399ebe2119107a4e0c133caaedc79c2423365551542a73fff
data/Gemfile CHANGED
@@ -3,7 +3,7 @@ source 'https://rubygems.org'
3
3
  gemspec
4
4
 
5
5
  gem 'orm_adapter', '~> 0.5'
6
- gem 'rails', '~> 4.0.5'
6
+ gem 'rails', '~> 4.2.1'
7
7
 
8
8
  group :test do
9
9
  gem 'minitest-reporters'
data/README.md CHANGED
@@ -1,5 +1,7 @@
1
1
  # Replication
2
2
 
3
+ [![Code Climate](https://codeclimate.com/github/rodrigoddalmeida/replication.png)](https://codeclimate.com/github/rodrigoddalmeida/replication)
4
+
3
5
  Data replication as templates for Ruby ORMs.
4
6
 
5
7
  > ![DNA]
@@ -5,7 +5,7 @@ class ReplicationMigration < ActiveRecord::Migration
5
5
  t.text :pairs
6
6
  t.references :origin, polymorphic: true, index: true
7
7
 
8
- t.timestamps
8
+ t.timestamps null: true
9
9
  end
10
10
  end
11
11
 
@@ -0,0 +1,7 @@
1
+ module Replication
2
+ module ActiveRecord
3
+ class PolymorphicStrand < Strand
4
+ belongs_to :origin, polymorphic: true
5
+ end
6
+ end
7
+ end
@@ -5,7 +5,7 @@ module Replication
5
5
  class Strand < ::ActiveRecord::Base
6
6
  extend Replication::StrandMethods
7
7
  serialize :pairs
8
- belongs_to :origin, polymorphic: true
8
+ belongs_to :origin
9
9
 
10
10
  validates :name, uniqueness: true, presence: true
11
11
  end
@@ -24,7 +24,7 @@ module Replication
24
24
  @strand_attributes.merge!({
25
25
  "#{a}_attributes".to_sym => self.send(association_reflection.name).select(attributes_from_association).to_a.map(&:serializable_hash)
26
26
  })
27
- end
27
+ end
28
28
  end
29
29
 
30
30
  @strand_attributes
@@ -0,0 +1,12 @@
1
+ module Replication
2
+ module Modules
3
+ module Polymorphic
4
+ def self.included(model_class)
5
+ model_class.class_eval do
6
+ has_many :strands, class_name: replication_config.strand_class,
7
+ as: :origin
8
+ end
9
+ end
10
+ end
11
+ end
12
+ end
@@ -9,7 +9,8 @@ module Replication
9
9
  strand_class.new({
10
10
  name: options[:name],
11
11
  pairs: strand_attributes,
12
- origin: self
12
+ origin_id: self.id,
13
+ origin_type: self.class.to_s
13
14
  })
14
15
  end
15
16
 
@@ -17,7 +18,8 @@ module Replication
17
18
  strand_class.to_adapter.create!({
18
19
  name: options[:name],
19
20
  pairs: strand_attributes,
20
- origin: self
21
+ origin_id: self.id,
22
+ origin_type: self.class.to_s
21
23
  })
22
24
  end
23
25
 
@@ -1,26 +1,20 @@
1
1
  module Replication
2
2
  module Process
3
3
 
4
- def self.extended(model_class)
5
- model_class.class_eval do
6
- include Model
7
- end
8
- end
4
+ def can_replicate(pairs_method = :attributes, **options)
5
+ reset_config if self.respond_to?(:unwound)
9
6
 
10
- # Include or extend it. We work with both.
11
- def self.included(model_class)
12
- model_class.extend self
13
- end
7
+ puts replication_config.inspect
14
8
 
15
- def can_replicate(pairs_method = :attributes, **options)
16
- @@replication_config = Class.new(Config).new(self)
17
9
  default_options = Replication.defaults
18
10
  modules = [:semi_conservative] # required module
19
11
  modules.concat([].push(options.delete(:with)).flatten).compact!
20
12
 
21
- @@replication_config.pairs_method = pairs_method
22
- @@replication_config.set default_options.merge(options)
23
- @@replication_config.with modules
13
+ replication_config.pairs_method = pairs_method
14
+ replication_config.set default_options.merge(options)
15
+ replication_config.with modules
16
+
17
+ include Model
24
18
  end
25
19
 
26
20
  def new_from_strand(id=nil, **options)
@@ -33,25 +27,34 @@ module Replication
33
27
  new(strand.pairs) if strand
34
28
  end
35
29
 
36
- def replication_config
37
- @@replication_config
30
+ def reset_config
31
+ @replication_config = nil
38
32
  end
39
33
 
40
- module Model
41
-
42
- def strand_class
43
- replication_config.strand_class
34
+ def replication_config
35
+ @replication_config ||= base_class.replication_config.dup.tap do |config|
36
+ config.model_class = self
44
37
  end
38
+ end
39
+ end
45
40
 
46
- def replication_config
47
- self.class.replication_config
48
- end
41
+ module Model
42
+ def self.included(model_class)
43
+ return if model_class.respond_to?(:can_replicate)
44
+ end
49
45
 
50
- private
46
+ def strand_class
47
+ replication_config.strand_class
48
+ end
51
49
 
52
- def _strand_attributes
53
- send(replication_config.pairs_method).deep_symbolize_keys
54
- end
50
+ def replication_config
51
+ self.class.replication_config
55
52
  end
53
+
54
+ private
55
+
56
+ def _strand_attributes
57
+ send(replication_config.pairs_method).deep_symbolize_keys
58
+ end
56
59
  end
57
60
  end
@@ -1,3 +1,3 @@
1
1
  module Replication
2
- VERSION = "0.3.2"
2
+ VERSION = "0.4.0"
3
3
  end
data/lib/replication.rb CHANGED
@@ -8,6 +8,7 @@ module Replication
8
8
 
9
9
  module ActiveRecord
10
10
  autoload :Strand, 'replication/active_record/strand'
11
+ autoload :PolymorphicStrand, 'replication/active_record/polymorphic_strand'
11
12
  end
12
13
 
13
14
  module Modules
@@ -15,17 +16,34 @@ module Replication
15
16
  autoload :Proofreading, 'replication/modules/proofreading'
16
17
 
17
18
  autoload :Association, 'replication/modules/association'
19
+ autoload :Polymorphic, 'replication/modules/polymorphic'
18
20
  end
19
21
 
20
22
  def self.defaults
21
23
  defaults = {
22
- only: [],
24
+ only: [],
23
25
  except: []
24
26
  }
25
27
  defaults.merge!({
26
- strand_class: ::Replication::ActiveRecord::Strand,
28
+ strand_class: ::Replication::ActiveRecord::PolymorphicStrand,
27
29
  except: [:id, :created_at, :updated_at]
28
30
  }) if defined?(ActiveRecord)
31
+
32
+ defaults
33
+ end
34
+
35
+ def self.extended(model_class)
36
+ return if model_class.respond_to?(:can_replicate)
37
+ model_class.class_eval do
38
+ extend Process
39
+ @replication_config = Class.new(Config).new(self)
40
+ include Model
41
+ end
42
+ end
43
+
44
+ # Include or extend it. We work with both.
45
+ def self.included(model_class)
46
+ model_class.extend self
29
47
  end
30
48
 
31
49
  class UnwoundError < StandardError; end;
@@ -3,8 +3,3 @@ ActiveRecord::Base.logger = Logger.new(nil)
3
3
  ActiveRecord::Base.include_root_in_json = true
4
4
 
5
5
  ActiveRecord::Migrator.migrate([File.expand_path("../../../db/migrate/", __FILE__), File.expand_path("../../rails_app/db/migrate/", __FILE__)])
6
-
7
- class ActiveSupport::TestCase
8
- self.use_transactional_fixtures = true
9
- self.use_instantiated_fixtures = false
10
- end
@@ -0,0 +1,2 @@
1
+ class Animal < Organism
2
+ end
@@ -1,4 +1,7 @@
1
1
  RailsApp::Application.configure do
2
+
3
+ config.active_support.test_order = :random
4
+
2
5
  # Settings specified here will take precedence over those in config/application.rb.
3
6
 
4
7
  # The test environment is used exclusively to run your application's
@@ -13,7 +16,7 @@ RailsApp::Application.configure do
13
16
  config.eager_load = false
14
17
 
15
18
  # Configure static asset server for tests with Cache-Control for performance.
16
- config.serve_static_assets = true
19
+ config.serve_static_files = true
17
20
  config.static_cache_control = 'public, max-age=3600'
18
21
 
19
22
  # Show full error reports and disable caching.
@@ -5,7 +5,7 @@ class OrganismMigration < ActiveRecord::Migration
5
5
  t.integer :number_of_legs
6
6
  t.datetime :birth_date
7
7
 
8
- t.timestamps
8
+ t.timestamps null: true
9
9
  end
10
10
  end
11
11
 
@@ -2,7 +2,6 @@ require "test_helper"
2
2
 
3
3
  class Replication::Modules::SemiConservativeTest < ActiveSupport::TestCase
4
4
 
5
-
6
5
  test "unwound with default options" do
7
6
  Organism.can_replicate
8
7
  organism = organism_object
@@ -3,7 +3,7 @@ require "test_helper"
3
3
  class Replication::ProcessTest < ActiveSupport::TestCase
4
4
 
5
5
  def setup
6
- Organism.extend Replication::Process
6
+ Organism.extend Replication
7
7
  end
8
8
 
9
9
  test "extending provide can_replicate method" do
@@ -48,4 +48,12 @@ class Replication::ProcessTest < ActiveSupport::TestCase
48
48
 
49
49
  assert_instance_of Organism, Organism.new_from_strand(name: 'Original Bacteria')
50
50
  end
51
+
52
+ test "STI support" do
53
+ Animal.can_replicate
54
+ original = Animal.create(name: 'Bat', number_of_legs: 2, birth_date: Time.now)
55
+ strand = original.replicate(name: 'Original Bat')
56
+
57
+ assert_equal "Animal", strand.origin_type
58
+ end
51
59
  end
data/test/test_helper.rb CHANGED
@@ -4,14 +4,14 @@ REPLICATION_ORM = (ENV["REPLICATION_ORM"] || :active_record).to_sym
4
4
  $:.unshift File.dirname(__FILE__)
5
5
 
6
6
  require "rails_app/config/environment"
7
- require "rails/test_help"
8
7
  require "orm/#{REPLICATION_ORM}"
8
+ require "rails/test_help"
9
9
 
10
10
  require "minitest/reporters"
11
11
  Minitest::Reporters.use! [Minitest::Reporters::DefaultReporter.new(color: false)]
12
12
 
13
13
  # Basic Setup
14
- Organism.extend Replication::Process
14
+ Organism.extend Replication
15
15
 
16
16
  def organism_object
17
17
  Organism.new(name: 'Bacteria', number_of_legs: 1, birth_date: Time.now)
metadata CHANGED
@@ -1,41 +1,41 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: replication
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.3.2
4
+ version: 0.4.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Rodrigo DeAlmeida
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2014-07-09 00:00:00.000000000 Z
11
+ date: 2015-03-31 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
15
15
  requirement: !ruby/object:Gem::Requirement
16
16
  requirements:
17
- - - ~>
17
+ - - "~>"
18
18
  - !ruby/object:Gem::Version
19
19
  version: '1.6'
20
20
  type: :development
21
21
  prerelease: false
22
22
  version_requirements: !ruby/object:Gem::Requirement
23
23
  requirements:
24
- - - ~>
24
+ - - "~>"
25
25
  - !ruby/object:Gem::Version
26
26
  version: '1.6'
27
27
  - !ruby/object:Gem::Dependency
28
28
  name: rake
29
29
  requirement: !ruby/object:Gem::Requirement
30
30
  requirements:
31
- - - '>='
31
+ - - ">="
32
32
  - !ruby/object:Gem::Version
33
33
  version: '0'
34
34
  type: :development
35
35
  prerelease: false
36
36
  version_requirements: !ruby/object:Gem::Requirement
37
37
  requirements:
38
- - - '>='
38
+ - - ">="
39
39
  - !ruby/object:Gem::Version
40
40
  version: '0'
41
41
  description: Data replication as templates for Ruby ORMs
@@ -45,17 +45,19 @@ executables: []
45
45
  extensions: []
46
46
  extra_rdoc_files: []
47
47
  files:
48
- - .gitignore
48
+ - ".gitignore"
49
49
  - Gemfile
50
50
  - LICENSE.txt
51
51
  - README.md
52
52
  - Rakefile
53
53
  - db/migrate/714417722_replication_migration.rb
54
54
  - lib/replication.rb
55
+ - lib/replication/active_record/polymorphic_strand.rb
55
56
  - lib/replication/active_record/strand.rb
56
57
  - lib/replication/config.rb
57
58
  - lib/replication/engine.rb
58
59
  - lib/replication/modules/association.rb
60
+ - lib/replication/modules/polymorphic.rb
59
61
  - lib/replication/modules/proofreading.rb
60
62
  - lib/replication/modules/semi_conservative.rb
61
63
  - lib/replication/process.rb
@@ -65,6 +67,7 @@ files:
65
67
  - test/orm/active_record.rb
66
68
  - test/rails_app/Rakefile
67
69
  - test/rails_app/app/models/.keep
70
+ - test/rails_app/app/models/animal.rb
68
71
  - test/rails_app/app/models/concerns/.keep
69
72
  - test/rails_app/app/models/organism.rb
70
73
  - test/rails_app/app/models/tool.rb
@@ -102,17 +105,17 @@ require_paths:
102
105
  - lib
103
106
  required_ruby_version: !ruby/object:Gem::Requirement
104
107
  requirements:
105
- - - '>='
108
+ - - ">="
106
109
  - !ruby/object:Gem::Version
107
110
  version: '0'
108
111
  required_rubygems_version: !ruby/object:Gem::Requirement
109
112
  requirements:
110
- - - '>='
113
+ - - ">="
111
114
  - !ruby/object:Gem::Version
112
115
  version: '0'
113
116
  requirements: []
114
117
  rubyforge_project:
115
- rubygems_version: 2.2.2
118
+ rubygems_version: 2.4.5
116
119
  signing_key:
117
120
  specification_version: 4
118
121
  summary: Data replication as templates for Ruby ORMs
@@ -120,6 +123,7 @@ test_files:
120
123
  - test/orm/active_record.rb
121
124
  - test/rails_app/Rakefile
122
125
  - test/rails_app/app/models/.keep
126
+ - test/rails_app/app/models/animal.rb
123
127
  - test/rails_app/app/models/concerns/.keep
124
128
  - test/rails_app/app/models/organism.rb
125
129
  - test/rails_app/app/models/tool.rb