Skip to content

Commit

Permalink
feat(has_j_send_status_and_attributes): link result to status, data, …
Browse files Browse the repository at this point in the history
…message, code
  • Loading branch information
marian13 committed May 1, 2023
1 parent 9ea2d27 commit dd87723
Show file tree
Hide file tree
Showing 19 changed files with 348 additions and 155 deletions.
1 change: 1 addition & 0 deletions lib/convenient_service/configs/minimal.rb
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,7 @@ class self::Result
use Plugins::Result::HasInspect::Concern

use Plugins::Common::HasConstructor::Concern
use Plugins::Common::HasConstructorWithoutInitialize::Concern

use Plugins::Result::HasJSendStatusAndAttributes::Concern
end
Expand Down
8 changes: 4 additions & 4 deletions lib/convenient_service/rspec/matchers/custom/results/base.rb
Original file line number Diff line number Diff line change
Expand Up @@ -36,13 +36,13 @@ def matches?(result)
##
# IMPORTANT: Result status is NOT marked as checked intentionally, since it is a mutable operation.
#
rules << ->(result) { result.status.in?(statuses) }
rules << ->(result) { result.status.in?(statuses.map { |status| result.class.status(value: status, result: result) }) }

rules << ->(result) { result.service.instance_of?(service_class) } if used_of_service?
rules << ->(result) { Commands::MatchResultStep.call(result: result, step: step) } if used_of_step?
rules << ->(result) { result.unsafe_data == data } if used_data?
rules << ->(result) { result.unsafe_message == message } if used_message?
rules << ->(result) { result.unsafe_code == code } if used_code?
rules << ->(result) { result.unsafe_data == result.class.data(value: data, result: result) } if used_data?
rules << ->(result) { result.unsafe_message == result.class.message(value: message, result: result) } if used_message?
rules << ->(result) { result.unsafe_code == result.class.code(value: code, result: result) } if used_code?

condition = Utils::Proc.conjunct(rules)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,8 @@ def call
Structs::JSendAttributes.new(service: service, status: status, data: data, message: message, code: code)
end

private

##
# @return [Object]
#
Expand All @@ -52,31 +54,31 @@ def service
# @raise [ConvenientService::Support::Castable::Errors::FailedToCast]
#
def status
@status ||= result.class.status_class.cast!(kwargs[:status])
@status ||= result.class.status(value: kwargs[:status], result: result)
end

##
# @return [ConvenientService::Service::Plugins::HasResult::Entities::Result::Plugins::HasJSendStatusAndAttributes::Entities::Data]
# @raise [ConvenientService::Support::Castable::Errors::FailedToCast]
#
def data
@data ||= result.class.data_class.cast!(kwargs[:data])
@data ||= result.class.data(value: kwargs[:data], result: result)
end

##
# @return [ConvenientService::Service::Plugins::HasResult::Entities::Result::Plugins::HasJSendStatusAndAttributes::Entities::Message]
# @raise [ConvenientService::Support::Castable::Errors::FailedToCast]
#
def message
@message ||= result.class.message_class.cast!(kwargs[:message])
@message ||= result.class.message(value: kwargs[:message], result: result)
end

##
# @return [ConvenientService::Service::Plugins::HasResult::Entities::Result::Plugins::HasJSendStatusAndAttributes::Entities::Code]
# @raise [ConvenientService::Support::Castable::Errors::FailedToCast]
#
def code
@code ||= result.class.code_class.cast!(kwargs[:code])
@code ||= result.class.code(value: kwargs[:code], result: result)
end
end
end
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,46 @@ module Plugins
module HasJSendStatusAndAttributes
module Concern
module ClassMethods
##
# @param value [Object] Can be any type.
# @param result [ConvenientService::Service::Plugins::HasResult::Entities::Result].
# @return [ConvenientService::Service::Plugins::HasResult::Entities::Result::Plugins::HasJSendStatusAndAttributes::Entities::Code]
# @raise [ConvenientService::Support::Castable::Errors::FailedToCast]
#
def code(value:, result: create_without_initialize)
code_class.cast!(value).copy(overrides: {kwargs: {result: result}})
end

