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
@@ -3,6 +3,55 @@
3
3
  module Clowne
4
4
  module Adapters
5
5
  class Registry # :nodoc: all
6
+ module Container
7
+ def self.included(base)
8
+ base.extend ClassMethods
9
+
10
+ base.class_eval do
11
+ self.registry = Registry.new
12
+
13
+ def registry
14
+ self.class.registry
15
+ end
16
+
17
+ def resolver_for(type)
18
+ self.class.resolver_for(type)
19
+ end
20
+ end
21
+ end
22
+
23
+ module ClassMethods
24
+ attr_reader :registry
25
+
26
+ def inherited(subclass)
27
+ # Duplicate registry
28
+ subclass.registry = registry.dup
29
+ end
30
+
31
+ def resolver_for(type)
32
+ registry.mapping[type] || raise("Uknown resolver #{type} for #{self}")
33
+ end
34
+
35
+ def register_resolver(type, resolver, after: nil, before: nil, prepend: nil)
36
+ registry.mapping[type] = resolver
37
+
38
+ if prepend
39
+ registry.unshift type
40
+ elsif after
41
+ registry.insert_after after, type
42
+ elsif before
43
+ registry.insert_before before, type
44
+ else
45
+ registry.append type
46
+ end
47
+ end
48
+
49
+ protected
50
+
51
+ attr_writer :registry
52
+ end
53
+ end
54
+
6
55
  attr_reader :actions, :mapping
7
56
 
8
57
  def initialize
@@ -1,17 +1,19 @@
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 Sequel
8
8
  class Sequel < Base
9
- def init_record(source)
10
- RecordWrapper.new(source)
11
- end
9
+ class << self
10
+ def dup_record(record)
11
+ Clowne::Adapters::Sequel::Copier.call(record)
12
+ end
12
13
 
13
- def dup_source(source)
14
- Clowne::Adapters::Sequel::Copier.call(source)
14
+ def operation_class
15
+ Clowne::Adapters::Sequel::Operation
16
+ end
15
17
  end
16
18
  end
17
19
  end
@@ -19,7 +21,9 @@ end
19
21
 
20
22
  ::Sequel::Model.extend Clowne::Ext::ORMExt
21
23
 
22
- require 'clowne/adapters/sequel/associations'
23
- require 'clowne/adapters/sequel/association'
24
- require 'clowne/adapters/sequel/copier'
25
- require 'clowne/adapters/sequel/record_wrapper'
24
+ require "clowne/adapters/sequel/operation"
25
+ require "clowne/adapters/sequel/associations"
26
+ require "clowne/adapters/sequel/copier"
27
+ require "clowne/adapters/sequel/record_wrapper"
28
+ require "clowne/adapters/sequel/resolvers/association"
29
+ require "clowne/adapters/sequel/resolvers/after_persist"
@@ -1,10 +1,10 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- require 'clowne/adapters/sequel/associations/base'
4
- require 'clowne/adapters/sequel/associations/noop'
5
- require 'clowne/adapters/sequel/associations/one_to_one'
6
- require 'clowne/adapters/sequel/associations/one_to_many'
7
- require 'clowne/adapters/sequel/associations/many_to_many'
3
+ require "clowne/adapters/sequel/associations/base"
4
+ require "clowne/adapters/sequel/associations/noop"
5
+ require "clowne/adapters/sequel/associations/one_to_one"
6
+ require "clowne/adapters/sequel/associations/one_to_many"
7
+ require "clowne/adapters/sequel/associations/many_to_many"
8
8
 
9
9
  module Clowne
10
10
  module Adapters # :nodoc: all
@@ -13,7 +13,7 @@ module Clowne
13
13
  SEQUEL_2_CLONER = {
14
14
  one_to_one: OneToOne,
15
15
  one_to_many: OneToMany,
16
- many_to_many: ManyToMany
16
+ many_to_many: ManyToMany,
17
17
  }.freeze
18
18
 
19
19
  # 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,12 +9,16 @@ module Clowne
9
9
  class Base < Base::Association
10
10
  private
11
11
 
