clowne 0.1.0 → 1.3.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (125) hide show
  1. checksums.yaml +5 -5
  2. data/.rubocop.yml +11 -45
  3. data/.travis.yml +14 -21
  4. data/CHANGELOG.md +30 -0
  5. data/Gemfile +11 -7
  6. data/README.md +38 -18
  7. data/Rakefile +3 -3
  8. data/clowne.gemspec +17 -10
  9. data/docs/.nojekyll +0 -0
  10. data/docs/.rubocop.yml +8 -2
  11. data/docs/CNAME +1 -0
  12. data/docs/README.md +131 -0
  13. data/docs/_sidebar.md +25 -0
  14. data/docs/active_record.md +4 -7
  15. data/docs/after_clone.md +53 -0
  16. data/docs/after_persist.md +77 -0
  17. data/docs/architecture.md +2 -5
  18. data/docs/assets/docsify.min.js +1 -0
  19. data/docs/assets/prism-ruby.min.js +1 -0
  20. data/docs/assets/styles.css +348 -0
  21. data/docs/assets/vue.css +1 -0
  22. data/docs/clone_mapper.md +59 -0
  23. data/docs/customization.md +6 -7
  24. data/docs/exclude_association.md +6 -8
  25. data/docs/finalize.md +11 -15
  26. data/docs/from_v02_to_v1.md +83 -0
  27. data/docs/getting_started.md +171 -0
  28. data/docs/implicit_cloner.md +2 -5
  29. data/docs/include_association.md +24 -10
  30. data/docs/index.html +29 -0
  31. data/docs/init_as.md +13 -9
  32. data/docs/inline_configuration.md +5 -6
  33. data/docs/nullify.md +3 -7
  34. data/docs/operation.md +55 -0
  35. data/docs/parameters.md +112 -0
  36. data/docs/sequel.md +16 -22
  37. data/docs/supported_adapters.md +3 -6
  38. data/docs/testing.md +194 -0
  39. data/docs/traits.md +1 -4
  40. data/gemfiles/activerecord42.gemfile +7 -5
  41. data/gemfiles/jruby.gemfile +8 -6
  42. data/gemfiles/railsmaster.gemfile +8 -6
  43. data/lib/clowne.rb +12 -9
  44. data/lib/clowne/adapters/active_record.rb +4 -5
  45. data/lib/clowne/adapters/active_record/associations.rb +8 -6
  46. data/lib/clowne/adapters/active_record/associations/base.rb +1 -5
  47. data/lib/clowne/adapters/active_record/associations/belongs_to.rb +29 -0
  48. data/lib/clowne/adapters/active_record/associations/has_one.rb +3 -2
  49. data/lib/clowne/adapters/active_record/dsl.rb +2 -2
  50. data/lib/clowne/adapters/active_record/resolvers/association.rb +38 -0
  51. data/lib/clowne/adapters/base.rb +49 -44
  52. data/lib/clowne/adapters/base/association.rb +24 -15
  53. data/lib/clowne/adapters/registry.rb +49 -0
  54. data/lib/clowne/adapters/sequel.rb +14 -10
  55. data/lib/clowne/adapters/sequel/associations.rb +6 -6
  56. data/lib/clowne/adapters/sequel/associations/base.rb +9 -5
  57. data/lib/clowne/adapters/sequel/associations/many_to_many.rb +6 -2
  58. data/lib/clowne/adapters/sequel/associations/one_to_many.rb +7 -2
  59. data/lib/clowne/adapters/sequel/associations/one_to_one.rb +7 -2
  60. data/lib/clowne/adapters/sequel/operation.rb +35 -0
  61. data/lib/clowne/adapters/sequel/record_wrapper.rb +0 -16
  62. data/lib/clowne/adapters/sequel/resolvers/after_persist.rb +22 -0
  63. data/lib/clowne/adapters/sequel/resolvers/association.rb +51 -0
  64. data/lib/clowne/adapters/sequel/specifications/after_persist_does_not_support.rb +15 -0
  65. data/lib/clowne/cloner.rb +49 -21
  66. data/lib/clowne/declarations.rb +15 -12
  67. data/lib/clowne/declarations/after_clone.rb +21 -0
  68. data/lib/clowne/declarations/after_persist.rb +21 -0
  69. data/lib/clowne/declarations/base.rb +13 -0
  70. data/lib/clowne/declarations/exclude_association.rb +2 -2
  71. data/lib/clowne/declarations/finalize.rb +5 -4
  72. data/lib/clowne/declarations/include_association.rb +16 -2
  73. data/lib/clowne/declarations/init_as.rb +5 -4
  74. data/lib/clowne/declarations/nullify.rb +4 -3
  75. data/lib/clowne/declarations/trait.rb +1 -0
  76. data/lib/clowne/dsl.rb +9 -0
  77. data/lib/clowne/ext/lambda_as_proc.rb +17 -0
  78. data/lib/clowne/ext/orm_ext.rb +1 -1
  79. data/lib/clowne/ext/record_key.rb +12 -0
  80. data/lib/clowne/ext/string_constantize.rb +9 -3
  81. data/lib/clowne/ext/yield_self_then.rb +25 -0
  82. data/lib/clowne/planner.rb +16 -3
  83. data/lib/clowne/resolvers/after_clone.rb +17 -0
  84. data/lib/clowne/resolvers/after_persist.rb +18 -0
  85. data/lib/clowne/resolvers/finalize.rb +12 -0
  86. data/lib/clowne/resolvers/init_as.rb +13 -0
  87. data/lib/clowne/resolvers/nullify.rb +15 -0
  88. data/lib/clowne/rspec.rb +5 -0
  89. data/lib/clowne/rspec/clone_association.rb +99 -0
  90. data/lib/clowne/rspec/clone_associations.rb +26 -0
  91. data/lib/clowne/rspec/helpers.rb +35 -0
  92. data/lib/clowne/utils/clone_mapper.rb +26 -0
  93. data/lib/clowne/utils/operation.rb +95 -0
  94. data/lib/clowne/utils/options.rb +39 -0
  95. data/lib/clowne/utils/params.rb +64 -0
  96. data/lib/clowne/utils/plan.rb +90 -0
  97. data/lib/clowne/version.rb +1 -1
  98. metadata +99 -45
  99. data/docs/alternatives.md +0 -26
  100. data/docs/basic_example.md +0 -66
  101. data/docs/configuration.md +0 -29
  102. data/docs/execution_order.md +0 -14
  103. data/docs/installation.md +0 -16
  104. data/docs/web/.gitignore +0 -11
  105. data/docs/web/core/Footer.js +0 -92
  106. data/docs/web/i18n/en.json +0 -134
  107. data/docs/web/package.json +0 -14
  108. data/docs/web/pages/en/help.js +0 -50
  109. data/docs/web/pages/en/index.js +0 -231
  110. data/docs/web/pages/en/users.js +0 -47
  111. data/docs/web/sidebars.json +0 -30
  112. data/docs/web/siteConfig.js +0 -44
  113. data/docs/web/static/css/custom.css +0 -229
  114. data/docs/web/static/fonts/FiraCode-Medium.woff +0 -0
  115. data/docs/web/static/fonts/FiraCode-Regular.woff +0 -0
  116. data/docs/web/static/fonts/StemText.woff +0 -0
  117. data/docs/web/static/fonts/StemTextBold.woff +0 -0
  118. data/docs/web/static/img/favicon/favicon.ico +0 -0
  119. data/docs/web/yarn.lock +0 -1741
  120. data/lib/clowne/adapters/active_record/association.rb +0 -34
  121. data/lib/clowne/adapters/base/finalize.rb +0 -19
  122. data/lib/clowne/adapters/base/init_as.rb +0 -21
  123. data/lib/clowne/adapters/base/nullify.rb +0 -19
  124. data/lib/clowne/adapters/sequel/association.rb +0 -47
  125. data/lib/clowne/plan.rb +0 -81
