realm-core 0.7.1 → 0.7.6

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: b7384c47348b48ded26a37ce232e0ccb357a8a6e7e1f25667d614f9dc37a8af2
4
- data.tar.gz: 0b4ae6a517da7377f458111447453423652b35166fadbb60df74337727d68068
3
+ metadata.gz: bbac3dd7cbfca4bf017df5b055b50655c997b124ca5dc6d460b673cab02e1b2c
4
+ data.tar.gz: 60e1823f9b8ecd9665e6622e0c17002e24cc633c7c9ffc5c38958a06a45d597f
5
5
  SHA512:
6
- metadata.gz: 4aa7ec7d85ce6777b58d13f6bf3ff691e83901124951f7ebb38b60c2fbf929bf58b6daec23631f8308147be8614f7ed82edce045855620de9ba6b05abab424b0
7
- data.tar.gz: ca2605753e57f0290a2b834f455389dfcc57a0ad51515b7fdc0b75c4daf2a166d32c9039aea45ce09a9995d3807beb0ac8ae97b38c2b9682658e6d28f0237cb1
6
+ metadata.gz: 9e1eda055d9e1d5c1f64b908e7cbac32602945a3697afcd01eca583dd89e733c6506757671fa3a05045d2d6a3ae06fb73cfd04ada9e2022bf7c0038e4196b7aa
7
+ data.tar.gz: 4bd655af9bf12ccaf82f98a41fd1953a8f396422a175a8fb9a00c8ff30f42b089ad3390d761a2249d7a28e812ac7f187e8c80453f66820f26c8bcd08af3aad16
data/lib/realm-core.rb CHANGED
@@ -2,5 +2,10 @@
2
2
  # frozen_string_literal: true
3
3
 
4
4
  require 'realm'
5
+ require 'zeitwerk'
6
+
7
+ loader = Zeitwerk::Loader.for_gem
8
+ loader.ignore(__FILE__)
9
+ loader.setup
5
10
 
6
11
  # rubocop:enable Naming/FileName
data/lib/realm.rb CHANGED
@@ -1,8 +1,7 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- Dir[File.join(File.dirname(__FILE__), 'realm', '**', '*.rb')].sort.each do |f|
4
- require f
5
- end
3
+ require 'active_support/all'
4
+ require 'dry/core/constants'
6
5
 
7
6
  module Realm
8
7
  class << self
@@ -18,5 +17,13 @@ module Realm
18
17
  root_module.define_singleton_method(:realm) { builder.runtime }
19
18
  end
20
19
  end
20
+
21
+ # port the construction method from Dry::Struct as it's not inherited
22
+ def Struct(attributes = Dry::Core::Constants::EMPTY_HASH, &block) # rubocop:disable Naming/MethodName
23
+ Class.new(Struct) do
24
+ attributes.each { |a, type| attribute a, type }
25
+ module_eval(&block) if block
26
+ end
27
+ end
21
28
  end
22
29
  end
@@ -1,12 +1,6 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  require 'dry-validation'
4
- require 'active_support/core_ext/module/delegation'
5
- require 'realm/error'
6
- require 'realm/mixins/context_injection'
7
- require 'realm/mixins/repository_helper'
8
- require 'realm/mixins/aggregate_member'
9
- require_relative 'action_handler/result'
10
4
 
11
5
  module Realm
12
6
  class ActionHandler
@@ -28,21 +22,25 @@ module Realm
28
22
  end
29
23
 
30
24
  def contract(&block)
31
- @method_contract = Class.new(Dry::Validation::Contract, &block).new
25
+ @method_contract = Class.new(Realm::Contract, &block).new
32
26
  end
33
27
 
34
- def contract_schema(&block)
35
- contract { schema(&block) }
28
+ def contract_schema(...)
29
+ contract { schema(...) }
36
30
  end
37
31
 
38
- def contract_params(&block)
39
- contract { params(&block) }
32
+ def contract_params(...)
33
+ contract { params(...) }
40
34
  end
41
35
 