12
- def clone_record(record)
13
- Clowne::Adapters::Sequel::Copier.call(record)
12
+ def init_scope
13
+ @_init_scope ||= source.__send__([association_name, "dataset"].join("_"))
14
14
  end
15
15
 
16
- def init_scope
17
- @_init_scope ||= source.__send__([association_name, 'dataset'].join('_'))
16
+ def record_wrapper(record)
17
+ operation.record_wrapper(record)
18
+ end
19
+
20
+ def operation
21
+ @_operation ||= adapter.class.operation_class.current
18
22
  end
19
23
  end
20
24
  end
@@ -6,9 +6,13 @@ module Clowne
6
6
  module Associations
7
7
  class ManyToMany < Base
8
8
  def call(record)
9
- clones = with_scope.map { |child| clone_one(child) }
9
+ clones = with_scope
10
+ .lazy
11
+ .map(&method(:clone_one))
12
+ .map(&method(:record_wrapper))
13
+ .to_a
10
14
 
11
- record.remember_assoc(:"#{association_name}_attributes", clones)
15
+ record_wrapper(record).remember_assoc(:"#{association_name}_attributes", clones)
12
16
 
13
17
  record
14
18
  end
@@ -1,5 +1,9 @@
1
1
  # frozen_string_literal: true
2
2
 
3
+ require "clowne/ext/yield_self_then"
4
+
5
+ using Clowne::Ext::YieldSelfThen
6
+
3
7
  module Clowne
4
8
  module Adapters # :nodoc: all
5
9
  class Sequel
@@ -10,9 +14,10 @@ module Clowne
10
14
  with_scope.map do |child|
11
15
  clone_one(child).tap do |child_clone|
12
16
  child_clone[:"#{reflection[:key]}"] = nil
13
- end
17
+ end.then(&method(:record_wrapper))
14
18
  end
15
- record.remember_assoc(:"#{association_name}_attributes", clones)
19
+
20
+ record_wrapper(record).remember_assoc(:"#{association_name}_attributes", clones)
16
21
 
17
22
  record
18
23
  end
@@ -8,11 +8,16 @@ module Clowne
8
8
  def call(record)
9
9
  child = association
10
10
  return record unless child
11
- warn '[Clowne] Has one association does not support scopes' unless scope.nil?
11
+
12
+ warn "[Clowne] Has one association does not support scope" unless declaration.scope.nil?
12
13
 
13
14
  child_clone = clone_one(child)
14
15
  child_clone[:"#{reflection[:key]}"] = nil
15
- record.remember_assoc(:"#{association_name}_attributes", child_clone)
16
+
17
+ record_wrapper(record).remember_assoc(
18
+ :"#{association_name}_attributes",
19
+ record_wrapper(child_clone)
20
+ )
16
21
 
17
22
  record
18
23
  end
@@ -0,0 +1,35 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "clowne/ext/record_key"
4
+
5
+ module Clowne
6
+ module Adapters
7
+ class Sequel # :nodoc: all
8
+ class Operation < Clowne::Utils::Operation
9
+ include Clowne::Ext::RecordKey
10
+ def initialize(mapper)
11
+ super
12
+ @records = {}
13
+ end
14
+
15
+ def record_wrapper(record)
16
+ @records[key(record)] ||= RecordWrapper.new(record)
17
+ end
18
+
19
+ def hash
20
+ @records[key(@clone)]
21
+ end
22
+
23
+ def to_record
24
+ return @_record if defined?(@_record)
25
+
26
+ record_wrapper(@clone)
27
+
28
+ @_record = @records[key(@clone)].to_model.tap do
29
+ run_after_clone
30
+ end
31
+ end
32
+ end
33
+ end
34
+ end
35
+ end
@@ -15,10 +15,6 @@ module Clowne
15
15
  @association_store[association] = value
16
16
  end
17
17
 
18
- def save
19
- to_model.save
20
- end
21
-
22
18
  def to_model
23
19
  association_store.each_with_object(record) do |(name, value), acc|
24
20
  acc.send("#{name}=", association_to_model(value))
