Skip to content

Commit

Permalink
update
Browse files Browse the repository at this point in the history
  • Loading branch information
adham90 committed Jan 19, 2021
1 parent d430191 commit 68f8588
Show file tree
Hide file tree
Showing 8 changed files with 52 additions and 21 deletions.
2 changes: 1 addition & 1 deletion Gemfile.lock
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
PATH
remote: .
specs:
command_service_object (1.1.0)
command_service_object (1.4.0)
hutch (~> 1.0)
virtus (~> 1.0, >= 1.0.5)

Expand Down
6 changes: 3 additions & 3 deletions lib/generators/service/command/templates/command_spec.rb.erb
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,12 @@

require 'rails_helper'

describe <%= service_name.classify %>::Commands::<%= @test %>, type: :model do
describe <%= service_name.classify %>::Commands::<%= @command %>, type: :model do
describe 'attrubites' do
it { expect(subject).to respond_to(:EX) }
<%# it { expect(subject).to respond_to(:EX) } %>
end

describe 'validatios' do
it { should validate_presence_of(:EX) }
<%# it { should validate_presence_of(:EX) } %>
end
end
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ require 'rails_helper'

describe <%= service_name.classify %>::Externals::<%= @external %> do
# Example
# context "when success" do
# context 'when success' do
# it 'should be okay?' do
# expect(subject.[method_name]).to eql([result])
# end
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,33 +7,37 @@ class << self
def call(cmd)
@cmd = cmd
@bm = Benchmark.measure do
raise Errors::InvalidCommand, cmd.class if cmd.invalid?
raise Errors::InvalidCommand if cmd.invalid?

@usecase = usecase_class.new(cmd)
raise Errors::NotAuthorizedError, cmd.class unless usecase.allowed?

raise Errors::NotAuthorizedError unless usecase.allowed?

@result = ServiceResult.new { usecase.call }

rollback if result.error.present?
usecase.broadcast if result.ok?
end

log_command
result
rescue StandardError => e
ServiceResult.new { raise e }
ServiceResult.new { raise e }
end

def rollback
usecase.rollback_micros
usecase.rollback
log_errors(result.error)
end

private

def log_command
service_logger = ActiveSupport::Logger.new(Rails.root.join('log', 'services.log').to_s)
service_logger.formatter = proc do |severity, datetime, progname, msg|
FileUtils.mkdir_p 'log/services'
service_logger = ActiveSupport::Logger.new(
Rails.root.join('log', 'services', "#{service_name.underscore}.log").to_s, 'daily'
)
service_logger.formatter = proc do |_severity, datetime, _progname, msg|
"[#{msg['usecase']}] [#{msg['status']}] [#{datetime.to_s(:db)} ##{Process.pid}] -- #{msg['body']}\n"
end
log_body = result.ok? ? success_log : failure_log
Expand All @@ -46,10 +50,10 @@ def success_log
usecase: "#{service_name}::#{usecase_name}",
status: 'success',
body: {
cmd: cmd.as_json,
result: result.value!.as_json,
benchmark: bm.as_json
}
cmd: cmd.as_json,
result: result.value!.as_json,
benchmark: bm.as_json
}
}.as_json
end

Expand Down Expand Up @@ -78,3 +82,13 @@ def usecase_name
end
end
end

module Errors
class NotAuthorizedError < StandardError
def initialize(msg: 'not allowd')
super(msg)
end
end

class InvalidCommand < StandardError; end
end
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@

class CommandBase
include ActiveModel::Validations
include Virtus.model(nullify_blank: true)
include Virtus.model

# if you need to enable policies add issuer attribute
attribute :issuer, Object, default: :default_issuer
attribute :issuer, Issuer, default: :default_issuer

private

Expand Down
10 changes: 10 additions & 0 deletions lib/generators/service/install/templates/services/issuer.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
class Issuer
include ActiveModel::Validations
include Virtus.model

attribute :id, String
attribute :type, String
attribute :scope, Array[String], default: []

validates :id, :type, presence: true
end
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
require 'json'

class Util::Json < Virtus::Attribute
def coerce(value)
value.is_a?(::Hash) ? value : JSON.parse(value)
end
end
8 changes: 4 additions & 4 deletions lib/generators/service/usecase/templates/usecase_spec.rb.erb
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,12 @@

require 'rails_helper'

describe <%= service_name.classify %>::Usecases::<%= @test %> do
let(:cmd) { <%= service_name.classify %>::Commands::<%= @test %>.new(cmd_params) }
describe <%= service_name.classify %>::Usecases::<%= @usecase %> do
let(:cmd) { <%= service_name.classify %>::Commands::<%= @usecase %>.new(cmd_params) }
let(:cmd_default_params) {{ name: 'test_name' }}

context "when success" do
let(:cmd_params) { cmd_default_params.merge!({ name: 'valid_name' }) }
context 'when success' do
let(:cmd_params) { cmd_default_params }
let!(:result) { ApplicationService.call(cmd) }

it 'should be okay?' do
Expand Down

0 comments on commit 68f8588

Please sign in to comment.