42
- def contract_json(&block)
43
- contract { json(&block) }
36
+ def contract_json(...)
37
+ contract { json(...) }
44
38
  end
45
39
 
40
+ alias schema_contract contract_schema
41
+ alias params_contract contract_params
42
+ alias json_contract contract_json
43
+
46
44
  def method_added(method_name)
47
45
  super
48
46
  return unless defined?(@method_contract)
data/lib/realm/builder.rb CHANGED
@@ -1,14 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- require 'active_support/core_ext/string'
4
- require 'realm/container'
5
- require 'realm/runtime'
6
- require 'realm/domain_resolver'
7
- require 'realm/persistence'
8
- require 'realm/dispatcher'
9
- require 'realm/event_router'
10
- require 'realm/plugin'
11
-
12
3
  module Realm
13
4
  class Builder
14
5
  def self.setup(config)
@@ -1,10 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- require 'active_support/core_ext/module/delegation'
4
- require 'active_support/core_ext/string'
5
- require 'realm/action_handler'
6
- require 'realm/mixins/reactive'
7
-
8
3
  module Realm
9
4
  class CommandHandler < Realm::ActionHandler
10
5
  include Mixins::Reactive
data/lib/realm/config.rb CHANGED
@@ -1,6 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- require 'active_support/core_ext/string'
4
3
  require 'dry-initializer'
5
4
 
6
5
  module Realm
@@ -1,9 +1,6 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  require 'dry-container'
4
- require 'active_support/core_ext/string'
5
- require 'active_support/core_ext/object/try'
6
- require 'realm/error'
7
4
 
8
5
  module Realm
9
6
  class Container
data/lib/realm/context.rb CHANGED
@@ -1,7 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- require 'realm/container'
4
-
5
3
  module Realm
6
4
  class Context
7
5
  include Enumerable
@@ -0,0 +1,42 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'dry-validation'
4
+
5
+ module Realm
6
+ class Contract < Dry::Validation::Contract
7
+ class NotConvertibleToSchema < Realm::Error
8
+ def initialize(thing)
9
+ super("Not convertible to schema: #{thing}")
10
+ end
11
+ end
12
+
13
+ class << self
14
+ def schema(*external_schemas, **attributes, &block)
15
+ super(*sanitize_schemas(external_schemas, attributes, :schema), &block)
16
+ end
17
+
18
+ def params(*external_schemas, **attributes, &block)
19
+ super(*sanitize_schemas(external_schemas, attributes, :params), &block)
20
+ end
21
+
22
+ def json(*external_schemas, **attributes, &block)
23
+ super(*sanitize_schemas(external_schemas, attributes, :json), &block)
24
+ end
25
+
26
+ private
27
+
28
+ def sanitize_schemas(things, attributes, type)
29
+ things << Realm.Struct(attributes) if attributes.present?
30
+ things.map { |thing| convert_to_schema(thing, type) }
31
+ end
32
+
33
+ def convert_to_schema(thing, type)
34
+ return thing if thing.is_a? Dry::Schema::Processor # already a schema
35
+
36
+ raise NotConvertibleToSchema, thing unless thing.respond_to?(:to_dry_schema)
37
+
38
+ thing.to_dry_schema(type: type)
39
+ end
40
+ end
41
+ end
42
+ end
@@ -1,7 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- require 'active_support/core_ext/string'
4
-
5
3
  module Realm
6
4
  class Dependency
7
5
  attr_reader :dependable, :name
@@ -1,13 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- require 'active_support/core_ext/string'
4
- require 'realm/query_handler'
5
- require 'realm/command_handler'
6
- require 'realm/domain_resolver'
7
- require 'realm/error'
8
- require 'realm/mixins/dependency_injection'
9
- require 'realm/persistence/repository_query_handler_adapter'
10
-
11
3
  module Realm
12
4
  class Dispatcher
13
5
  include Mixins::DependencyInjection
@@ -1,11 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- require 'active_support/core_ext/string'
4
- require 'realm/command_handler'
5
- require 'realm/query_handler'
6
- require 'realm/event_handler'
7
- require 'realm/event'
8
-
9
3
  module Realm
