replication 0.2.0 → 0.3.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: af7f89d66290f3a6e85b44aa53a710af654de579
4
- data.tar.gz: 1e4068b2e2debd7567d283e8c0a56c3606004239
3
+ metadata.gz: fd5a01b4b8a01177c8ebac63ee886a3addcbd23a
4
+ data.tar.gz: 63febe92d691600d7d74581e60cd094170f33cde
5
5
  SHA512:
6
- metadata.gz: 17a772115a1e9a9f53f4c8570fb1f6e78c72a940f159199333eb6d7f157f792c71d825066ca788de3b959b3174f4dad0005bf353ff314d9ecb6cab890eaad252
7
- data.tar.gz: 46ec1398c27134b0db676e1d45a88449b39e0252659a191f88dd3286e6516333d6da949ac158a37f89bb853caec9a6b40aa04d429483dc183631a6887f2a7db9
6
+ metadata.gz: 943df247a131e72c686d2aeea3d5fcf19a0e884b2611b29538a0f81d2c2e7492dd79450cb0518d91bee22b1f447878d5954508bb3c0a84aab322fed945cc8daf
7
+ data.tar.gz: 6e17a74b7940d84c570cb6d4c003bddbf0ae73f8467f633f61fa723399f0ccb76082fec643b9c5c275177524ff9a37cb5f93773a42f4d1eacb94c38eb8b95995
data/README.md CHANGED
@@ -101,6 +101,12 @@ needs the 'id' and 'type' references for the association.
101
101
 
102
102
  In some cases you don't want/won't need the strands to be associated, so, be free.
103
103
 
104
+ There's also a Association module, that is experimental and should be used with care. It uses the same parameters as the ```accepts_nested_attributes_for```.
105
+
106
+ ```ruby
107
+ Model.can_replicate with: { associations: [:children, reject_if: :all_blank] }
108
+ ```
109
+
104
110
  ### Rails
105
111
 
106
112
  Migrations are be provided by:
@@ -2,20 +2,29 @@ module Replication
2
2
 
3
3
  class Config
4
4
 
5
- attr_accessor :model_class, :pairs_method, :strand_class, :except, :only
5
+ attr_accessor :model_class, :pairs_method, :strand_class, :except, :only, :options
6
6
 
7
7
  def initialize(model_class)
8
8
  @model_class = model_class
9
+ @options = {}
9
10
  end
10
11
 
11
12
  def with(modules)
12
13
  modules.each do |m|
13
- model_class.send :include, Replication::Modules.const_get(m.to_s.classify)
14
+ case m
15
+ when Symbol
16
+ model_class.send :include, Replication::Modules.const_get(m.to_s.classify)
17
+ when Hash
18
+ @options.merge!(m)
19
+ with(m.keys)
20
+ # else
21
+ # type not known, ignore
22
+ end
14
23
  end
15
24
  end
16
25
 
17
- def set(options)
18
- options and options.each {|name, value| self.send "#{name}=", value}
26
+ def set(params)
27
+ params and params.each {|name, value| self.send "#{name}=", value}
19
28
  end
20
29
  end
21
30
  end
@@ -0,0 +1,34 @@
1
+ module Replication
2
+ module Modules
3
+ module Association
4
+
5
+ def self.included(model_class)
6
+ model_class.accepts_nested_attributes_for *model_class.replication_config.options[:associations]
7
+ end
8
+
9
+ def strand_attributes
10
+ super
11
+ associations_to_replicate = self.class.replication_config.options[:associations]
12
+
13
+ if associations_to_replicate
14
+ if associations_to_replicate.last.class == Hash
15
+ associations_to_replicate.pop
16
+ end
17
+
18
+ associations_to_replicate.each do |a|
19
+ association_reflection = self.class.reflect_on_association(a)
20
+ association_model = association_reflection.klass
21
+ attributes_from_association = association_model.column_names.dup
22
+ attributes_from_association.delete('id')
23
+ attributes_from_association.delete(association_reflection.foreign_key)
24
+ @strand_attributes.merge!({
25
+ "#{a}_attributes".to_sym => association_model.select(*attributes_from_association).to_a.map(&:serializable_hash)
26
+ })
27
+ end
28
+ end
29
+
30
+ @strand_attributes
31
+ end
32
+ end
33
+ end
34
+ end
@@ -3,7 +3,6 @@ module Replication
3
3
 
