clowne 0.1.0 → 1.3.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +5 -5
- data/.rubocop.yml +11 -45
- data/.travis.yml +14 -21
- data/CHANGELOG.md +30 -0
- data/Gemfile +11 -7
- data/README.md +38 -18
- data/Rakefile +3 -3
- data/clowne.gemspec +17 -10
- data/docs/.nojekyll +0 -0
- data/docs/.rubocop.yml +8 -2
- data/docs/CNAME +1 -0
- data/docs/README.md +131 -0
- data/docs/_sidebar.md +25 -0
- data/docs/active_record.md +4 -7
- data/docs/after_clone.md +53 -0
- data/docs/after_persist.md +77 -0
- data/docs/architecture.md +2 -5
- 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 +6 -7
- data/docs/exclude_association.md +6 -8
- data/docs/finalize.md +11 -15
- data/docs/from_v02_to_v1.md +83 -0
- data/docs/getting_started.md +171 -0
- data/docs/implicit_cloner.md +2 -5
- data/docs/include_association.md +24 -10
- data/docs/index.html +29 -0
- data/docs/init_as.md +13 -9
- data/docs/inline_configuration.md +5 -6
- data/docs/nullify.md +3 -7
- data/docs/operation.md +55 -0
- data/docs/parameters.md +112 -0
- data/docs/sequel.md +16 -22
- data/docs/supported_adapters.md +3 -6
- data/docs/testing.md +194 -0
- data/docs/traits.md +1 -4
- data/gemfiles/activerecord42.gemfile +7 -5
- data/gemfiles/jruby.gemfile +8 -6
- data/gemfiles/railsmaster.gemfile +8 -6
- data/lib/clowne.rb +12 -9
- data/lib/clowne/adapters/active_record.rb +4 -5
- data/lib/clowne/adapters/active_record/associations.rb +8 -6
- data/lib/clowne/adapters/active_record/associations/base.rb +1 -5
- data/lib/clowne/adapters/active_record/associations/belongs_to.rb +29 -0
- data/lib/clowne/adapters/active_record/associations/has_one.rb +3 -2
- data/lib/clowne/adapters/active_record/dsl.rb +2 -2
- data/lib/clowne/adapters/active_record/resolvers/association.rb +38 -0
- data/lib/clowne/adapters/base.rb +49 -44
- data/lib/clowne/adapters/base/association.rb +24 -15
- data/lib/clowne/adapters/registry.rb +49 -0
- data/lib/clowne/adapters/sequel.rb +14 -10
- data/lib/clowne/adapters/sequel/associations.rb +6 -6
- data/lib/clowne/adapters/sequel/associations/base.rb +9 -5
- data/lib/clowne/adapters/sequel/associations/many_to_many.rb +6 -2
- data/lib/clowne/adapters/sequel/associations/one_to_many.rb +7 -2
- data/lib/clowne/adapters/sequel/associations/one_to_one.rb +7 -2
- data/lib/clowne/adapters/sequel/operation.rb +35 -0
- data/lib/clowne/adapters/sequel/record_wrapper.rb +0 -16
- 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 +49 -21
- data/lib/clowne/declarations.rb +15 -12
- 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 +2 -2
- data/lib/clowne/declarations/finalize.rb +5 -4
- data/lib/clowne/declarations/include_association.rb +16 -2
- data/lib/clowne/declarations/init_as.rb +5 -4
- data/lib/clowne/declarations/nullify.rb +4 -3
- data/lib/clowne/declarations/trait.rb +1 -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 +1 -1
- data/lib/clowne/ext/record_key.rb +12 -0
- data/lib/clowne/ext/string_constantize.rb +9 -3
- data/lib/clowne/ext/yield_self_then.rb +25 -0
- data/lib/clowne/planner.rb +16 -3
- 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 +99 -45
- data/docs/alternatives.md +0 -26
- data/docs/basic_example.md +0 -66
- data/docs/configuration.md +0 -29
- data/docs/execution_order.md +0 -14
- data/docs/installation.md +0 -16
- data/docs/web/.gitignore +0 -11
- data/docs/web/core/Footer.js +0 -92
- data/docs/web/i18n/en.json +0 -134
- data/docs/web/package.json +0 -14
- data/docs/web/pages/en/help.js +0 -50
- data/docs/web/pages/en/index.js +0 -231
- data/docs/web/pages/en/users.js +0 -47
- data/docs/web/sidebars.json +0 -30
- data/docs/web/siteConfig.js +0 -44
- data/docs/web/static/css/custom.css +0 -229
- data/docs/web/static/fonts/FiraCode-Medium.woff +0 -0
- data/docs/web/static/fonts/FiraCode-Regular.woff +0 -0
- data/docs/web/static/fonts/StemText.woff +0 -0
- data/docs/web/static/fonts/StemTextBold.woff +0 -0
- data/docs/web/static/img/favicon/favicon.ico +0 -0
- data/docs/web/yarn.lock +0 -1741
- data/lib/clowne/adapters/active_record/association.rb +0 -34
- data/lib/clowne/adapters/base/finalize.rb +0 -19
- data/lib/clowne/adapters/base/init_as.rb +0 -21
- data/lib/clowne/adapters/base/nullify.rb +0 -19
- data/lib/clowne/adapters/sequel/association.rb +0 -47
- data/lib/clowne/plan.rb +0 -81
data/lib/clowne/declarations.rb
CHANGED
@@ -1,18 +1,18 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
|
-
require
|
4
|
-
require
|
3
|
+
require "clowne/dsl"
|
4
|
+
require "clowne/utils/plan"
|
5
5
|
|
6
6
|
module Clowne
|
7
7
|
module Declarations # :nodoc:
|
8
8
|
module_function
|
9
9
|
|
10
|
-
def add(id, declaration = nil)
|
11
|
-
declaration =
|
10
|
+
def add(id, declaration = nil, &block)
|
11
|
+
declaration = block if block_given?
|
12
12
|
|
13
13
|
if declaration.is_a?(Class)
|
14
|
-
DSL.send(:define_method, id) do |*args, &
|
15
|
-
declarations.push declaration.new(*args, &
|
14
|
+
DSL.send(:define_method, id) do |*args, **hargs, &inner_block|
|
15
|
+
declarations.push declaration.new(*args, **hargs, &inner_block)
|
16
16
|
end
|
17
17
|
elsif declaration.is_a?(Proc)
|
18
18
|
DSL.send(:define_method, id, &declaration)
|
@@ -23,9 +23,12 @@ module Clowne
|
|
23
23
|
end
|
24
24
|
end
|
25
25
|
|
26
|
-
require
|
27
|
-
require
|
28
|
-
require
|
29
|
-
require
|
30
|
-
require
|
31
|
-
require
|
26
|
+
require "clowne/declarations/base"
|
27
|
+
require "clowne/declarations/init_as"
|
28
|
+
require "clowne/declarations/exclude_association"
|
29
|
+
require "clowne/declarations/finalize"
|
30
|
+
require "clowne/declarations/include_association"
|
31
|
+
require "clowne/declarations/nullify"
|
32
|
+
require "clowne/declarations/trait"
|
33
|
+
require "clowne/declarations/after_persist"
|
34
|
+
require "clowne/declarations/after_clone"
|
@@ -0,0 +1,21 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module Clowne
|
4
|
+
module Declarations
|
5
|
+
class AfterClone < Base # :nodoc: all
|
6
|
+
attr_reader :block
|
7
|
+
|
8
|
+
def initialize(*, &block)
|
9
|
+
raise ArgumentError, "Block is required for after_clone" unless block
|
10
|
+
|
11
|
+
@block = block
|
12
|
+
end
|
13
|
+
|
14
|
+
def compile(plan)
|
15
|
+
plan.add(:after_clone, self)
|
16
|
+
end
|
17
|
+
end
|
18
|
+
end
|
19
|
+
end
|
20
|
+
|
21
|
+
Clowne::Declarations.add :after_clone, Clowne::Declarations::AfterClone
|
@@ -0,0 +1,21 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module Clowne
|
4
|
+
module Declarations
|
5
|
+
class AfterPersist < Base # :nodoc: all
|
6
|
+
attr_reader :block
|
7
|
+
|
8
|
+
def initialize(*, &block)
|
9
|
+
raise ArgumentError, "Block is required for after_persist" unless block
|
10
|
+
|
11
|
+
@block = block
|
12
|
+
end
|
13
|
+
|
14
|
+
def compile(plan)
|
15
|
+
plan.add(:after_persist, self)
|
16
|
+
end
|
17
|
+
end
|
18
|
+
end
|
19
|
+
end
|
20
|
+
|
21
|
+
Clowne::Declarations.add :after_persist, Clowne::Declarations::AfterPersist
|
@@ -2,12 +2,13 @@
|
|
2
2
|
|
3
3
|
module Clowne
|
4
4
|
module Declarations
|
5
|
-
class Finalize # :nodoc: all
|
5
|
+
class Finalize < Base # :nodoc: all
|
6
6
|
attr_reader :block
|
7
7
|
|
8
|
-
def initialize
|
9
|
-
raise ArgumentError,
|
10
|
-
|
8
|
+
def initialize(*, &block)
|
9
|
+
raise ArgumentError, "Block is required for finalize" unless block
|
10
|
+
|
11
|
+
@block = block
|
11
12
|
end
|
12
13
|
|
13
14
|
def compile(plan)
|
@@ -1,10 +1,10 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
|
-
require
|
3
|
+
require "clowne/ext/string_constantize"
|
4
4
|
|
5
5
|
module Clowne
|
6
6
|
module Declarations
|
7
|
-
class IncludeAssociation # :nodoc: all
|
7
|
+
class IncludeAssociation < Base # :nodoc: all
|
8
8
|
using Clowne::Ext::StringConstantize
|
9
9
|
|
10
10
|
attr_accessor :name, :scope, :options
|
@@ -19,8 +19,22 @@ module Clowne
|
|
19
19
|
plan.add_to(:association, name, self)
|
20
20
|
end
|
21
21
|
|
22
|
+
def matches?(names)
|
23
|
+
names = Array(names)
|
24
|
+
names.include?(name)
|
25
|
+
end
|
26
|
+
|
27
|
+
def params_proxy
|
28
|
+
@_params_proxy ||= Clowne::Utils::Params.proxy(options[:params])
|
29
|
+
end
|
30
|
+
|
31
|
+
def params
|
32
|
+
options[:params]
|
33
|
+
end
|
34
|
+
|
22
35
|
def clone_with
|
23
36
|
return @clone_with if instance_variable_defined?(:@clone_with)
|
37
|
+
|
24
38
|
@clone_with =
|
25
39
|
case options[:clone_with]
|
26
40
|
when String, Symbol
|
@@ -2,12 +2,13 @@
|
|
2
2
|
|
3
3
|
module Clowne
|
4
4
|
module Declarations
|
5
|
-
class InitAs # :nodoc: all
|
5
|
+
class InitAs < Base # :nodoc: all
|
6
6
|
attr_reader :block
|
7
7
|
|
8
|
-
def initialize
|
9
|
-
raise ArgumentError,
|
10
|
-
|
8
|
+
def initialize(*, &block)
|
9
|
+
raise ArgumentError, "Block is required for init_as" unless block
|
10
|
+
|
11
|
+
@block = block
|
11
12
|
end
|
12
13
|
|
13
14
|
def compile(plan)
|
@@ -2,11 +2,12 @@
|
|
2
2
|
|
3
3
|
module Clowne
|
4
4
|
module Declarations
|
5
|
-
class Nullify # :nodoc: all
|
5
|
+
class Nullify < Base # :nodoc: all
|
6
6
|
attr_reader :attributes
|
7
7
|
|
8
|
-
def initialize(*attributes)
|
9
|
-
raise ArgumentError,
|
8
|
+
def initialize(*attributes, **)
|
9
|
+
raise ArgumentError, "At least one attribute required" if attributes.empty?
|
10
|
+
|
10
11
|
@attributes = attributes
|
11
12
|
end
|
12
13
|
|
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
|
data/lib/clowne/ext/orm_ext.rb
CHANGED
@@ -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,17 +1,19 @@
|
|
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
8
|
# Compile plan for cloner with traits
|
9
|
-
def compile(cloner, traits: nil)
|
9
|
+
def compile(adapter, cloner, traits: nil)
|
10
10
|
declarations = cloner.declarations.dup
|
11
11
|
|
12
12
|
declarations += compile_traits(cloner, traits) unless traits.nil?
|
13
13
|
|
14
|
-
declarations.each_with_object(
|
14
|
+
declarations.each_with_object(
|
15
|
+
Utils::Plan.new(adapter.registry)
|
16
|
+
) do |declaration, plan|
|
15
17
|
declaration.compile(plan)
|
16
18
|
end
|
17
19
|
end
|
@@ -26,12 +28,23 @@ module Clowne
|
|
26
28
|
end
|
27
29
|
end
|
28
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
|
+
|
29
41
|
private
|
30
42
|
|
31
43
|
def compile_traits(cloner, traits)
|
32
44
|
traits.map do |id|
|
33
45
|
trait = cloner.traits[id]
|
34
46
|
raise ConfigurationError, "Trait not found: #{id}" if trait.nil?
|
47
|
+
|
35
48
|
trait.compiled
|
36
49
|
end.flatten
|
37
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
|