data/docs/traits.md CHANGED
@@ -1,7 +1,4 @@
1
- ---
2
- id: traits
3
- title: Traits
4
- ---
1
+ # Traits
5
2
 
6
3
  Traits allow you to group cloner declarations together and then apply them (like in [`factory_bot`](https://github.com/thoughtbot/factory_bot)):
7
4
 
@@ -1,7 +1,9 @@
1
- source 'https://rubygems.org'
1
+ # frozen_string_literal: true
2
2
 
3
- gem 'activerecord', '~> 4.2'
4
- gem 'sequel', '>= 5.0'
5
- gem 'sqlite3'
3
+ source "https://rubygems.org"
6
4
 
7
- gemspec path: '..'
5
+ gem "activerecord", "~> 4.2"
6
+ gem "sequel", ">= 5.0"
7
+ gem "sqlite3", "~> 1.3.13"
8
+
9
+ gemspec path: ".."
@@ -1,8 +1,10 @@
1
- source 'https://rubygems.org'
1
+ # frozen_string_literal: true
2
2
 
3
- gem 'activerecord-jdbcsqlite3-adapter', '~> 50.0'
4
- gem 'jdbc-sqlite3'
5
- gem 'activerecord', '~> 5.0.0'
6
- gem 'sequel', '>= 5.0'
3
+ source "https://rubygems.org"
7
4
 
8
- 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,8 +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 'sequel', github: 'jeremyevans/sequel'
6
- gem 'sqlite3'
3
+ source "https://rubygems.org"
7
4
 
8
- 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: ".."
data/lib/clowne.rb CHANGED
@@ -1,19 +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',
15
- sequel: 'Sequel'
14
+ base: "Base",
15
+ active_record: "ActiveRecord",
16
+ sequel: "Sequel",
16
17
  }.freeze
18
+ # rubocop:enable Layout/AlignHash
17
19
 
18
20
  class << self
19
21
  attr_reader :default_adapter, :raise_on_override
@@ -29,6 +31,7 @@ module Clowne
29
31
  elsif adapter.is_a?(Symbol)
30
32
  adapter_class = ADAPTERS[adapter]
31
33
  raise "Unknown adapter: #{adapter}" if adapter_class.nil?
34
+
32
35
  Clowne::Adapters.const_get(adapter_class).new
33
36
  else
34
37
  adapter
@@ -37,5 +40,5 @@ module Clowne
37
40
  end
38
41
  end
39
42
 
40
- require 'clowne/adapters/active_record' if defined?(::ActiveRecord)
41
- require 'clowne/adapters/sequel' if defined?(::Sequel)
43
+ require "clowne/adapters/active_record" if defined?(::ActiveRecord)
44
+ require "clowne/adapters/sequel" if defined?(::Sequel)
@@ -1,12 +1,11 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- require 'clowne/ext/orm_ext'
3
+ require "clowne/ext/orm_ext"
4
4
 
5
5
  module Clowne
6
6
  module Adapters
7
7
  # Cloning adapter for ActiveRecord
8
- class ActiveRecord < Base
9
- end
8
+ class ActiveRecord < Base; end
10
9
  end
11
10
  end
12
11
 
@@ -14,5 +13,5 @@ ActiveSupport.on_load(:active_record) do
14
13
  ::ActiveRecord::Base.extend Clowne::Ext::ORMExt
15
14
  end
16
15
 
17
- require 'clowne/adapters/active_record/associations'
18
- 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,6 +1,6 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- require 'clowne/adapters/base/association'
3
+ require "clowne/adapters/base/association"
4
4
 
5
5
  module Clowne
6
6
  module Adapters # :nodoc: all
@@ -9,10 +9,6 @@ module Clowne
9
9
  class Base < Base::Association
10
10
  private
11
11
 
12
- def clone_record(record)
13
- record.dup
14
- end
15
-
16
12
  def init_scope
17
13
  association
18
14
  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
@@ -9,9 +9,10 @@ module Clowne
9
9
  def call(record)
10
10
  child = association
11
11
  return record unless child
12
- unless scope.nil?
12
+
13
+ unless declaration.scope.nil?
13
14
  warn(
14
- '[Clowne] Has one association does not support scopes ' \
15
+ "[Clowne] Has one association does not support scopes " \
15
16
  "(#{@association_name} for #{@source.class})"
16
17
  )
17
18
  end
@@ -6,8 +6,8 @@ module Clowne
6
6
  module ActiveRecordDSL
7
7
  module InstanceMethods # :nodoc:
8
8
  # Shortcut to call class's cloner call with self
9
- def clowne(*args, &block)
10
- self.class.cloner_class.call(self, *args, &block)
9
+ def clowne(**args, &block)
10
+ self.class.cloner_class.call(self, **args, &block)
11
11
  end
12
12
  end
13
13
 
@@ -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,47 +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
16
+ include Clowne::Adapters::Registry::Container
21
17
 
22
- def register_resolver(type, resolver, after: nil, before: nil, prepend: nil)
23
- registry.mapping[type] = resolver
24
-
25
- if prepend
26
- registry.unshift type
27
- elsif after
28
- registry.insert_after after, type
29
- elsif before
30
- registry.insert_before before, type
31
- else
32
- 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)
33
26
  end
