clowne 0.1.0.pre1 → 1.2.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (108) hide show
  1. checksums.yaml +5 -5
  2. data/.codeclimate.yml +7 -0
  3. data/.gitattributes +1 -0
  4. data/.gitignore +1 -0
  5. data/.rubocop.yml +16 -33
  6. data/.travis.yml +14 -10
  7. data/CHANGELOG.md +39 -2
  8. data/Gemfile +11 -6
  9. data/README.md +48 -384
  10. data/Rakefile +3 -3
  11. data/clowne.gemspec +16 -8
  12. data/docs/.nojekyll +0 -0
  13. data/docs/.rubocop.yml +18 -0
  14. data/docs/CNAME +1 -0
  15. data/docs/README.md +131 -0
  16. data/docs/_sidebar.md +25 -0
  17. data/docs/active_record.md +33 -0
  18. data/docs/after_clone.md +53 -0
  19. data/docs/after_persist.md +77 -0
  20. data/docs/architecture.md +138 -0
  21. data/docs/assets/docsify.min.js +1 -0
  22. data/docs/assets/prism-ruby.min.js +1 -0
  23. data/docs/assets/styles.css +348 -0
  24. data/docs/assets/vue.css +1 -0
  25. data/docs/clone_mapper.md +59 -0
  26. data/docs/customization.md +63 -0
  27. data/docs/exclude_association.md +61 -0
  28. data/docs/finalize.md +31 -0
  29. data/docs/from_v02_to_v1.md +83 -0
  30. data/docs/getting_started.md +171 -0
  31. data/docs/implicit_cloner.md +33 -0
  32. data/docs/include_association.md +133 -0
  33. data/docs/index.html +29 -0
  34. data/docs/init_as.md +40 -0
  35. data/docs/inline_configuration.md +37 -0
  36. data/docs/nullify.md +33 -0
  37. data/docs/operation.md +55 -0
  38. data/docs/parameters.md +112 -0
  39. data/docs/sequel.md +50 -0
  40. data/docs/supported_adapters.md +10 -0
  41. data/docs/testing.md +194 -0
  42. data/docs/traits.md +25 -0
  43. data/gemfiles/activerecord42.gemfile +7 -4
  44. data/gemfiles/jruby.gemfile +8 -4
  45. data/gemfiles/railsmaster.gemfile +8 -5
  46. data/lib/clowne.rb +12 -7
  47. data/lib/clowne/adapters/active_record.rb +6 -16
  48. data/lib/clowne/adapters/active_record/associations.rb +8 -6
  49. data/lib/clowne/adapters/active_record/associations/base.rb +5 -49
  50. data/lib/clowne/adapters/active_record/associations/belongs_to.rb +29 -0
  51. data/lib/clowne/adapters/active_record/associations/has_one.rb +9 -1
  52. data/lib/clowne/adapters/active_record/associations/noop.rb +4 -1
  53. data/lib/clowne/adapters/active_record/dsl.rb +33 -0
  54. data/lib/clowne/adapters/active_record/resolvers/association.rb +38 -0
  55. data/lib/clowne/adapters/base.rb +53 -41
  56. data/lib/clowne/adapters/base/association.rb +78 -0
  57. data/lib/clowne/adapters/registry.rb +54 -11
  58. data/lib/clowne/adapters/sequel.rb +29 -0
  59. data/lib/clowne/adapters/sequel/associations.rb +26 -0
  60. data/lib/clowne/adapters/sequel/associations/base.rb +27 -0
  61. data/lib/clowne/adapters/sequel/associations/many_to_many.rb +23 -0
  62. data/lib/clowne/adapters/sequel/associations/noop.rb +16 -0
  63. data/lib/clowne/adapters/sequel/associations/one_to_many.rb +28 -0
  64. data/lib/clowne/adapters/sequel/associations/one_to_one.rb +28 -0
  65. data/lib/clowne/adapters/sequel/copier.rb +23 -0
  66. data/lib/clowne/adapters/sequel/operation.rb +35 -0
  67. data/lib/clowne/adapters/sequel/record_wrapper.rb +43 -0
  68. data/lib/clowne/adapters/sequel/resolvers/after_persist.rb +22 -0
  69. data/lib/clowne/adapters/sequel/resolvers/association.rb +51 -0
  70. data/lib/clowne/adapters/sequel/specifications/after_persist_does_not_support.rb +15 -0
  71. data/lib/clowne/cloner.rb +50 -20
  72. data/lib/clowne/declarations.rb +15 -11
  73. data/lib/clowne/declarations/after_clone.rb +21 -0
  74. data/lib/clowne/declarations/after_persist.rb +21 -0
  75. data/lib/clowne/declarations/base.rb +13 -0
  76. data/lib/clowne/declarations/exclude_association.rb +1 -6
  77. data/lib/clowne/declarations/finalize.rb +3 -2
  78. data/lib/clowne/declarations/include_association.rb +16 -4
  79. data/lib/clowne/declarations/init_as.rb +21 -0
  80. data/lib/clowne/declarations/nullify.rb +3 -2
  81. data/lib/clowne/declarations/trait.rb +3 -0
  82. data/lib/clowne/dsl.rb +9 -0
  83. data/lib/clowne/ext/lambda_as_proc.rb +17 -0
  84. data/lib/clowne/ext/orm_ext.rb +21 -0
  85. data/lib/clowne/ext/record_key.rb +12 -0
  86. data/lib/clowne/ext/string_constantize.rb +10 -4
  87. data/lib/clowne/ext/yield_self_then.rb +25 -0
  88. data/lib/clowne/planner.rb +27 -7
  89. data/lib/clowne/resolvers/after_clone.rb +17 -0
  90. data/lib/clowne/resolvers/after_persist.rb +18 -0
  91. data/lib/clowne/resolvers/finalize.rb +12 -0
  92. data/lib/clowne/resolvers/init_as.rb +13 -0
  93. data/lib/clowne/resolvers/nullify.rb +15 -0
  94. data/lib/clowne/rspec.rb +5 -0
  95. data/lib/clowne/rspec/clone_association.rb +99 -0
  96. data/lib/clowne/rspec/clone_associations.rb +26 -0
  97. data/lib/clowne/rspec/helpers.rb +35 -0
  98. data/lib/clowne/utils/clone_mapper.rb +26 -0
  99. data/lib/clowne/utils/operation.rb +95 -0
  100. data/lib/clowne/utils/options.rb +39 -0
  101. data/lib/clowne/utils/params.rb +64 -0
  102. data/lib/clowne/utils/plan.rb +90 -0
  103. data/lib/clowne/version.rb +1 -1
  104. metadata +140 -20
  105. data/lib/clowne/adapters/active_record/association.rb +0 -34
  106. data/lib/clowne/adapters/base/finalize.rb +0 -19
  107. data/lib/clowne/adapters/base/nullify.rb +0 -19
  108. data/lib/clowne/plan.rb +0 -81
