-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
10 changed files
with
392 additions
and
116 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
5 changes: 5 additions & 0 deletions
5
lib/convenient_service/common/plugins/can_have_user_provided_entity.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
# frozen_string_literal: true | ||
|
||
require_relative "can_have_user_provided_entity/commands" | ||
require_relative "can_have_user_provided_entity/container" | ||
require_relative "can_have_user_provided_entity/errors" |
3 changes: 3 additions & 0 deletions
3
lib/convenient_service/common/plugins/can_have_user_provided_entity/commands.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
# frozen_string_literal: true | ||
|
||
require_relative "commands/find_or_create_entity" |
117 changes: 117 additions & 0 deletions
117
...nt_service/common/plugins/can_have_user_provided_entity/commands/find_or_create_entity.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,117 @@ | ||
# frozen_string_literal: true | ||
|
||
module ConvenientService | ||
module Common | ||
module Plugins | ||
module CanHaveUserProvidedEntity | ||
module Commands | ||
class FindOrCreateEntity < Support::Command | ||
## | ||
# @!attribute [r] namespace | ||
# @return [Class] | ||
# | ||
attr_reader :namespace | ||
|
||
## | ||
# @!attribute [r] proto_entity | ||
# @return [Class] | ||
# | ||
attr_reader :proto_entity | ||
|
||
## | ||
# @param namespace [Class] | ||
# @param proto_entity [Class] | ||
# @return [void] | ||
# | ||
def initialize(namespace:, proto_entity:) | ||
@namespace = namespace | ||
@proto_entity = proto_entity | ||
end | ||
|
||
## | ||
# @return [void] | ||
# | ||
def call | ||
raise Errors::ProtoEntityHasNoName.new(proto_entity: proto_entity) unless proto_entity_name | ||
raise Errors::ProtoEntityHasNoConcern.new(proto_entity: proto_entity) unless proto_entity_concern | ||
|
||
entity.include proto_entity_concern | ||
|
||
## | ||
# @example Result for service. | ||
# | ||
# klass = ConvenientService::Common::Plugins::CanHaveUserProvidedEntity::Commands::FindOrCreateEntity.call( | ||
# namespace: SomeService, | ||
# proto_entity: ConvenientService::Service::Plugins::HasResult::Entities::Result | ||
# ) | ||
# | ||
# ## | ||
# # `klass` is something like: | ||
# # | ||
# # class Result < ConvenientService::Service::Plugins::HasResult::Entities::Result # or just `class Result` if service (namespace) class defines its own. | ||
# # include ConvenientService::Service::Plugins::HasResult::Entities::Result::Concern # (concern) | ||
# # | ||
# # class << self | ||
# # def proto_entity | ||
# # ## | ||
# # # NOTE: Returns `proto_entity` passed to `FindOrCreateEntity`. | ||
# # # | ||
# # proto_entity | ||
# # end | ||
# # | ||
# # def ==(other) | ||
# # return unless other.respond_to?(:proto_entity) | ||
# # | ||
# # self.proto_entity == other.proto_entity | ||
# # end | ||
# # end | ||
# # end | ||
# | ||
entity.class_exec(proto_entity) do |proto_entity| | ||
define_singleton_method(:proto_entity) { proto_entity } | ||
define_singleton_method(:==) { |other| self.proto_entity == other.proto_entity if other.respond_to?(:proto_entity) } | ||
|
||
## | ||
# TODO: `inspect`. | ||
# | ||
# define_singleton_method(:inspect) { "#{entity}(Prototyped by #{proto_entity})" } | ||
end | ||
|
||
entity | ||
end | ||
|
||
private | ||
|
||
## | ||
# @return [Class] | ||
# | ||
def entity | ||
@entity ||= Utils::Module.get_own_const(namespace, proto_entity_demodulized_name) || ::Class.new(proto_entity) | ||
end | ||
|
||
## | ||
# @return [String, nil] | ||
# | ||
def proto_entity_name | ||
Utils::Object.memoize_including_falsy_values(self, :@proto_entity_name) { proto_entity.name } | ||
end | ||
|
||
## | ||
# @return [String] | ||
# | ||
def proto_entity_demodulized_name | ||
@proto_entity_demodulized_name ||= Utils::String.demodulize(proto_entity_name) | ||
end | ||
|
||
## | ||
# @return [Module] | ||
# | ||
def proto_entity_concern | ||
Utils::Object.memoize_including_falsy_values(self, :@proto_entity_concern) { Utils::Module.get_own_const(proto_entity, :Concern) } | ||
end | ||
end | ||
end | ||
end | ||
end | ||
end | ||
end |
17 changes: 17 additions & 0 deletions
17
lib/convenient_service/common/plugins/can_have_user_provided_entity/container.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
# frozen_string_literal: true | ||
|
||
module ConvenientService | ||
module Common | ||
module Plugins | ||
module CanHaveUserProvidedEntity | ||
module Container | ||
include Support::DependencyContainer::Export | ||
|
||
export :"commands.FindOrCreateEntity" do | ||
Commands::FindOrCreateEntity | ||
end | ||
end | ||
end | ||
end | ||
end | ||
end |
42 changes: 42 additions & 0 deletions
42
lib/convenient_service/common/plugins/can_have_user_provided_entity/errors.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
# frozen_string_literal: true | ||
|
||
module ConvenientService | ||
module Common | ||
module Plugins | ||
module CanHaveUserProvidedEntity | ||
module Errors | ||
class ProtoEntityHasNoName < ConvenientService::Error | ||
def initialize(proto_entity:) | ||
message = <<~TEXT | ||
Proto entity `#{proto_entity}` has no name. | ||
In other words: | ||
proto_entity.name | ||
# => nil | ||
NOTE: Anonymous classes do NOT have names. Are you passing an anonymous class? | ||
TEXT | ||
|
||
super(message) | ||
end | ||
end | ||
|
||
class ProtoEntityHasNoConcern < ConvenientService::Error | ||
def initialize(proto_entity:) | ||
message = <<~TEXT | ||
Proto entity `#{proto_entity}` has no concern. | ||
Have a look at `ConvenientService::Service::Plugins::HasResult::Entities::Result`. | ||
It is an example of a valid proto entity. | ||
TEXT | ||
|
||
super(message) | ||
end | ||
end | ||
end | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.