34
27
  end
35
28
 
36
- protected
37
-
38
- attr_writer :registry
39
- end
40
-
41
- self.registry = Registry.new
42
-
43
- def registry
44
- 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
45
34
  end
46
35
 
47
36
  # Using a plan make full duplicate of record
@@ -50,19 +39,13 @@ module Clowne
50
39
  # +params+:: Custom params hash
51
40
  def clone(source, plan, params: {})
52
41
  declarations = plan.declarations
53
- declarations.inject(init_record(dup_source(source))) do |record, (type, declaration)|
42
+ init_record = init_record(self.class.dup_record(source))
43
+
44
+ declarations.inject(init_record) do |record, (type, declaration)|
54
45
  resolver_for(type).call(source, record, declaration, params: params, adapter: self)
55
46
  end
56
47
  end
57
48
 
58
- def resolver_for(type)
59
- self.class.resolver_for(type)
60
- end
61
-
62
- def dup_source(source)
63
- source.dup
64
- end
65
-
66
49
  def init_record(record)
67
50
  # Override in custom adapters
68
51
  record
@@ -71,6 +54,28 @@ module Clowne
71
54
  end
72
55
  end
73
56
 
74
- require 'clowne/adapters/base/init_as'
75
- require 'clowne/adapters/base/nullify'
76
- 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
+ )
@@ -9,16 +9,15 @@ module Clowne
9
9
  # +source+:: Instance of cloned object (ex: User.new(posts: posts))
