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.
- checksums.yaml +5 -5
- data/.codeclimate.yml +7 -0
- data/.gitattributes +1 -0
- data/.gitignore +1 -0
- data/.rubocop.yml +16 -33
- data/.travis.yml +14 -10
- data/CHANGELOG.md +39 -2
- data/Gemfile +11 -6
- data/README.md +48 -384
- data/Rakefile +3 -3
- data/clowne.gemspec +16 -8
- data/docs/.nojekyll +0 -0
- data/docs/.rubocop.yml +18 -0
- data/docs/CNAME +1 -0
- data/docs/README.md +131 -0
- data/docs/_sidebar.md +25 -0
- data/docs/active_record.md +33 -0
- data/docs/after_clone.md +53 -0
- data/docs/after_persist.md +77 -0
- data/docs/architecture.md +138 -0
- data/docs/assets/docsify.min.js +1 -0
- data/docs/assets/prism-ruby.min.js +1 -0
- data/docs/assets/styles.css +348 -0
- data/docs/assets/vue.css +1 -0
- data/docs/clone_mapper.md +59 -0
- data/docs/customization.md +63 -0
- data/docs/exclude_association.md +61 -0
- data/docs/finalize.md +31 -0
- data/docs/from_v02_to_v1.md +83 -0
- data/docs/getting_started.md +171 -0
- data/docs/implicit_cloner.md +33 -0
- data/docs/include_association.md +133 -0
- data/docs/index.html +29 -0
- data/docs/init_as.md +40 -0
- data/docs/inline_configuration.md +37 -0
- data/docs/nullify.md +33 -0
- data/docs/operation.md +55 -0
- data/docs/parameters.md +112 -0
- data/docs/sequel.md +50 -0
- data/docs/supported_adapters.md +10 -0
- data/docs/testing.md +194 -0
- data/docs/traits.md +25 -0
- data/gemfiles/activerecord42.gemfile +7 -4
- data/gemfiles/jruby.gemfile +8 -4
- data/gemfiles/railsmaster.gemfile +8 -5
- data/lib/clowne.rb +12 -7
- data/lib/clowne/adapters/active_record.rb +6 -16
- data/lib/clowne/adapters/active_record/associations.rb +8 -6
- data/lib/clowne/adapters/active_record/associations/base.rb +5 -49
- data/lib/clowne/adapters/active_record/associations/belongs_to.rb +29 -0
- data/lib/clowne/adapters/active_record/associations/has_one.rb +9 -1
- data/lib/clowne/adapters/active_record/associations/noop.rb +4 -1
- data/lib/clowne/adapters/active_record/dsl.rb +33 -0
- data/lib/clowne/adapters/active_record/resolvers/association.rb +38 -0
- data/lib/clowne/adapters/base.rb +53 -41
- data/lib/clowne/adapters/base/association.rb +78 -0
- data/lib/clowne/adapters/registry.rb +54 -11
- data/lib/clowne/adapters/sequel.rb +29 -0
- data/lib/clowne/adapters/sequel/associations.rb +26 -0
- data/lib/clowne/adapters/sequel/associations/base.rb +27 -0
- data/lib/clowne/adapters/sequel/associations/many_to_many.rb +23 -0
- data/lib/clowne/adapters/sequel/associations/noop.rb +16 -0
- data/lib/clowne/adapters/sequel/associations/one_to_many.rb +28 -0
- data/lib/clowne/adapters/sequel/associations/one_to_one.rb +28 -0
- data/lib/clowne/adapters/sequel/copier.rb +23 -0
- data/lib/clowne/adapters/sequel/operation.rb +35 -0
- data/lib/clowne/adapters/sequel/record_wrapper.rb +43 -0
- data/lib/clowne/adapters/sequel/resolvers/after_persist.rb +22 -0
- data/lib/clowne/adapters/sequel/resolvers/association.rb +51 -0
- data/lib/clowne/adapters/sequel/specifications/after_persist_does_not_support.rb +15 -0
- data/lib/clowne/cloner.rb +50 -20
- data/lib/clowne/declarations.rb +15 -11
- data/lib/clowne/declarations/after_clone.rb +21 -0
- data/lib/clowne/declarations/after_persist.rb +21 -0
- data/lib/clowne/declarations/base.rb +13 -0
- data/lib/clowne/declarations/exclude_association.rb +1 -6
- data/lib/clowne/declarations/finalize.rb +3 -2
- data/lib/clowne/declarations/include_association.rb +16 -4
- data/lib/clowne/declarations/init_as.rb +21 -0
- data/lib/clowne/declarations/nullify.rb +3 -2
- data/lib/clowne/declarations/trait.rb +3 -0
- data/lib/clowne/dsl.rb +9 -0
- data/lib/clowne/ext/lambda_as_proc.rb +17 -0
- data/lib/clowne/ext/orm_ext.rb +21 -0
- data/lib/clowne/ext/record_key.rb +12 -0
- data/lib/clowne/ext/string_constantize.rb +10 -4
- data/lib/clowne/ext/yield_self_then.rb +25 -0
- data/lib/clowne/planner.rb +27 -7
- data/lib/clowne/resolvers/after_clone.rb +17 -0
- data/lib/clowne/resolvers/after_persist.rb +18 -0
- data/lib/clowne/resolvers/finalize.rb +12 -0
- data/lib/clowne/resolvers/init_as.rb +13 -0
- data/lib/clowne/resolvers/nullify.rb +15 -0
- data/lib/clowne/rspec.rb +5 -0
- data/lib/clowne/rspec/clone_association.rb +99 -0
- data/lib/clowne/rspec/clone_associations.rb +26 -0
- data/lib/clowne/rspec/helpers.rb +35 -0
- data/lib/clowne/utils/clone_mapper.rb +26 -0
- data/lib/clowne/utils/operation.rb +95 -0
- data/lib/clowne/utils/options.rb +39 -0
- data/lib/clowne/utils/params.rb +64 -0
- data/lib/clowne/utils/plan.rb +90 -0
- data/lib/clowne/version.rb +1 -1
- metadata +140 -20
- data/lib/clowne/adapters/active_record/association.rb +0 -34
- data/lib/clowne/adapters/base/finalize.rb +0 -19
- data/lib/clowne/adapters/base/nullify.rb +0 -19
- data/lib/clowne/plan.rb +0 -81
data/lib/clowne/dsl.rb
CHANGED
@@ -5,10 +5,19 @@ module Clowne
|
|
5
5
|
def adapter(adapter = nil)
|
6
6
|
if adapter.nil?
|
7
7
|
return @_adapter if instance_variable_defined?(:@_adapter)
|
8
|
+
|
8
9
|
@_adapter = Clowne.default_adapter
|
9
10
|
else
|
10
11
|
@_adapter = Clowne.resolve_adapter(adapter)
|
11
12
|
end
|
12
13
|
end
|
14
|
+
|
15
|
+
private
|
16
|
+
|
17
|
+
def current_adapter(user_adapter)
|
18
|
+
return adapter if user_adapter.nil?
|
19
|
+
|
20
|
+
Clowne.resolve_adapter(user_adapter)
|
21
|
+
end
|
13
22
|
end
|
14
23
|
end
|
@@ -0,0 +1,17 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module Clowne
|
4
|
+
module Ext
|
5
|
+
# Add to_proc method for lambda
|
6
|
+
module LambdaAsProc
|
7
|
+
refine Proc do
|
8
|
+
def to_proc
|
9
|
+
return self unless lambda?
|
10
|
+
|
11
|
+
this = self
|
12
|
+
proc { |*args| this.call(*args.take(this.arity)) }
|
13
|
+
end
|
14
|
+
end
|
15
|
+
end
|
16
|
+
end
|
17
|
+
end
|
@@ -0,0 +1,21 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require "clowne/ext/string_constantize"
|
4
|
+
|
5
|
+
module Clowne
|
6
|
+
module Ext
|
7
|
+
# Adds #cloner_class method to ORM base model
|
8
|
+
module ORMExt
|
9
|
+
using Clowne::Ext::StringConstantize
|
10
|
+
|
11
|
+
def cloner_class
|
12
|
+
return @_clowne_cloner if instance_variable_defined?(:@_clowne_cloner)
|
13
|
+
|
14
|
+
cloner = "#{name}Cloner".constantize
|
15
|
+
return @_clowne_cloner = cloner if cloner && cloner <= Clowne::Cloner
|
16
|
+
|
17
|
+
@_clowne_cloner = superclass.cloner_class if superclass.respond_to?(:cloner_class)
|
18
|
+
end
|
19
|
+
end
|
20
|
+
end
|
21
|
+
end
|
@@ -6,15 +6,21 @@ module Clowne
|
|
6
6
|
module StringConstantize
|
7
7
|
refine String do
|
8
8
|
def constantize
|
9
|
-
names = split(
|
9
|
+
names = split("::")
|
10
10
|
|
11
|
-
|
11
|
+
return nil if names.empty?
|
12
12
|
|
13
13
|
# Remove the first blank element in case of '::ClassName' notation.
|
14
14
|
names.shift if names.size > 1 && names.first.empty?
|
15
15
|
|
16
|
-
|
17
|
-
|
16
|
+
begin
|
17
|
+
names.inject(Object) do |constant, name|
|
18
|
+
constant.const_get(name)
|
19
|
+
end
|
20
|
+
# rescue instead of const_defined? allow us to use
|
21
|
+
# Rails const autoloading (aka patched const_get)
|
22
|
+
rescue NameError
|
23
|
+
nil
|
18
24
|
end
|
19
25
|
end
|
20
26
|
end
|
@@ -0,0 +1,25 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module Clowne
|
4
|
+
module Ext # :nodoc: all
|
5
|
+
# Add yield_self and then if missing
|
6
|
+
module YieldSelfThen
|
7
|
+
module Ext
|
8
|
+
unless nil.respond_to?(:yield_self)
|
9
|
+
def yield_self
|
10
|
+
yield self
|
11
|
+
end
|
12
|
+
end
|
13
|
+
|
14
|
+
alias then yield_self
|
15
|
+
end
|
16
|
+
|
17
|
+
# See https://github.com/jruby/jruby/issues/5220
|
18
|
+
::Object.include(Ext) if RUBY_PLATFORM.match?(/java/i)
|
19
|
+
|
20
|
+
refine Object do
|
21
|
+
include Ext
|
22
|
+
end
|
23
|
+
end
|
24
|
+
end
|
25
|
+
end
|
data/lib/clowne/planner.rb
CHANGED
@@ -1,30 +1,50 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
|
-
require
|
3
|
+
require "clowne/utils/plan"
|
4
4
|
|
5
5
|
module Clowne
|
6
6
|
class Planner # :nodoc: all
|
7
7
|
class << self
|
8
|
-
#
|
9
|
-
|
10
|
-
# +init_plan+:: Init plan
|
11
|
-
# +traits+:: List of traits if any
|
12
|
-
def compile(cloner, traits: nil)
|
8
|
+
# Compile plan for cloner with traits
|
9
|
+
def compile(adapter, cloner, traits: nil)
|
13
10
|
declarations = cloner.declarations.dup
|
14
11
|
|
15
12
|
declarations += compile_traits(cloner, traits) unless traits.nil?
|
16
13
|
|
17
|
-
declarations.each_with_object(
|
14
|
+
declarations.each_with_object(
|
15
|
+
Utils::Plan.new(adapter.registry)
|
16
|
+
) do |declaration, plan|
|
18
17
|
declaration.compile(plan)
|
19
18
|
end
|
20
19
|
end
|
21
20
|
|
21
|
+
# Extend previously compiled plan with an arbitrary block
|
22
|
+
# NOTE: It doesn't modify the plan itself but return a copy
|
23
|
+
def enhance(plan, block)
|
24
|
+
trait = Clowne::Declarations::Trait.new.tap { |t| t.extend_with(block) }
|
25
|
+
|
26
|
+
trait.compiled.each_with_object(plan.dup) do |declaration, new_plan|
|
27
|
+
declaration.compile(new_plan)
|
28
|
+
end
|
29
|
+
end
|
30
|
+
|
31
|
+
def filter_declarations(plan, only)
|
32
|
+
return plan if only.nil?
|
33
|
+
|
34
|
+
plan.dup.tap do |new_plan|
|
35
|
+
new_plan.declarations.reject! do |(type, declaration)|
|
36
|
+
!only.key?(type) || !declaration.matches?(only[type])
|
37
|
+
end
|
38
|
+
end
|
39
|
+
end
|
40
|
+
|
22
41
|
private
|
23
42
|
|
24
43
|
def compile_traits(cloner, traits)
|
25
44
|
traits.map do |id|
|
26
45
|
trait = cloner.traits[id]
|
27
46
|
raise ConfigurationError, "Trait not found: #{id}" if trait.nil?
|
47
|
+
|
28
48
|
trait.compiled
|
29
49
|
end.flatten
|
30
50
|
end
|
@@ -0,0 +1,17 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module Clowne
|
4
|
+
class Resolvers
|
5
|
+
module AfterClone # :nodoc: all
|
6
|
+
def self.call(source, record, declaration, params:, **_options)
|
7
|
+
operation = Clowne::Utils::Operation.current
|
8
|
+
operation.add_after_clone(
|
9
|
+
proc do
|
10
|
+
declaration.block.call(source, record, params)
|
11
|
+
end
|
12
|
+
)
|
13
|
+
record
|
14
|
+
end
|
15
|
+
end
|
16
|
+
end
|
17
|
+
end
|
@@ -0,0 +1,18 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module Clowne
|
4
|
+
class Resolvers
|
5
|
+
module AfterPersist # :nodoc: all
|
6
|
+
def self.call(source, record, declaration, params:, **_options)
|
7
|
+
operation = Clowne::Utils::Operation.current
|
8
|
+
params ||= {}
|
9
|
+
operation.add_after_persist(
|
10
|
+
proc do
|
11
|
+
declaration.block.call(source, record, params.merge(mapper: operation.mapper))
|
12
|
+
end
|
13
|
+
)
|
14
|
+
record
|
15
|
+
end
|
16
|
+
end
|
17
|
+
end
|
18
|
+
end
|
@@ -0,0 +1,13 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module Clowne
|
4
|
+
class Resolvers
|
5
|
+
module InitAs # :nodoc: all
|
6
|
+
# rubocop: disable Metrics/ParameterLists
|
7
|
+
def self.call(source, _record, declaration, params:, adapter:, **_options)
|
8
|
+
adapter.init_record(declaration.block.call(source, **params))
|
9
|
+
end
|
10
|
+
# rubocop: enable Metrics/ParameterLists
|
11
|
+
end
|
12
|
+
end
|
13
|
+
end
|
@@ -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
|
data/lib/clowne/rspec.rb
ADDED
@@ -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
|