Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use #to_hash instead of #as_json #44

Merged
merged 1 commit into from
Jan 1, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions .rubocop_todo.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# This configuration was generated by
# `rubocop --auto-gen-config`
# on 2023-12-24 10:13:24 UTC using RuboCop version 1.59.0.
# on 2024-01-01 09:54:28 UTC using RuboCop version 1.59.0.
# The point is for the user to remove these configuration records
# one by one as the offenses are removed from the code base.
# Note that changes in the inspected code, or installation of new
Expand All @@ -27,7 +27,7 @@ Metrics/ClassLength:
# Offense count: 1
# Configuration parameters: CountComments, CountAsOne.
Metrics/ModuleLength:
Max: 105
Max: 111

# Offense count: 1
# Configuration parameters: CountKeywordArgs, MaxOptionalParameters.
Expand Down
4 changes: 2 additions & 2 deletions lib/operations.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,12 @@
require "operations/contract"
require "operations/contract/messages_resolver"
require "operations/convenience"
require "operations/command"
require "operations/result"
require "operations/form"
require "operations/form/base"
require "operations/form/attribute"
require "operations/form/builder"
require "operations/command"
require "operations/result"

# The root gem module
module Operations
Expand Down
12 changes: 6 additions & 6 deletions lib/operations/command.rb
Original file line number Diff line number Diff line change
Expand Up @@ -290,11 +290,11 @@ def pretty_print(pp)
end
end

def as_json(*)
def to_hash
{
**main_components_as_json,
**form_components_as_json,
configuration: configuration.as_json
**main_components_to_hash,
**form_components_to_hash,
configuration: configuration
}
end

Expand All @@ -304,7 +304,7 @@ def form_class

private

def main_components_as_json
def main_components_to_hash
{
operation: operation.class.name,
contract: contract.class.name,
Expand All @@ -316,7 +316,7 @@ def main_components_as_json
}
end

def form_components_as_json
def form_components_to_hash
{
form_model_map: form_model_map,
form_base: form_base.name,
Expand Down
7 changes: 7 additions & 0 deletions lib/operations/form/base.rb
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,13 @@ def read_attribute(name)
end
end

def to_hash
{
attributes: attributes,
errors: errors
}
end

private

def add_messages(errors, key, messages)
Expand Down
12 changes: 8 additions & 4 deletions lib/operations/result.rb
Original file line number Diff line number Diff line change
Expand Up @@ -92,16 +92,20 @@ def pretty_print(pp)
end
end

def as_json(*, include_command: false, **)
def as_json(options = {})
to_hash(**options.slice(:include_command))
end

def to_hash(include_command: false)
hash = {
component: component,
params: params,
context: context_as_json,
context: context_to_hash,
on_success: on_success.as_json,
on_failure: on_failure.as_json,
errors: errors(full: true).to_h
}
hash[:command] = operation.as_json if include_command
hash[:command] = operation&.to_hash if include_command
hash
end

Expand All @@ -112,7 +116,7 @@ def errors_with_code?(name, *names)
(errors.map { |error| error.meta[:code] } & names).present?
end

def context_as_json
def context_to_hash
context.transform_values do |context_value|
next context_value.class.name unless context_value.respond_to?(:id)

Expand Down
26 changes: 13 additions & 13 deletions spec/operations/command_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -921,7 +921,7 @@ def build(**kwargs)
end

describe "#as_json" do
subject(:as_json) { command.as_json(:ignored_arg, ignored: :kwarg) }
subject(:as_json) { command.as_json }

let(:command) { DummyOperation.instance }
let(:command_implementation) do
Expand Down Expand Up @@ -988,18 +988,18 @@ def call; end

specify do
expect(as_json).to eq(
operation: "DummyOperation",
contract: "DummyOperation::Contract",
policies: ["DummyOperation::Policy"],
preconditions: ["DummyOperation::Precondition"],
idempotency: ["DummyOperation::IdempotencyCheck"],
on_success: ["DummyOperation::OnSuccess"],
on_failure: ["DummyOperation::OnFailure"],
form_base: "DummyOperation::FormBase",
form_class: "DummyOperation::FormClass",
form_hydrator: "DummyOperation::FormHydrator",
form_model_map: { [:attribute] => "attribute_map" },
configuration: { "after_commit" => {}, "error_reporter" => {}, "transaction" => {} }
"operation" => "DummyOperation",
"contract" => "DummyOperation::Contract",
"policies" => ["DummyOperation::Policy"],
"preconditions" => ["DummyOperation::Precondition"],
"idempotency" => ["DummyOperation::IdempotencyCheck"],
"on_success" => ["DummyOperation::OnSuccess"],
"on_failure" => ["DummyOperation::OnFailure"],
"form_base" => "DummyOperation::FormBase",
"form_class" => "DummyOperation::FormClass",
"form_hydrator" => "DummyOperation::FormHydrator",
"form_model_map" => { "[:attribute]" => "attribute_map" },
"configuration" => { "after_commit" => {}, "error_reporter" => {}, "transaction" => {} }
)
end
end
Expand Down
33 changes: 33 additions & 0 deletions spec/operations/form/base_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -305,6 +305,39 @@
it { is_expected.to be_valid }
end
end

describe "#as_json" do
subject(:as_json) { form.as_json }

let(:messages) do
{
name: ["should be present"],
author: { title: [:invalid] }
}
end
let(:attributes) do
{
name: "Name",
author: { ignored: 42 },
ignored: 42
}
end

specify do
expect(as_json).to eq({
"attributes" => {
"author" => {
"attributes" => { "title" => nil },
"errors" => { title: ["is invalid"] }
},
"name" => "Name",
"posts" => [],
"tags" => []
},
"errors" => { name: ["should be present"] }
})
end
end
end

it_behaves_like "a form base", form_base: described_class
Expand Down
8 changes: 4 additions & 4 deletions spec/operations/result_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -209,7 +209,7 @@ def initialize(params, messages:)
end

describe "#as_json" do
subject(:as_json) { result.as_json(:ignored_arg, ignored: :kwarg) }
subject(:as_json) { result.as_json }

let(:traceable_object) { Struct.new(:id, :name) }
let(:anonymous_object) { Struct.new(:name) }
Expand All @@ -224,7 +224,7 @@ def initialize(params, messages:)
let(:on_success) { [Dry::Monads::Success(Entity: "Model#1"), Dry::Monads::Failure(additional: :value)] }
let(:on_failure) { [Dry::Monads::Success(Entity: "Model#1"), Dry::Monads::Failure(additional: :value)] }
let(:errors) { Dry::Validation::MessageSet.new([message]).freeze }
let(:command_json) do
let(:command_to_hash) do
{
operation: "DummyOperation",
contract: "DummyOperation::Contract",
Expand All @@ -247,7 +247,7 @@ def initialize(params, messages:)
stub_const("TraceableObject", traceable_object)
stub_const("AnonymousObject", anonymous_object)

allow(operation).to receive(:as_json).and_return(command_json)
allow(operation).to receive(:to_hash).and_return(command_to_hash)
end

specify do
Expand All @@ -273,7 +273,7 @@ def initialize(params, messages:)
specify do
expect(as_json).to include(
component: :contract,
command: command_json,
command: command_to_hash,
params: { id: 123, name: "Jon", lastname: "Snow" },
context: { record: "TraceableObject#1", object: "AnonymousObject" },
on_success: match([
Expand Down
Loading