10
4
  class DomainResolver
11
5
  DOMAIN_CLASS_TYPES = [CommandHandler, QueryHandler, EventHandler].freeze
@@ -21,7 +15,7 @@ module Realm
21
15
  handlers = @index[type]
22
16
  return [handlers[identifier], :handle] if handlers.key?(identifier)
23
17
 
24
- # The last part of the identifier can action method name inside the handler
18
+ # The last part of the identifier can be action method name inside the handler
25
19
  parts = identifier.split('.')
26
20
  handler_part = parts[..-2].join('.')
27
21
  action = parts[-1]
data/lib/realm/event.rb CHANGED
@@ -1,10 +1,8 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  require 'securerandom'
4
- require 'active_support/core_ext/hash'
5
- require 'active_support/core_ext/string'
4
+ require 'dry/core/constants'
6
5
  require 'dry-struct'
7
- require 'realm/types'
8
6
 
9
7
  module Realm
10
8
  class Event < Dry::Struct
@@ -34,14 +32,33 @@ module Realm
34
32
  super({ head: head }.merge(body.empty? ? {} : { body: body }))
35
33
  end
36
34
 
37
- def type
35
+ def type(value = :not_provided)
36
+ @type = value unless value == :not_provided
38
37
  @type ||= name.demodulize.sub('Event', '').underscore
39
38
  end
40
39
 
40
+ def flatten_attributes_meta
41
+ @flatten_attributes_meta ||= collect_attributes_meta(schema.key(:body).type)
42
+ end
43
+
41
44
  protected
42
45
 
43
- def body_struct(&block)
44
- attribute(:body, &block)
46
+ def body_struct(type = Dry::Core::Constants::Undefined, &block)
47
+ attribute(:body, type, &block)
48
+ end
49
+
50
+ private
51
+
52
+ def collect_attributes_meta(thing, path = []) # rubocop:disable Metrics/AbcSize
53
+ if thing.respond_to?(:schema) && thing.constructor_type != Dry::Types::Hash::Constructor # struct
54
+ thing.schema.keys.reduce({}) do |memo, key|
55
+ memo.merge(collect_attributes_meta(key.type, path + [key.name]))
56
+ end
57
+ elsif thing.constructor_type == Dry::Types::Array::Constructor # array
58
+ collect_attributes_meta(thing.type.member, path + [:[]])
59
+ else
60
+ thing.meta.present? ? { path => thing.meta } : {}
61
+ end
45
62
  end
46
63
  end
47
64
 
@@ -1,8 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- require 'realm/error'
4
- require 'realm/event'
5
-
6
3
  module Realm
7
4
  class EventFactory
8
5
  def initialize(events_module)
@@ -31,11 +28,10 @@ module Realm
31
28
  root_module_str = root_module.to_s
32
29
  root_module.constants.each_with_object({}) do |const_sym, all|
33
30
  const = root_module.const_get(const_sym)
34
- if !(const < Event) && const.to_s.start_with?(root_module_str)
35
- all.merge!(collect_event_classes(const))
36
- elsif const < Event
37
- all[const.type] = const
38
- end
31
+ next unless const.is_a?(Module) && const.to_s.start_with?(root_module_str)
32
+
33
+ all[const.type] = const if const < Event
34
+ all.merge!(collect_event_classes(const))
39
35
  end
40
36
  end
41
37
 
@@ -1,12 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- require 'active_support/core_ext/string'
4
- require 'realm/persistence'
5
- require 'realm/mixins/context_injection'
6
- require 'realm/mixins/reactive'
7
- require 'realm/mixins/repository_helper'
8
- require 'realm/mixins/aggregate_member'
9
-
10
3
  module Realm
11
4
  class EventHandler
12
5
  extend Mixins::ContextInjection::ClassMethods
@@ -38,12 +31,11 @@ module Realm
38
31
  new(runtime: runtime).(event)
39
32
  end
40
33
 