4
4
  def self.extended(model_class)
5
5
  model_class.class_eval do
6
- @@replication_config = Class.new(Config).new(self)
7
6
  include Model
8
7
  end
9
8
  end
@@ -14,9 +13,10 @@ module Replication
14
13
  end
15
14
 
16
15
  def can_replicate(pairs_method = :attributes, **options)
16
+ @@replication_config = Class.new(Config).new(self)
17
17
  default_options = Replication.defaults
18
18
  modules = [:semi_conservative] # required module
19
- modules.concat(Array(options.delete(:with)))
19
+ modules.concat([].push(options.delete(:with)).flatten).compact!
20
20
 
21
21
  @@replication_config.pairs_method = pairs_method
22
22
  @@replication_config.set default_options.merge(options)
@@ -1,3 +1,3 @@
1
1
  module Replication
2
- VERSION = "0.2.0"
2
+ VERSION = "0.3.0"
3
3
  end
data/lib/replication.rb CHANGED
@@ -13,6 +13,8 @@ module Replication
13
13
  module Modules
14
14
  autoload :SemiConservative, 'replication/modules/semi_conservative'
15
15
  autoload :Proofreading, 'replication/modules/proofreading'
16
+
17
+ autoload :Association, 'replication/modules/association'
16
18
  end
17
19
 
18
20
  def self.defaults
@@ -1,3 +1,5 @@
1
1
  class Organism < ::ActiveRecord::Base
2
+ has_many :toolings
3
+ has_many :tools, through: :toolings
2
4
  validates :name, presence: true
3
5
  end
@@ -0,0 +1,4 @@
1
+ class Tool < ActiveRecord::Base
2
+ has_many :toolings
3
+ has_many :organisms, through: :toolings
4
+ end
@@ -0,0 +1,4 @@
1
+ class Tooling < ActiveRecord::Base
2
+ belongs_to :organism
3
+ belongs_to :tool
4
+ end
@@ -0,0 +1,17 @@
1
+ class ToolMigration < ActiveRecord::Migration
2
+ def self.up
3
+ create_table :toolings do |t|
4
+ t.references :organism
5
+ t.references :tool
6
+ end
7
+
8
+ create_table :tools do |t|
9
+ t.string :name
10
+ end
11
+ end
12
+
13
+ def self.down
14
+ drop_table :toolings
15
+ drop_table :tools
16
+ end
17
+ end
@@ -0,0 +1,16 @@
1
+ require "test_helper"
2
+
3
+ class Replication::Modules::AssociationTest < ActiveSupport::TestCase
4
+
5
+ test "replicates associations" do
6
+ Organism.can_replicate with: { associations: [:toolings, reject_if: :all_blank] }
7
+
8
+ organism = organism_object
9
+ organism.tools.build(name: 'wrench')
10
+ organism.save
11
+ strand = organism.unwound(name: 'Monkey with a wrench')
12
+
13
+ assert_includes strand.pairs, :toolings_attributes
14
+ end
15
+ end
16
+
@@ -3,19 +3,20 @@ require "test_helper"
3
3
  class Replication::Modules::ProofreadingTest < ActiveSupport::TestCase
4
4
 
5
5
  def setup
6
- Organism.extend Replication::Process
7
6
  Organism.can_replicate with: :proofreading
8
7
  end
9
8
 
10
9
  test "unwound with proofreading" do
11
- organism = Organism.new(number_of_legs: 1, birth_date: Time.now) # invalid (needs name)
10
+ organism = organism_object
11
+ organism.name = nil
12
12
  strand = organism.unwound(name: 'First bacteria')
13
13
 
14
14
  assert_nil strand
15
15
  end
16
16
 
17
17
  test "replicate with proofreading" do
