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
@@ -0,0 +1,15 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Clowne
4
+ class Resolvers
5
+ module Nullify # :nodoc: all
6
+ def self.call(_source, record, declaration, **_options)
7
+ declaration.attributes.each do |attr|
8
+ record.__send__("#{attr}=", nil)
9
+ end
10
+
11
+ record
12
+ end
13
+ end
14
+ end
15
+ end
@@ -0,0 +1,5 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "clowne/rspec/helpers"
4
+ require "clowne/rspec/clone_associations"
5
+ require "clowne/rspec/clone_association"
@@ -0,0 +1,99 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Clowne
4
+ module RSpec
5
+ module Matchers # :nodoc: all
6
+ class CloneAssociation < ::RSpec::Matchers::BuiltIn::BaseMatcher
7
+ include Clowne::RSpec::Helpers
8
+
9
+ AVAILABLE_PARAMS = %i[
10
+ traits
11
+ clone_with
12
+ params
13
+ scope
14
+ ].freeze
15
+
16
+ attr_reader :expected_params
17
+
18
+ def initialize(name, options)
19
+ @expected = name
20
+ extract_options! options
21
+ end
22
+
23
+ # rubocop: disable Metrics/AbcSize
24
+ def match(expected, _actual)
25
+ @actual = plan.declarations
26
+ .find { |key, decl| key == :association && decl.name == expected }
27
+
28
+ return false if @actual.nil?
29
+
30
+ @actual = actual.last
31
+
32
+ AVAILABLE_PARAMS.each do |param|
33
+ if expected_params[param] != UNDEFINED
34
+ return false if expected_params[param] != actual.send(param)
35
+ end
36
+ end
37
+
38
+ true
39
+ end
40
+ # rubocop: enable Metrics/AbcSize
41
+
42
+ def does_not_match?(*)
43
+ raise "This matcher doesn't support negation"
44
+ end
45
+
46
+ def failure_message
47
+ if @actual.nil?
48
+ "expected to include association #{expected}, but none found"
49
+ else
50
+ params_failure_message
51
+ end
52
+ end
53
+
54
+ private
55
+
56
+ def extract_options!(options)
57
+ @expected_params = {}
58
+
59
+ AVAILABLE_PARAMS.each do |param|
60
+ expected_params[param] = options.fetch(param, UNDEFINED)
61
+ end
62
+
63
+ raise ArgumentError, "Lambda scope is not supported" if
64
+ expected_params[:scope].is_a?(Proc)
65
+
66
+ raise ArgumentError, "Lambda params is not supported" if
67
+ expected_params[:params].is_a?(Proc)
68
+ end
69
+
70
+ def params_failure_message
71
+ "expected :#{@expected} association " \
72
+ "to have options #{formatted_expected_params}, " \
73
+ "but got #{formatted_actual_params}"
74
+ end
75
+
76
+ def formatted_expected_params
77
+ ::RSpec::Support::ObjectFormatter.format(
78
+ expected_params.reject { |_, v| v == UNDEFINED }
79
+ )
80
+ end
81
+
82
+ def formatted_actual_params
83
+ actual_params = AVAILABLE_PARAMS.each_with_object({}) do |key, acc|
84
+ acc[key] = actual.send(key)
85
+ end
86
+ ::RSpec::Support::ObjectFormatter.format(actual_params)
87
+ end
88
+ end
89
+ end
90
+ end
91
+ end
92
+
93
+ RSpec.configure do |config|
94
+ config.include(Module.new do
95
+ def clone_association(expected, **options)
96
+ Clowne::RSpec::Matchers::CloneAssociation.new(expected, options)
97
+ end
98
+ end, type: :cloner)
99
+ end
@@ -0,0 +1,26 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Clowne
4
+ module RSpec
5
+ module Matchers # :nodoc: all
6
+ # `clone_associations` matcher is just an extension of `contain_exactly` matcher
7
+ class CloneAssociations < ::RSpec::Matchers::BuiltIn::ContainExactly
8
+ include Clowne::RSpec::Helpers
9
+
10
+ def convert_actual_to_an_array
11
+ @actual = plan.declarations
12
+ .select { |key, _| key == :association }
13
+ .map { |_, decl| decl.name }
14
+ end
15
+ end
16
+ end
17
+ end
18
+ end
19
+
20
+ RSpec.configure do |config|
21
+ config.include(Module.new do
22
+ def clone_associations(*expected)
23
+ Clowne::RSpec::Matchers::CloneAssociations.new(expected)
24
+ end
25
+ end, type: :cloner)
26
+ end
@@ -0,0 +1,35 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Clowne
4
+ module RSpec
5
+ module Helpers # :nodoc: all
6
+ attr_reader :cloner
7
+
8
+ def with_traits(*traits)
9
+ @traits = traits
10
+ self
11
+ end
12
+
13
+ def matches?(actual)
14
+ raise ArgumentError, non_cloner_message unless actual <= ::Clowne::Cloner
15
+
16
+ @cloner = actual
17
+ super
18
+ end
19
+
20
+ def plan
21
+ @plan ||=
22
+ if @traits.nil?
23
+ cloner.default_plan
24
+ else
25
+ cloner.plan_with_traits(@traits)
26
+ end
27
+ end
28
+
29
+ def non_cloner_message
30
+ "expected a cloner to be passed to `expect(...)`, " \
31
+ "but got #{actual_formatted}"
32
+ end
33
+ end
34
+ end
35
+ end
@@ -0,0 +1,26 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "clowne/ext/record_key"
4
+
5
+ module Clowne
6
+ module Utils
7
+ class CloneMapper # :nodoc: all
8
+ def initialize
9
+ @store = {}
10
+ end
11
+
12
+ def add(origin, clone)
13
+ @store[origin] ||= clone
14
+ end
15
+
16
+ def clone_of(origin)
17
+ @store[origin]
18
+ end
19
+
20
+ def origin_of(clone)
21
+ origin, _clone = @store.rassoc(clone)
22
+ origin
23
+ end
24
+ end
25
+ end
26
+ end
@@ -0,0 +1,95 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "clowne/utils/clone_mapper"
4
+
5
+ module Clowne
6
+ module Utils
7
+ class Operation # :nodoc: all
8
+ THREAD_KEY = :"#{name}.clowne_operation"
9
+ DEFAULT_MAPPER = Utils::CloneMapper
10
+
11
+ private_constant :THREAD_KEY
12
+
13
+ class << self
14
+ def current
15
+ Thread.current[THREAD_KEY]
16
+ end
17
+
18
+ def wrap(mapper: nil)
19
+ return yield if current
20
+
21
+ Thread.current[THREAD_KEY] = new(mapper || DEFAULT_MAPPER.new)
22
+
23
+ current.tap do |operation|
24
+ operation.clone = yield
25
+ clear!
26
+ end
27
+ end
28
+
29
+ def clear!
30
+ Thread.current[THREAD_KEY] = nil
31
+ end
32
+ end
33
+
34
+ attr_writer :clone
35
+ attr_reader :mapper
36
+
37
+ def initialize(mapper)
38
+ @after_clone_blocks = []
39
+ @after_persist_blocks = []
40
+ @mapper = mapper
41
+ end
42
+
43
+ def add_after_persist(block)
44
+ @after_persist_blocks.unshift(block)
45
+ end
46
+
47
+ def add_after_clone(block)
48
+ @after_clone_blocks.unshift(block)
49
+ end
50
+
51
+ def add_mapping(origin, clone)
52
+ @mapper.add(origin, clone)
53
+ end
54
+
55
+ def to_record
56
+ return @_record if defined?(@_record)
57
+
58
+ run_after_clone
59
+ @_record = @clone
60
+ end
61
+
62
+ def persist!
63
+ to_record.save!.tap do
64
+ run_after_persist
65
+ end
66
+ end
67
+
68
+ def persist
69
+ to_record.save.tap do |result|
70
+ next unless result
71
+
72
+ run_after_persist
73
+ end
74
+ end
75
+
76
+ def save
77
+ warn "[DEPRECATION] `save` is deprecated. Please use `persist` instead."
78
+ @clone.save
79
+ end
80
+
81
+ def save!
82
+ warn "[DEPRECATION] `save!` is deprecated. Please use `persist!` instead."
83
+ @clone.save!
84
+ end
85
+
86
+ def run_after_persist
87
+ @after_persist_blocks.each(&:call)
88
+ end
89
+
90
+ def run_after_clone
91
+ @after_clone_blocks.each(&:call)
92
+ end
93
+ end
94
+ end
95
+ end
@@ -0,0 +1,39 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Clowne
4
+ module Utils # :nodoc: all
5
+ class Options
6
+ INTERNAL_KEYS = %i[adapter traits clowne_only_actions mapping only].freeze
7
+
8
+ def initialize(options)
9
+ @options = options
10
+ end
11
+
12
+ def traits
13
+ @_traits ||= Array(options[:traits])
14
+ end
15
+
16
+ def only
17
+ options[:clowne_only_actions]
18
+ end
19
+
20
+ def mapper
21
+ options[:mapper]
22
+ end
23
+
24
+ def adapter
25
+ options[:adapter]
26
+ end
27
+
28
+ def params
29
+ options.dup.tap do |o|
30
+ INTERNAL_KEYS.each { |key| o.delete(key) }
31
+ end
32
+ end
33
+
34
+ private
35
+
36
+ attr_reader :options
37
+ end
38
+ end
39
+ end
@@ -0,0 +1,64 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "clowne/ext/lambda_as_proc"
4
+
5
+ module Clowne
6
+ module Utils
7
+ class Params # :nodoc: all
8
+ class BaseProxy
9
+ attr_reader :value
10
+
11
+ def initialize(value)
12
+ @value = value
13
+ end
14
+
15
+ def permit(_params)
16
+ raise NotImplementedError
17
+ end
18
+ end
19
+
20
+ class PassProxy < BaseProxy
21
+ def permit(params:, **)
22
+ params
23
+ end
24
+ end
25
+
26
+ class NullProxy < BaseProxy
27
+ def permit(_params)
28
+ {}
29
+ end
30
+ end
31
+
32
+ class BlockProxy < BaseProxy
33
+ using Clowne::Ext::LambdaAsProc
34
+
35
+ def permit(params:, parent:)
36
+ value.to_proc.call(params, parent)
37
+ end
38
+ end
39
+
40
+ class KeyProxy < BaseProxy
41
+ def permit(params:, **)
42
+ nested_params = params.fetch(value)
43
+ return nested_params if nested_params.is_a?(Hash)
44
+
45
+ raise KeyError, "value by key '#{value}' must be a Hash"
46
+ end
47
+ end
48
+
49
+ class << self
50
+ def proxy(value)
51
+ if value == true
52
+ PassProxy
53
+ elsif value.nil? || value == false
54
+ NullProxy
55
+ elsif value.is_a?(Proc)
56
+ BlockProxy
57
+ else
58
+ KeyProxy
59
+ end.new(value)
60
+ end
61
+ end
62
+ end
63
+ end
64
+ end
@@ -0,0 +1,90 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Clowne
4
+ module Utils
5
+ class Plan # :nodoc: all
6
+ class TwoPhaseSet
7
+ def initialize
8
+ @added = {}
9
+ @removed = []
10
+ end
11
+
12
+ def []=(k, v)
13
+ return if @removed.include?(k)
14
+
15
+ @added[k] = v
16
+ end
17
+
18
+ def delete(k)
19
+ return if @removed.include?(k)
20
+
21
+ @removed << k
22
+ @added.delete(k)
23
+ end
24
+
25
+ def values
26
+ @added.values
27
+ end
28
+ end
29
+
30
+ def initialize(registry)
31
+ @registry = registry
32
+ @data = {}
33
+ end
34
+
35
+ def add(type, declaration)
36
+ data[type] = [] unless data.key?(type)
37
+ data[type] << declaration
38
+ end
39
+
40
+ def add_to(type, id, declaration)
41
+ data[type] = TwoPhaseSet.new unless data.key?(type)
42
+ data[type][id] = declaration
43
+ end
44
+
45
+ def set(type, declaration)
46
+ data[type] = declaration
47
+ end
48
+
49
+ def get(type)
50
+ data[type]
51
+ end
52
+
53
+ def remove(type)
54
+ data.delete(type)
55
+ end
56
+
57
+ def remove_from(type, id)
58
+ return unless data[type]
59
+
60
+ data[type].delete(id)
61
+ end
62
+
63
+ def declarations(reload = false)
64
+ return @declarations if !reload && instance_variable_defined?(:@declarations)
65
+
66
+ @declarations =
67
+ registry.actions.flat_map do |type|
68
+ value = data[type]
69
+ next if value.nil?
70
+
71
+ value = value.values if value.is_a?(TwoPhaseSet)
72
+ value = Array(value)
73
+ value.map { |v| [type, v] }
74
+ end.compact
75
+ end
76
+
77
+ def dup
78
+ self.class.new(registry).tap do |duped|
79
+ data.each do |k, v|
80
+ duped.set(k, v.dup)
81
+ end
82
+ end
83
+ end
84
+
85
+ private
86
+
87
+ attr_reader :data, :registry
88
+ end
89
+ end
90
+ end