41
- def identifier(value = :undefined)
42
- if value == :undefined
43
- defined?(@identifier) ? @identifier : name.gsub(/(Domain|(Event)?Handlers?)/, '').underscore.gsub(%r{/+}, '-')
44
- else
45
- @identifier = value
46
- end
34
+ def identifier(value = :not_provided)
35
+ @identifier = value unless value == :not_provided
36
+ return @identifier if defined?(@identifier)
37
+
38
+ @identifier = name.gsub(/(Domain|(::)?(Event)?Handlers?)/, '').underscore.gsub(%r{/+}, '-')
47
39
  end
48
40
 
49
41
  def event_types
@@ -1,14 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- require 'active_support/core_ext/string'
4
- require 'active_support/core_ext/hash'
5
- require 'realm/error'
6
- require 'realm/domain_resolver'
7
- require 'realm/event_handler'
8
- require 'realm/event_factory'
9
- require 'realm/mixins/dependency_injection'
10
- require_relative 'event_router/internal_loop_gateway'
11
-
12
3
  module Realm
13
4
  class EventRouter
14
5
  include Mixins::DependencyInjection
@@ -1,7 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- require_relative './gateway'
4
-
5
3
  module Realm
6
4
  class EventRouter
7
5
  class InternalLoopGateway < Gateway
@@ -1,7 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- require 'active_support/core_ext/object/blank'
4
-
5
3
  module Realm
6
4
  class HealthStatus
7
5
  CODES = %i[green yellow red].freeze
@@ -1,7 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- require 'realm/error'
4
-
5
3
  module Realm
6
4
  module Mixins
7
5
  module ContextInjection
@@ -1,8 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- require 'active_support/core_ext/module/introspection'
4
- require 'active_support/core_ext/class/attribute'
5
-
6
3
  module Realm
7
4
  module Mixins
8
5
  module Controller
@@ -1,7 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- require 'realm/dependency'
4
-
5
3
  module Realm
6
4
  module Mixins
7
5
  module DependencyInjection
@@ -1,7 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- require 'realm/error'
4
-
5
3
  module Realm
6
4
  module Mixins
7
5
  module Reactive
@@ -1,7 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- require 'realm/error'
4
-
5
3
  module Realm
6
4
  module Mixins
7
5
  module RepositoryHelper
@@ -1,8 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- require 'active_support/core_ext/string'
4
- require 'realm/error'
5
-
6
3
  module Realm
7
4
  class Persistence
8
5
  class InvalidPersistanceType < Realm::Error; end
@@ -1,8 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- require 'realm/persistence'
4
- require 'realm/error'
5
-
6
3
  module Realm
7
4
  class Persistence
8
5
  class QueryCannotModifyState < Realm::Error; end
data/lib/realm/plugin.rb CHANGED
@@ -1,8 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- require 'active_support/core_ext/class'
4
- require 'active_support/core_ext/string'
5
-
6
3
  module Realm
7
4
  class Plugin
8
5
  class << self
@@ -1,7 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- require 'realm/action_handler'
4
-
5
3
  module Realm
6
4
  class QueryHandler < Realm::ActionHandler
7
5
  end
data/lib/realm/runtime.rb CHANGED
@@ -1,15 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- require 'active_support/core_ext/module/delegation'
4
- require 'active_support/core_ext/object/try'
5
- require 'realm/context'
6
- require 'realm/container'
7
- require 'realm/dispatcher'
8
- require 'realm/event_router'
9
- require 'realm/multi_worker'
10
- require 'realm/health_status'
11
- require 'realm/runtime/session'
12
-
13
3
  module Realm
14
4
  class Runtime
15
5
  delegate :query, :run, :run_as_job, :wait_for_jobs, to: :dispatcher
@@ -1,7 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- require 'realm/dispatcher'
4
-
5
3
  module Realm
6
4
  class Runtime
7
5
  class Session