18
- organism = Organism.new(number_of_legs: 1, birth_date: Time.now) # invalid (needs name)
18
+ organism = organism_object
19
+ organism.name = nil
19
20
 
20
21
  assert_raises(Replication::UnwoundError) { organism.replicate(name: 'First bacteria') }
21
22
  end
@@ -2,13 +2,6 @@ require "test_helper"
2
2
 
3
3
  class Replication::Modules::SemiConservativeTest < ActiveSupport::TestCase
4
4
 
5
- def setup
6
- Organism.extend Replication::Process
7
- end
8
-
9
- def organism_object
10
- Organism.new(name: 'Bacteria', number_of_legs: 1, birth_date: Time.now)
11
- end
12
5
 
13
6
  test "unwound with default options" do
14
7
  Organism.can_replicate
data/test/test_helper.rb CHANGED
@@ -9,3 +9,10 @@ require "orm/#{REPLICATION_ORM}"
9
9
 
10
10
  require "minitest/reporters"
11
11
  Minitest::Reporters.use! [Minitest::Reporters::DefaultReporter.new(color: false)]
12
+
13
+ # Basic Setup
14
+ Organism.extend Replication::Process
15
+
16
+ def organism_object
17
+ Organism.new(name: 'Bacteria', number_of_legs: 1, birth_date: Time.now)
18
+ end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: replication
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.0
4
+ version: 0.3.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-02 00:00:00.000000000 Z
11
+ date: 2014-07-08 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -55,6 +55,7 @@ files:
55
55
  - lib/replication/active_record/strand.rb
56
56
  - lib/replication/config.rb
57
57
  - lib/replication/engine.rb
58
+ - lib/replication/modules/association.rb
58
59
  - lib/replication/modules/proofreading.rb
59
60
  - lib/replication/modules/semi_conservative.rb
60
61
  - lib/replication/process.rb
@@ -66,6 +67,8 @@ files:
66
67
  - test/rails_app/app/models/.keep
67
68
  - test/rails_app/app/models/concerns/.keep
68
69
  - test/rails_app/app/models/organism.rb
70
+ - test/rails_app/app/models/tool.rb
71
+ - test/rails_app/app/models/tooling.rb
69
72
  - test/rails_app/config.ru
70
73
  - test/rails_app/config/application.rb
71
74
  - test/rails_app/config/boot.rb
@@ -81,7 +84,9 @@ files:
81
84
  - test/rails_app/config/initializers/wrap_parameters.rb
82
85
  - test/rails_app/config/routes.rb
83
86
  - test/rails_app/config/secrets.yml
87
+ - test/rails_app/db/migrate/125162068_tool_migration.rb
84
88
  - test/rails_app/db/migrate/830335961_organism_migration.rb
89
+ - test/replication/modules/association_test.rb
85
90
  - test/replication/modules/proofreading_test.rb
86
91
  - test/replication/modules/semi_conservative_test.rb
87
92
  - test/replication/process_test.rb
@@ -117,6 +122,8 @@ test_files:
117
122
  - test/rails_app/app/models/.keep
118
123
  - test/rails_app/app/models/concerns/.keep
119
124
  - test/rails_app/app/models/organism.rb
125
+ - test/rails_app/app/models/tool.rb
126
+ - test/rails_app/app/models/tooling.rb
120
127
  - test/rails_app/config.ru
121
128
  - test/rails_app/config/application.rb
122
129
  - test/rails_app/config/boot.rb
@@ -132,7 +139,9 @@ test_files:
132
139
  - test/rails_app/config/initializers/wrap_parameters.rb
133
140
  - test/rails_app/config/routes.rb
134
141
  - test/rails_app/config/secrets.yml
142
+ - test/rails_app/db/migrate/125162068_tool_migration.rb
135
143
  - test/rails_app/db/migrate/830335961_organism_migration.rb
144
+ - test/replication/modules/association_test.rb
136
145
  - test/replication/modules/proofreading_test.rb
137
146
  - test/replication/modules/semi_conservative_test.rb
138
147
  - test/replication/process_test.rb