@@ -32,18 +28,6 @@ module Clowne
32
28
  end
33
29
  end
34
30
 
35
- def respond_to_missing?(method_name, include_private = false)
36
- record.respond_to?(method_name) || super
37
- end
38
-
39
- def method_missing(method_name, *args, &block)
40
- if record.respond_to?(method_name)
41
- record.public_send(method_name, *args, &block)
42
- else
43
- super
44
- end
45
- end
46
-
47
31
  private
48
32
 
49
33
  def association_to_model(assoc)
@@ -0,0 +1,22 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "clowne/adapters/sequel/specifications/after_persist_does_not_support"
4
+
5
+ module Clowne
6
+ module Adapters # :nodoc: all
7
+ class Sequel
8
+ module Resolvers
9
+ class AfterPersist
10
+ def self.call(_source, _record, _declaration, **)
11
+ raise Specifications::AfterPersistDoesNotSupportException
12
+ end
13
+ end
14
+ end
15
+ end
16
+ end
17
+ end
18
+
19
+ Clowne::Adapters::Sequel.register_resolver(
20
+ :after_persist,
21
+ Clowne::Adapters::Sequel::Resolvers::AfterPersist
22
+ )
@@ -0,0 +1,51 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Clowne
4
+ module Adapters # :nodoc: all
5
+ class Sequel
6
+ module Resolvers
7
+ class Association
8
+ class << self
9
+ # rubocop: disable Metrics/ParameterLists
10
+ def call(source, record, declaration, adapter:, params:, **_options)
11
+ with_clonable(source, record, declaration) do
12
+ reflection = source.class.association_reflections[declaration.name.to_sym]
13
+
14
+ cloner_class = Associations.cloner_for(reflection)
15
+
16
+ cloner_class.new(reflection, source, declaration, adapter, params).call(record)
17
+
18
+ record
19
+ end
20
+ end
21
+ # rubocop: enable Metrics/ParameterLists
22
+
23
+ private
24
+
25
+ def with_clonable(source, record, declaration)
26
+ if clonable_assoc?(source, declaration)
27
+ yield(record)
28
+ else
29
+ warn <<-WARN
30
+ Relation #{declaration.name} of #{source.class.name} does not configure for Sequel::Plugins::NestedAttributes
31
+ WARN
32
+ end
33
+
34
+ record
35
+ end
36
+
37
+ def clonable_assoc?(source, declaration)
38
+ source.class.plugins.include?(::Sequel::Plugins::NestedAttributes) &&
39
+ source.respond_to?(:"#{declaration.name}_attributes=")
40
+ end
41
+ end
42
+ end
43
+ end
44
+ end
45
+ end
46
+ end
47
+
48
+ Clowne::Adapters::Sequel.register_resolver(
49
+ :association,
50
+ Clowne::Adapters::Sequel::Resolvers::Association
51
+ )
@@ -0,0 +1,15 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Clowne
4
+ module Adapters
5
+ class Sequel
6
+ module Specifications # :nodoc: all
7
+ class AfterPersistDoesNotSupportException < StandardError
8
+ def message
9
+ "Sequel adapter does not support after_persist callback"
10
+ end
11
+ end
12
+ end
13
+ end
14
+ end
15
+ end
data/lib/clowne/cloner.rb CHANGED
@@ -1,7 +1,10 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- require 'clowne/planner'
4
- require 'clowne/dsl'
3
+ require "clowne/planner"
4
+ require "clowne/dsl"
5
+ require "clowne/utils/options"
6
+ require "clowne/utils/params"
7
+ require "clowne/utils/operation"
5
8
 
6
9
  module Clowne # :nodoc: all
7
10
  class UnprocessableSourceError < StandardError; end
@@ -24,11 +27,13 @@ module Clowne # :nodoc: all
24
27
 
25
28
  def declarations
26
29
  return @declarations if instance_variable_defined?(:@declarations)
30
+
27
31
  @declarations = []
28
32
  end
29
33
 
30
34
  def traits
31
35
  return @traits if instance_variable_defined?(:@traits)