##
# @param value [Object] Can be any type.
# @param result [ConvenientService::Service::Plugins::HasResult::Entities::Result].
# @return [ConvenientService::Service::Plugins::HasResult::Entities::Result::Plugins::HasJSendStatusAndAttributes::Entities::Data]
# @raise [ConvenientService::Support::Castable::Errors::FailedToCast]
#
def data(value:, result: create_without_initialize)
data_class.cast!(value).copy(overrides: {kwargs: {result: result}})
end

##
# @param value [Object] Can be any type.
# @param result [ConvenientService::Service::Plugins::HasResult::Entities::Result].
# @return [ConvenientService::Service::Plugins::HasResult::Entities::Result::Plugins::HasJSendStatusAndAttributes::Entities::Message]
# @raise [ConvenientService::Support::Castable::Errors::FailedToCast]
#
def message(value:, result: create_without_initialize)
message_class.cast!(value).copy(overrides: {kwargs: {result: result}})
end

##
# @param value [Object] Can be any type.
# @param result [ConvenientService::Service::Plugins::HasResult::Entities::Result].
# @return [ConvenientService::Service::Plugins::HasResult::Entities::Result::Plugins::HasJSendStatusAndAttributes::Entities::Status]
# @raise [ConvenientService::Support::Castable::Errors::FailedToCast]
#
def status(value:, result: create_without_initialize)
status_class.cast!(value).copy(overrides: {kwargs: {result: result}})
end

##
# @api private
# @return [Class]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,30 +12,48 @@ module Entities
class Code
module Concern
module InstanceMethods
include Support::Copyable

##
# @!attribute [r] value
# @return [Symbol]
#
attr_reader :value

##
# @!attribute [r] result
# @return [ConvenientService::Service::Plugins::HasResult::Entities::Result]
#
attr_reader :result

##
# @param value [Symbol]
# @param result [ConvenientService::Service::Plugins::HasResult::Entities::Result]
# @return [void]
#
def initialize(value:)
def initialize(value:, result: nil)
@value = value
@result = result
end

##
# @param other [Object] Can be any type.
# @return [Boolean, nil]
#
def ==(other)
casted = cast(other)
return unless other.instance_of?(self.class)

return unless casted
return false if result.class != other.result.class
return false if value != other.value

value == casted.value
true
end

##
# @return [Hash]
#
def to_kwargs
@to_kwargs ||= {value: value, result: result}
end

##
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,23 @@ module Entities
class Data
module Concern
module InstanceMethods
include Support::Copyable

##
# @!attribute [r] value
# @return [Hash]
#
attr_reader :value

##
# @!attribute [r] result
# @return [ConvenientService::Service::Plugins::HasResult::Entities::Result]
#
attr_reader :result

##
# @param value [Hash]
# @param result [ConvenientService::Service::Plugins::HasResult::Entities::Result]
# @return [void]
#
# @internal
Expand All @@ -34,8 +43,9 @@ module InstanceMethods
# - https://github.com/rails/rails/issues/22681
# - https://api.rubyonrails.org/classes/ActiveSupport/OrderedOptions.html
#
def initialize(value:)
def initialize(value:, result: nil)
@value = value
@result = result
end

##
Expand All @@ -51,11 +61,12 @@ def has_attribute?(key)
# @return [Boolean, nil]
#
def ==(other)
casted = cast(other)
return unless other.instance_of?(self.class)

return unless casted
return false if result.class != other.result.class
return false if value != other.value

value == casted.value
true
end

##
Expand All @@ -67,6 +78,13 @@ def [](key)
value.fetch(key.to_sym) { raise Errors::NotExistingAttribute.new(attribute: key) }
end

##
# @return [Hash]
#
def to_kwargs
@to_kwargs ||= {value: value, result: result}
end

##
# @return [Hash]
#
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,30 +12,48 @@ module Entities
class Message
module Concern
module InstanceMethods
include Support::Copyable

##
# @!attribute [r] value
# @return [String]
#
attr_reader :value

##
# @!attribute [r] result
# @return [ConvenientService::Service::Plugins::HasResult::Entities::Result]
#
attr_reader :result

##
# @param value [String]
# @param result [ConvenientService::Service::Plugins::HasResult::Entities::Result]
# @return [void]
#
def initialize(value:)
def initialize(value:, result: nil)
@value = value
@result = result
end