10
10
  # +declaration+:: = Relation description
11
11
  # (ex: Clowne::Declarations::IncludeAssociation.new(:posts))
12
+ # +adapter+:: Clowne adapter
12
13
  # +params+:: = Instance of Hash
13
- def initialize(reflection, source, declaration, params)
14
+ def initialize(reflection, source, declaration, adapter, params)
14
15
  @source = source
15
- @scope = declaration.scope
16
- @clone_with = declaration.clone_with
16
+ @declaration = declaration
17
+ @adapter = adapter
17
18
  @params = params
18
19
  @association_name = declaration.name.to_s
19
20
  @reflection = reflection
20
- @cloner_options = params
21
- @cloner_options.merge!(traits: declaration.traits) if declaration.traits
22
21
  end
23
22
 
24
23
  def call(_record)
@@ -31,24 +30,24 @@ module Clowne
31
30
 
32
31
  def clone_one(child)
33
32
  cloner = cloner_for(child)
34
- cloner ? cloner.call(child, cloner_options) : clone_record(child)
33
+ cloner ? cloner.call(child, **cloner_options) : dup_record(child)
35
34
  end
36
35
 
37
36
  def with_scope
38
- base_scope = init_scope
37
+ scope = declaration.scope
39
38
  if scope.is_a?(Symbol)
40
- base_scope.__send__(scope)
39
+ init_scope.__send__(scope)
41
40
  elsif scope.is_a?(Proc)
42
- base_scope.instance_exec(params, &scope) || base_scope
41
+ init_scope.instance_exec(params, &scope) || init_scope
43
42
  else
44
- base_scope
43
+ init_scope
45
44
  end.to_a
46
45
  end
47
46
 
48
47
  private
49
48
 
50
- def clone_record(_record)
51
- raise NotImplementedError
49
+ def dup_record(record)
50
+ adapter.class.dup_record(record)
52
51
  end
53
52
 
54
53
  def init_scope
@@ -56,13 +55,23 @@ module Clowne
56
55
  end
57
56
 
58
57
  def cloner_for(child)
59
- return clone_with if clone_with
58
+ return declaration.clone_with if declaration.clone_with
60
59
 
61
60
  return child.class.cloner_class if child.class.respond_to?(:cloner_class)
62
61
  end
63
62
 
64
- attr_reader :source, :scope, :clone_with, :params, :association_name,
65
- :reflection, :cloner_options
63
+ def cloner_options
64
+ return @_cloner_options if defined?(@_cloner_options)
65
+
66
+ @_cloner_options = declaration.params_proxy.permit(
67
+ params: params, parent: source
68
+ ).tap do |options|
69
+ options[:adapter] = adapter
70
+ options.merge!(traits: declaration.traits) if declaration.traits
71
+ end
72
+ end
73
+
74
+ attr_reader :source, :declaration, :adapter, :params, :association_name, :reflection
66
75
  end
67
76
  end
68
77
  end