Skip to content

Commit

Permalink
feat(has_inspect): indroduce inspect for Service and Result
Browse files Browse the repository at this point in the history
  • Loading branch information
marian13 committed Nov 25, 2022
1 parent 763b5f4 commit c3815c8
Show file tree
Hide file tree
Showing 14 changed files with 260 additions and 48 deletions.
2 changes: 1 addition & 1 deletion ROADMAP.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
| High | 🚧 | Optimize `stack.dup` in `MethodMiddlewares#call` | Core v3 |
| Medium | 🚧 | Make a decision of what to do with `printable_block` in custom RSpec matchers | |
| Medium | 🚧 | User-friendly exception messages | |
| High | 🚧 | Factories for POROs in specs ❗❗❗ | |
| High | 🚧 | Factories for POROs in specs ❗❗❗ | Start with `result_class`, `class self::Result`, `service_class` |
| High | 🚧 | Resolve warning during specs | |
| Medium | 🚧 | Consider to change/rewrite `delegate` backend to minify its interface | |
| Medium | 🚧 | Same order of attr macros, delegators, initialize, class methods, attr methods, queries, actions, `to_*`, comparison, inspect | |
Expand Down
3 changes: 3 additions & 0 deletions lib/convenient_service/configs/standard.rb
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ module Standard
use Plugins::Common::HasCallbacks::Concern
use Plugins::Common::HasAroundCallbacks::Concern

use Plugins::Service::HasInspect::Concern
##
# NOTE: Optional plugins.
# TODO: Specs.
Expand Down Expand Up @@ -105,6 +106,8 @@ class self::Result

use Plugins::Result::HasResultShortSyntax::Concern
use Plugins::Result::CanRecalculateResult::Concern

use Plugins::Result::HasInspect::Concern
end

middlewares :initialize do
Expand Down
1 change: 1 addition & 0 deletions lib/convenient_service/service/plugins.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
# NOTE: Order matters.
#
require_relative "plugins/can_recalculate_result"
require_relative "plugins/has_inspect"
require_relative "plugins/has_result"
require_relative "plugins/has_result_method_steps"
require_relative "plugins/has_result_short_syntax"
Expand Down
3 changes: 3 additions & 0 deletions lib/convenient_service/service/plugins/has_inspect.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# frozen_string_literal: true

require_relative "has_inspect/concern"
22 changes: 22 additions & 0 deletions lib/convenient_service/service/plugins/has_inspect/concern.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
# frozen_string_literal: true

module ConvenientService
module Service
module Plugins
module HasInspect
module Concern
include Support::Concern

instance_methods do
##
# @return [String]
#
def inspect
"<#{self.class.name}>"
end
end
end
end
end
end
end
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

require_relative "plugins/can_recalculate_result"
require_relative "plugins/has_jsend_status_and_attributes"
require_relative "plugins/has_inspect"
require_relative "plugins/has_result_short_syntax"
require_relative "plugins/marks_result_status_as_checked"
require_relative "plugins/raises_on_not_checked_result_status"
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# frozen_string_literal: true

require_relative "has_inspect/concern"
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
# frozen_string_literal: true

module ConvenientService
module Service
module Plugins
module HasResult
module Entities
class Result
module Plugins
module HasInspect
module Concern
include Support::Concern

instance_methods do
##
# @return [String]
#
def inspect
"<#{service.class.name}::Result status: :#{status}>"
end
end
end
end
end
end
end
end
end
end
end
6 changes: 4 additions & 2 deletions spec/lib/convenient_service/configs/standard_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,8 @@
ConvenientService::Service::Plugins::CanRecalculateResult::Concern,
ConvenientService::Service::Plugins::HasResultStatusCheckShortSyntax::Concern,
ConvenientService::Common::Plugins::HasCallbacks::Concern,
ConvenientService::Common::Plugins::HasAroundCallbacks::Concern
ConvenientService::Common::Plugins::HasAroundCallbacks::Concern,
ConvenientService::Service::Plugins::HasInspect::Concern
]
end