##
# @param other [Object] Can be any type.
# @return [Boolean, nil]
#
def ==(other)
casted = cast(other)
return unless other.instance_of?(self.class)

return unless casted
return false if result.class != other.result.class
return false if value != other.value

value == casted.value
true
end

##
# @return [Hash]
#
def to_kwargs
@to_kwargs ||= {value: value, result: result}
end

##
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,30 +12,41 @@ module Entities
class Status
module Concern
module InstanceMethods
include Support::Copyable

##
# @!attribute [r] value
# @return [String]
#
attr_reader :value

##
# @!attribute [r] result
# @return [ConvenientService::Service::Plugins::HasResult::Entities::Result]
#
attr_reader :result

##
# @param value [String]
# @param result [ConvenientService::Service::Plugins::HasResult::Entities::Result]
# @return [void]
#
def initialize(value:)
def initialize(value:, result: nil)
@value = value
@result = result
end

##
# @param other [Object] Can be any type.
# @return [Boolean, nil]
#
def ==(other)
casted = cast(other)
return unless other.instance_of?(self.class)

return unless casted
return false if result.class != other.result.class
return false if value != other.value

value == casted.value
true
end

##
Expand Down Expand Up @@ -88,6 +99,13 @@ def in?(statuses)
statuses.any? { |status| self == status }
end

##
# @return [Hash]
#
def to_kwargs
@to_kwargs ||= {value: value, result: result}
end

##
# @return [String]
#
Expand Down
1 change: 1 addition & 0 deletions spec/lib/convenient_service/configs/minimal_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,7 @@
ConvenientService::Common::Plugins::HasInternals::Concern,
ConvenientService::Service::Plugins::HasResult::Entities::Result::Plugins::HasInspect::Concern,
ConvenientService::Common::Plugins::HasConstructor::Concern,
ConvenientService::Common::Plugins::HasConstructorWithoutInitialize::Concern,
ConvenientService::Service::Plugins::HasResult::Entities::Result::Plugins::HasJSendStatusAndAttributes::Concern
]
end
Expand Down
1 change: 1 addition & 0 deletions spec/lib/convenient_service/configs/standard_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -200,6 +200,7 @@
ConvenientService::Common::Plugins::HasInternals::Concern,
ConvenientService::Service::Plugins::HasResult::Entities::Result::Plugins::HasInspect::Concern,
ConvenientService::Common::Plugins::HasConstructor::Concern,
ConvenientService::Common::Plugins::HasConstructorWithoutInitialize::Concern,
ConvenientService::Service::Plugins::HasResult::Entities::Result::Plugins::HasJSendStatusAndAttributes::Concern,
ConvenientService::Common::Plugins::HasResultDuckShortSyntax::Concern,
ConvenientService::Service::Plugins::HasResult::Entities::Result::Plugins::CanRecalculateResult::Concern,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -204,6 +204,7 @@
ConvenientService::Common::Plugins::HasInternals::Concern,
ConvenientService::Service::Plugins::HasResult::Entities::Result::Plugins::HasInspect::Concern,
ConvenientService::Common::Plugins::HasConstructor::Concern,
ConvenientService::Common::Plugins::HasConstructorWithoutInitialize::Concern,
ConvenientService::Service::Plugins::HasResult::Entities::Result::Plugins::HasJSendStatusAndAttributes::Concern,
ConvenientService::Common::Plugins::HasResultDuckShortSyntax::Concern,
ConvenientService::Service::Plugins::HasResult::Entities::Result::Plugins::CanRecalculateResult::Concern,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -206,6 +206,7 @@
ConvenientService::Common::Plugins::HasInternals::Concern,
ConvenientService::Service::Plugins::HasResult::Entities::Result::Plugins::HasInspect::Concern,
ConvenientService::Common::Plugins::HasConstructor::Concern,
ConvenientService::Common::Plugins::HasConstructorWithoutInitialize::Concern,
ConvenientService::Service::Plugins::HasResult::Entities::Result::Plugins::HasJSendStatusAndAttributes::Concern,
ConvenientService::Common::Plugins::HasResultDuckShortSyntax::Concern,
ConvenientService::Service::Plugins::HasResult::Entities::Result::Plugins::CanRecalculateResult::Concern,
Expand Down
Loading

0 comments on commit dd87723

Please sign in to comment.