@@ -0,0 +1,25 @@
1
+ # Traits
2
+
3
+ Traits allow you to group cloner declarations together and then apply them (like in [`factory_bot`](https://github.com/thoughtbot/factory_bot)):
4
+
5
+ ```ruby
6
+ class UserCloner < Clowne::Cloner
7
+ trait :with_posts do
8
+ include_association :posts
9
+ end
10
+
11
+ trait :with_profile do
12
+ include_association :profile
13
+ end
14
+
15
+ trait :nullify_name do
16
+ nullify :name
17
+ end
18
+ end
19
+
20
+ UserCloner.call(user, traits: %i[with_posts with_profile nullify_name])
21
+ # or
22
+ UserCloner.call(user, traits: :nullify_name)
23
+ # or
24
+ # ...
25
+ ```
@@ -1,6 +1,9 @@
1
- source 'https://rubygems.org'
1
+ # frozen_string_literal: true
2
2
 
3
- gem 'activerecord', '~> 4.2'
4
- gem 'sqlite3'
3
+ source "https://rubygems.org"
5
4
 
6
- gemspec path: '..'
5
+ gem "activerecord", "~> 4.2"
6
+ gem "sequel", ">= 5.0"
7
+ gem "sqlite3", "~> 1.3.13"
8
+
9
+ gemspec path: ".."
@@ -1,6 +1,10 @@
1
- source 'https://rubygems.org'
1
+ # frozen_string_literal: true
2
2
 
3
- gem 'activerecord-jdbcsqlite3-adapter', '~> 50.0'
4
- gem 'activerecord', '~> 5.0.0'
3
+ source "https://rubygems.org"
5
4
 
6
- gemspec path: '..'
5
+ gem "activerecord-jdbcsqlite3-adapter", "~> 50.0"
6
+ gem "jdbc-sqlite3"
7
+ gem "activerecord", "~> 5.0.0"
8
+ gem "sequel", ">= 5.0"
9
+
10
+ gemspec path: ".."
@@ -1,7 +1,10 @@
1
- source 'https://rubygems.org'
1
+ # frozen_string_literal: true
2
2
 
3
- gem 'arel', github: 'rails/arel'
4
- gem 'rails', github: 'rails/rails'
5
- gem 'sqlite3'
3
+ source "https://rubygems.org"
6
4
 
7
- gemspec path: '..'
5
+ gem "arel", github: "rails/arel"
6
+ gem "rails", github: "rails/rails"
7
+ gem "sequel", github: "jeremyevans/sequel"
8
+ gem "sqlite3"
9
+
10
+ gemspec path: ".."
@@ -1,18 +1,21 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- require 'clowne/version'
4
- require 'clowne/declarations'
5
- require 'clowne/cloner'
3
+ require "clowne/version"
4
+ require "clowne/declarations"
5
+ require "clowne/cloner"
6
6
 
7
- require 'clowne/adapters/base'
7
+ require "clowne/adapters/base"
8
8
 
9
9
  # Declarative models cloning
10
10
  module Clowne
11
11
  # List of built-in adapters
12
+ # rubocop:disable Layout/AlignHash
12
13
  ADAPTERS = {
13
- base: 'Base',
14
- active_record: 'ActiveRecord'
14
+ base: "Base",
15
+ active_record: "ActiveRecord",
16
+ sequel: "Sequel",
15
17
  }.freeze
18
+ # rubocop:enable Layout/AlignHash
16
19
 
17
20
  class << self
18
21
  attr_reader :default_adapter, :raise_on_override
@@ -28,6 +31,7 @@ module Clowne
28
31
  elsif adapter.is_a?(Symbol)
29
32
  adapter_class = ADAPTERS[adapter]
30
33
  raise "Unknown adapter: #{adapter}" if adapter_class.nil?
34
+
31
35
  Clowne::Adapters.const_get(adapter_class).new
32
36
  else
33
37
  adapter
@@ -36,4 +40,5 @@ module Clowne
36
40
  end
37
41
  end
38
42
 
39
- require 'clowne/adapters/active_record' if defined?(::ActiveRecord)
43
+ require "clowne/adapters/active_record" if defined?(::ActiveRecord)
44
+ require "clowne/adapters/sequel" if defined?(::Sequel)
@@ -1,27 +1,17 @@
1
1
  # frozen_string_literal: true
2
2
 
3
+ require "clowne/ext/orm_ext"
4
+
3
5
  module Clowne
4
6
  module Adapters
5
7
  # Cloning adapter for ActiveRecord
6
- class ActiveRecord < Base
7
- # Adds #cloner_class method to ActiveRecord::Base
8
- module ActiveRecordExt
9
- def cloner_class
10
- return @_clowne_cloner if instance_variable_defined?(:@_clowne_cloner)
11
-
12
- cloner = "#{name}Cloner".safe_constantize
13
- return @_clowne_cloner = cloner if cloner && cloner <= Clowne::Cloner
14
-
15
- @_clowne_cloner = superclass.cloner_class if superclass.respond_to?(:cloner_class)
16
- end
17
- end
18
- end
8
+ class ActiveRecord < Base; end
19
9
  end
20
10
  end
21
11
 
22
12
  ActiveSupport.on_load(:active_record) do
23
- ::ActiveRecord::Base.extend Clowne::Adapters::ActiveRecord::ActiveRecordExt
13
+ ::ActiveRecord::Base.extend Clowne::Ext::ORMExt
24
14
  end
25
15
 
26
- require 'clowne/adapters/active_record/associations'
27
- require 'clowne/adapters/active_record/association'
16
+ require "clowne/adapters/active_record/associations"
17
+ require "clowne/adapters/active_record/resolvers/association"
@@ -1,19 +1,21 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- require 'clowne/adapters/active_record/associations/base'
4
- require 'clowne/adapters/active_record/associations/noop'
5
- require 'clowne/adapters/active_record/associations/has_one'
6
- require 'clowne/adapters/active_record/associations/has_many'
7
- require 'clowne/adapters/active_record/associations/has_and_belongs_to_many'
3
+ require "clowne/adapters/active_record/associations/base"
4
+ require "clowne/adapters/active_record/associations/noop"
5
+ require "clowne/adapters/active_record/associations/belongs_to"
6
+ require "clowne/adapters/active_record/associations/has_one"
7
+ require "clowne/adapters/active_record/associations/has_many"
8
+ require "clowne/adapters/active_record/associations/has_and_belongs_to_many"
8
9
 
9
10
  module Clowne
10
11
  module Adapters # :nodoc: all
11
12
  class ActiveRecord
12
13
  module Associations
13
14
  AR_2_CLONER = {
15
+ belongs_to: BelongsTo,
14
16
  has_one: HasOne,
15
17
  has_many: HasMany,
16
- has_and_belongs_to_many: HABTM
18
+ has_and_belongs_to_many: HABTM,
17
19
  }.freeze
18
20
 
19
21
  # Returns an association cloner class for reflection
@@ -1,61 +1,17 @@
1
1
  # frozen_string_literal: true
2
2
 
3
+ require "clowne/adapters/base/association"
4
+
3
5
  module Clowne
4
6
  module Adapters # :nodoc: all
5
7
  class ActiveRecord
6
8
  module Associations
7
- class Base
8
- # Params:
9
- # +reflection+:: Association eflection object
10
- # +source+:: Instance of cloned object (ex: User.new(posts: posts))
11
- # +declaration+:: = Relation description
12
- # (ex: Clowne::Declarations::IncludeAssociation.new(:posts))
13
- # +params+:: = Instance of Clowne::Params
14
- def initialize(reflection, source, declaration, params)
15
- @source = source
16
- @scope = declaration.scope
17
- @clone_with = declaration.clone_with
18
- @params = params
19
- @association_name = declaration.name.to_s
20
- @reflection = reflection
21
- @cloner_options = params
22
- @cloner_options.merge!(traits: declaration.traits) if declaration.traits
23
- end
24
-
25
- def call(_record)
26
- raise NotImplementedError
27
- end
28
-
29
- def association
30
- @_association ||= source.__send__(association_name)
31
- end
32
-
33
- def clone_one(child)
34
- cloner = cloner_for(child)
35
- cloner ? cloner.call(child, cloner_options) : child.dup
36
- end
37
-
38
- def with_scope
39
- base_scope = association
40
- if scope.is_a?(Symbol)
41
- base_scope.__send__(scope)
42
- elsif scope.is_a?(Proc)
43
- base_scope.instance_exec(params, &scope) || base_scope
44
- else
45
- base_scope
46
- end
47
- end
48
-
9
+ class Base < Base::Association
49
10
  private
50
11
 
51
- def cloner_for(child)
52
- return clone_with if clone_with
53
-
54
- return child.class.cloner_class if child.class.respond_to?(:cloner_class)
12
+ def init_scope
13
+ association
55
14
  end
56
-
57
- attr_reader :source, :scope, :clone_with, :params, :association_name,
58
- :reflection, :cloner_options
59
15
  end
60
16
  end
61
17
  end
@@ -0,0 +1,29 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Clowne
4
+ module Adapters # :nodoc: all
5
+ class ActiveRecord
6
+ module Associations
7
+ class BelongsTo < Base
8
+ # rubocop: disable Metrics/MethodLength
9
+ def call(record)
10
+ child = association
11
+ return record unless child
12
+
13
+ unless declaration.scope.nil?
14
+ warn(
15
+ "[Clowne] Belongs to association does not support scopes " \
16
+ "(#{@association_name} for #{@source.class})"
17
+ )
18
+ end
19
+
20
+ child_clone = clone_one(child)
21
+ record.__send__(:"#{association_name}=", child_clone)
22
+ record
23
+ end
24
+ # rubocop: enable Metrics/MethodLength
25
+ end
26
+ end
27
+ end
28
+ end
29
+ end
@@ -5,10 +5,17 @@ module Clowne
5
5
  class ActiveRecord
6
6
  module Associations
7
7
  class HasOne < Base
8
+ # rubocop: disable Metrics/MethodLength
8
9
  def call(record)
9
10
  child = association
10
11
  return record unless child
11
- warn '[Clowne] Has one association does not support scopes' unless scope.nil?
12
+
13
+ unless declaration.scope.nil?
14
+ warn(
15
+ "[Clowne] Has one association does not support scopes " \
16
+ "(#{@association_name} for #{@source.class})"
17
+ )
18
+ end
12
19
 
13
20
  child_clone = clone_one(child)
14
21
  child_clone[:"#{reflection.foreign_key}"] = nil
@@ -16,6 +23,7 @@ module Clowne
16
23
 
17
24
  record
18
25
  end
26
+ # rubocop: enable Metrics/MethodLength
19
27
  end
20
28
  end
21
29
  end
@@ -6,7 +6,10 @@ module Clowne
6
6
  module Associations
7
7
  class Noop < Base
8
8
  def call(record)
9
- warn("[Clowne] Reflection #{reflection.class.name} is not supported")
9
+ warn(
10
+ "[Clowne] Reflection #{reflection.class.name} is not supported "\
11
+ "(#{@association_name} for #{@source.class})"
12
+ )
10
13
  record
11
14
  end
12
15
  end
@@ -0,0 +1,33 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Clowne
4
+ module Adapters
5
+ # Extend ActiveRecord with Clowne DSL and methods
6
+ module ActiveRecordDSL
7
+ module InstanceMethods # :nodoc:
8
+ # Shortcut to call class's cloner call with self
9
+ def clowne(*args, &block)
10
+ self.class.cloner_class.call(self, *args, &block)
11
+ end
12
+ end
13
+
14
+ module ClassMethods # :nodoc:
15
+ def clowne_config(options = {}, &block)
16
+ if options.delete(:inherit) != false && superclass.respond_to?(:cloner_class)
17
+ parent_cloner = superclass.cloner_class
18
+ end
19
+
20
+ parent_cloner ||= Clowne::Cloner
21
+ cloner = instance_variable_set(:@_clowne_cloner, Class.new(parent_cloner))
22
+ cloner.adapter :active_record
23
+ cloner.instance_exec(&block)
24
+ end
25
+ end
26
+ end
27
+ end
28
+ end
29
+
30
+ ActiveSupport.on_load(:active_record) do
31
+ ::ActiveRecord::Base.extend Clowne::Adapters::ActiveRecordDSL::ClassMethods
32
+ ::ActiveRecord::Base.include Clowne::Adapters::ActiveRecordDSL::InstanceMethods
33
+ end
@@ -0,0 +1,38 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Clowne
4
+ module Adapters # :nodoc: all
5
+ class ActiveRecord
6
+ module Resolvers
7
+ class UnknownAssociation < StandardError; end
8
+
9
+ class Association
10
+ class << self
11
+ # rubocop: disable Metrics/ParameterLists
12
+ def call(source, record, declaration, adapter:, params:, **_options)
13
+ reflection = source.class.reflections[declaration.name.to_s]
14
+
15
+ if reflection.nil?
16
+ raise UnknownAssociation,
17
+ "Association #{declaration.name} couldn't be found for #{source.class}"
18
+ end
19
+
20
+ cloner_class = Associations.cloner_for(reflection)
21
+
22
+ cloner_class.new(reflection, source, declaration, adapter, params).call(record)
23
+
24
+ record
25
+ end
26
+ # rubocop: enable Metrics/ParameterLists
27
+ end
28
+ end
29
+ end
30
+ end
31
+ end
32
+ end
33
+
34
+ Clowne::Adapters::ActiveRecord.register_resolver(
35
+ :association,
36
+ Clowne::Adapters::ActiveRecord::Resolvers::Association,
37
+ before: :nullify
38
+ )
@@ -1,45 +1,36 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- require 'clowne/adapters/registry'
3
+ require "clowne/adapters/registry"
4
+
5
+ require "clowne/resolvers/init_as"
6
+ require "clowne/resolvers/nullify"
7
+ require "clowne/resolvers/finalize"
8
+ require "clowne/resolvers/after_persist"
9
+ require "clowne/resolvers/after_clone"
4
10
 
5
11
  module Clowne
6
12
  module Adapters
7
13
  # ORM-independant adapter (just calls #dup).
8
14
  # Works with nullify/finalize.
9
15
  class Base
10
- class << self
11
- attr_reader :registry
12
-
13
- def inherited(subclass)
14
- # Duplicate registry
15
- subclass.registry = registry.dup
16
- end
17
-
18
- def resolver_for(type)
19
- registry.mapping[type] || raise("Uknown resolver #{type} for #{self}")
20
- end
21
-
22
- def register_resolver(type, resolver, after: nil, before: nil)
23
- registry.mapping[type] = resolver
16
+ include Clowne::Adapters::Registry::Container
24
17
 
25
- if after
26
- registry.insert_after after, type
27
- elsif before
28
- registry.insert_before before, type
29
- else
30
- registry.append type
18
+ class << self
19
+ # Duplicate record and remember record <-> clone relationship in operation
20
+ # Cab be overrided in special adapter
21
+ # +record+:: Instance of record (ActiveRecord or Sequel)
22
+ def dup_record(record)
23
+ record.dup.tap do |clone|
24
+ operation = operation_class.current
25
+ operation.add_mapping(record, clone)
31
26
  end
32
27
  end
33
28
 
34
- protected
35
-
36
- attr_writer :registry
37
- end
38
-
39
- self.registry = Registry.new
40
-
41
- def registry
42
- self.class.registry
29
+ # Operation class which using for cloning
30
+ # Cab be overrided in special adapter
31
+ def operation_class
32
+ Clowne::Utils::Operation
33
+ end
43
34
  end
44
35
 
45
36
  # Using a plan make full duplicate of record
@@ -48,22 +39,43 @@ module Clowne
48
39
  # +params+:: Custom params hash
49
40
  def clone(source, plan, params: {})
50
41
  declarations = plan.declarations
51
- declarations.inject(clone_record(source)) do |record, (type, declaration)|
52
- resolver_for(type).call(source, record, declaration, params: params)
53
- end
54
- end
42
+ init_record = init_record(self.class.dup_record(source))
55
43
 
56
- def resolver_for(type)
57
- self.class.resolver_for(type)
44
+ declarations.inject(init_record) do |record, (type, declaration)|
45
+ resolver_for(type).call(source, record, declaration, params: params, adapter: self)
46
+ end
58
47
  end
59
48
 
60
- # Return #dup if any
61
- def clone_record(source)
62
- source.dup
49
+ def init_record(record)
50
+ # Override in custom adapters
51
+ record
63
52
  end
64
53
  end
65
54
  end
66
55
  end
67
56
 
68
- require 'clowne/adapters/base/nullify'
69
- require 'clowne/adapters/base/finalize'
57
+ Clowne::Adapters::Base.register_resolver(
58
+ :init_as,
59
+ Clowne::Resolvers::InitAs,
60
+ prepend: true
61
+ )
62
+
63
+ Clowne::Adapters::Base.register_resolver(
64
+ :nullify,
65
+ Clowne::Resolvers::Nullify
66
+ )
67
+
68
+ Clowne::Adapters::Base.register_resolver(
69
+ :finalize, Clowne::Resolvers::Finalize,
70
+ after: :nullify
71
+ )
72
+
73
+ Clowne::Adapters::Base.register_resolver(
74
+ :after_clone, Clowne::Resolvers::AfterClone,
75
+ after: :finalize
76
+ )
77
+
78
+ Clowne::Adapters::Base.register_resolver(
79
+ :after_persist, Clowne::Resolvers::AfterPersist,
80
+ after: :after_clone
81
+ )