Expand Down Expand Up @@ -136,7 +137,8 @@
ConvenientService::Common::Plugins::HasConstructor::Concern,
ConvenientService::Service::Plugins::HasResult::Entities::Result::Plugins::HasJsendStatusAndAttributes::Concern,
ConvenientService::Service::Plugins::HasResult::Entities::Result::Plugins::HasResultShortSyntax::Concern,
ConvenientService::Service::Plugins::HasResult::Entities::Result::Plugins::CanRecalculateResult::Concern
ConvenientService::Service::Plugins::HasResult::Entities::Result::Plugins::CanRecalculateResult::Concern,
ConvenientService::Service::Plugins::HasResult::Entities::Result::Plugins::HasInspect::Concern
]
end

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
# frozen_string_literal: true

require "spec_helper"

require "convenient_service"

# rubocop:disable RSpec/NestedGroups, RSpec/MultipleMemoizedHelpers
RSpec.describe ConvenientService::Service::Plugins::HasInspect::Concern do
include ConvenientService::RSpec::Matchers::DelegateTo

let(:service_class) do
Class.new.tap do |klass|
klass.class_exec(described_class) do |mod|
include mod

def self.name
"Service"
end
end
end
end

let(:service_instance) { service_class.new }

example_group "modules" do
include ConvenientService::RSpec::Matchers::IncludeModule

subject { described_class }

it { is_expected.to include_module(ConvenientService::Support::Concern) }

context "when included" do
subject { service_class }

it { is_expected.to include_module(described_class::InstanceMethods) }
end
end

example_group "instance methods" do
describe "#inspect" do
it "returns `inspect` representation of service" do
expect(service_instance.inspect).to eq("<#{service_class.name}>")
end
end
end
end
# rubocop:enable RSpec/NestedGroups, RSpec/MultipleMemoizedHelpers
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
##
# TODO: Integration specs.
#
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
# frozen_string_literal: true

require "spec_helper"

require "convenient_service"

# rubocop:disable RSpec/NestedGroups, RSpec/MultipleMemoizedHelpers
RSpec.describe ConvenientService::Service::Plugins::HasResult::Entities::Result::Plugins::HasInspect::Concern do
include ConvenientService::RSpec::Matchers::DelegateTo

example_group "modules" do
include ConvenientService::RSpec::Matchers::IncludeModule

subject { described_class }

let(:result_class) do
Class.new.tap do |klass|
klass.class_exec(described_class) do |mod|
include mod
end
end
end

let(:result_instance) { result_class.new }

it { is_expected.to include_module(ConvenientService::Support::Concern) }

context "when included" do
subject { result_class }

before { result_class }

it { is_expected.to include_module(described_class::InstanceMethods) }
end
end

example_group "instance methods" do
let(:service_class) do
Class.new do
def self.name
"Service"
end
end
end

let(:service_instance) { service_class.new }

# rubocop:disable RSpec/LeakyConstantDeclaration, Lint/ConstantDefinitionInBlock
let(:result_class) do
Class.new do
include ConvenientService::Core

concerns do
use ConvenientService::Common::Plugins::HasInternals::Concern
use ConvenientService::Common::Plugins::HasConstructor::Concern
use ConvenientService::Service::Plugins::HasResult::Entities::Result::Plugins::HasJsendStatusAndAttributes::Concern
use ConvenientService::Service::Plugins::HasResult::Entities::Result::Plugins::HasInspect::Concern
end

middlewares :initialize do
use ConvenientService::Common::Plugins::NormalizesEnv::Middleware

use ConvenientService::Service::Plugins::HasResult::Entities::Result::Plugins::HasJsendStatusAndAttributes::Middleware
end

class self::Internals
include ConvenientService::Core

concerns do
use ConvenientService::Common::Plugins::HasInternals::Entities::Internals::Plugins::HasCache::Concern
end
end
end
end
# rubocop:enable RSpec/LeakyConstantDeclaration, Lint/ConstantDefinitionInBlock

let(:result_instance) do
result_class.new(
status: :success,
data: {},
message: "",
code: :default_success,
service: service_instance
)
end

describe "#inspect" do
it "returns `inspect` representation of result" do
expect(result_instance.inspect).to eq("<#{result_instance.service.class.name}::Result status: :#{result_instance.status}>")
end
end
end
end
# rubocop:enable RSpec/NestedGroups, RSpec/MultipleMemoizedHelpers
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
##
# TODO: Integration specs.
#
Loading

0 comments on commit c3815c8

Please sign in to comment.