@@ -0,0 +1,48 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'dry-struct'
4
+ require 'dry-schema'
5
+
6
+ module Realm
7
+ class Struct < Dry::Struct
8
+ class << self
9
+ def to_dry_schema(type: :schema) # rubocop:disable Metrics/AbcSize
10
+ keys = schema.type.keys
11
+
12
+ Dry::Schema.send(schema_type_to_method(type)) do
13
+ keys.each do |key|
14
+ param = key.required? ? required(key.name) : optional(key.name)
15
+
16
+ if key.type.constructor_type == Dry::Types::Array::Constructor # array type
17
+ member = key.type.member
18
+ param.array(member.respond_to?(:to_dry_schema) ? member.to_dry_schema(type: type) : member)
19
+ elsif key.respond_to?(:to_dry_schema) # realm struct
20
+ param.hash(key.to_dry_schema(type: type))
21
+ else
22
+ param.send(key.required? ? :filled : :maybe, key.type)
23
+ end
24
+ end
25
+ end
26
+ end
27
+
28
+ def merge(attributes)
29
+ clone.attributes(attributes)
30
+ end
31
+
32
+ private
33
+
34
+ def schema_type_to_method(type)
35
+ case type
36
+ when :schema
37
+ :define
38
+ when :params
39
+ :Params
40
+ when :json
41
+ :JSON
42
+ else
43
+ raise "Not supported schema type #{type}"
44
+ end
45
+ end
46
+ end
47
+ end
48
+ end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: realm-core
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.7.1
4
+ version: 0.7.6
5
5
  platform: ruby
6
6
  authors:
7
7
  - developers@reevoo.com
8
- autorequire:
8
+ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2021-06-29 00:00:00.000000000 Z
11
+ date: 2021-07-28 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activesupport
@@ -108,6 +108,20 @@ dependencies:
108
108
  - - "~>"
109
109
  - !ruby/object:Gem::Version
110
110
  version: '1.5'
111
+ - !ruby/object:Gem::Dependency
112
+ name: zeitwerk
113
+ requirement: !ruby/object:Gem::Requirement
114
+ requirements:
115
+ - - "~>"
116
+ - !ruby/object:Gem::Version
117
+ version: '2.4'
118
+ type: :runtime
119
+ prerelease: false
120
+ version_requirements: !ruby/object:Gem::Requirement
121
+ requirements:
122
+ - - "~>"
123
+ - !ruby/object:Gem::Version
124
+ version: '2.4'
111
125
  - !ruby/object:Gem::Dependency
112
126
  name: pry-byebug
113
127
  requirement: !ruby/object:Gem::Requirement
@@ -150,8 +164,8 @@ dependencies:
150
164
  - - ">="
151
165
  - !ruby/object:Gem::Version
152
166
  version: '0'
153
- description:
154
- email:
167
+ description:
168
+ email:
155
169
  executables: []
156
170
  extensions: []
157
171
  extra_rdoc_files: []
@@ -165,6 +179,7 @@ files:
165
179
  - lib/realm/config.rb
166
180
  - lib/realm/container.rb
167
181
  - lib/realm/context.rb
182
+ - lib/realm/contract.rb
168
183
  - lib/realm/dependency.rb
169
184
  - lib/realm/dispatcher.rb
170
185
  - lib/realm/domain_resolver.rb
@@ -190,12 +205,13 @@ files:
190
205
  - lib/realm/query_handler.rb
191
206
  - lib/realm/runtime.rb
192
207
  - lib/realm/runtime/session.rb
208
+ - lib/realm/struct.rb
193
209
  - lib/realm/types.rb
194
- homepage:
210
+ homepage:
195
211
  licenses:
196
212
  - MIT
197
213
  metadata: {}
198
- post_install_message:
214
+ post_install_message:
199
215
  rdoc_options: []
200
216
  require_paths:
201
217
  - lib
@@ -210,8 +226,8 @@ required_rubygems_version: !ruby/object:Gem::Requirement
210
226
  - !ruby/object:Gem::Version
211
227
  version: '0'
212
228
  requirements: []
213
- rubygems_version: 3.1.6
214
- signing_key:
229
+ rubygems_version: 3.1.4
230
+ signing_key:
215
231
  specification_version: 4
216
232
  summary: Domain layer framework following Domain-driven/CQRS design principles
217
233
  test_files: []