36
+
32
37
  @traits = {}
33
38
  end
34
39
 
@@ -39,42 +44,46 @@ module Clowne # :nodoc: all
39
44
  end
40
45
 
41
46
  # rubocop: disable Metrics/AbcSize, Metrics/MethodLength
42
- # rubocop: disable Metrics/CyclomaticComplexity, Metrics/PerceivedComplexity
43
- def call(object, **options)
44
- raise(UnprocessableSourceError, 'Nil is not cloneable object') if object.nil?
45
-
46
- raise(ConfigurationError, 'Adapter is not defined') if adapter.nil?
47
+ def call(object, **options, &block)
48
+ raise(UnprocessableSourceError, "Nil is not cloneable object") if object.nil?
47
49
 
48
- traits = options.delete(:traits)
50
+ options = Clowne::Utils::Options.new(options)
51
+ current_adapter = current_adapter(options.adapter)
49
52
 
50
- traits = Array(traits) unless traits.nil?
53
+ raise(ConfigurationError, "Adapter is not defined") if current_adapter.nil?
51
54
 
52
55
  plan =
53
- if traits.nil? || traits.empty?
54
- default_plan
56
+ if options.traits.empty?
57
+ default_plan(current_adapter: current_adapter)
55
58
  else
56
- plan_with_traits(traits)
59
+ plan_with_traits(options.traits, current_adapter: current_adapter)
57
60
  end
58
61
 
59
- plan = Clowne::Planner.enhance(plan, Proc.new) if block_given?
62
+ plan = Clowne::Planner.enhance(plan, block) if block
60
63
 
61
- adapter.clone(object, plan, params: options)
62
- end
64
+ plan = Clowne::Planner.filter_declarations(plan, options.only)
63
65
 
66
+ call_operation(current_adapter, object, plan, options)
67
+ end
64
68
  # rubocop: enable Metrics/AbcSize, Metrics/MethodLength
65
- # rubocop: enable Metrics/CyclomaticComplexity, Metrics/PerceivedComplexity
66
69
 
67
- def default_plan
70
+ def partial_apply(only, *args, **hargs)
71
+ call(*args, **hargs, clowne_only_actions: prepare_only(only))
72
+ end
73
+
74
+ def default_plan(current_adapter: adapter)
68
75
  return @default_plan if instance_variable_defined?(:@default_plan)
69
- @default_plan = Clowne::Planner.compile(self)
76
+
77
+ @default_plan = Clowne::Planner.compile(current_adapter, self)
70
78
  end
71
79
 
72
- def plan_with_traits(ids)
80
+ def plan_with_traits(ids, current_adapter: adapter)
73
81
  # Cache plans for combinations of traits
74
- traits_id = ids.map(&:to_s).join(':')
82
+ traits_id = ids.map(&:to_s).join(":")
75
83
  return traits_plans[traits_id] if traits_plans.key?(traits_id)
84
+
76
85
  traits_plans[traits_id] = Clowne::Planner.compile(
77
- self, traits: ids
86
+ current_adapter, self, traits: ids
78
87
  )
79
88
  end
80
89
 
@@ -84,10 +93,29 @@ module Clowne # :nodoc: all
84
93
 
85
94
  private
86
95
 
96
+ def call_operation(adapter, object, plan, options)
97
+ adapter.class.operation_class.wrap(mapper: options.mapper) do
98
+ adapter.clone(object, plan, params: options.params)
99
+ end
100
+ end
101
+
87
102
  def traits_plans
88
103
  return @traits_plans if instance_variable_defined?(:@traits_plans)
104
+
89
105
  @traits_plans = {}
90
106
  end
107
+
108
+ def prepare_only(val)
109
+ val = Array.wrap(val)
110
+ val.each_with_object({}) do |type, acc|
111
+ # type is a Symbol or Hash
112
+ if type.is_a?(Hash)
113
+ acc.merge!(type)
114
+ elsif type.is_a?(Symbol)
115
+ acc[type] = nil
116
+ end
117
+ end
118
+ end
91
119
  end
92
120
  end
93
121
  end