Last active
May 18, 2023 11:36
-
-
Save matheusazzi/ae04d27e1bdf3b57a30469d9f460c437 to your computer and use it in GitHub Desktop.
Revisions
-
matheusazzi revised this gist
May 18, 2023 . 1 changed file with 2 additions and 2 deletions.There are no files selected for viewing
This file contains hidden or 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 charactersOriginal file line number Diff line number Diff line change @@ -1,6 +1,6 @@ # frozen_string_literal: true class Things::Something Result = Data.define(:thing, :errors) do def initialize(thing: nil, errors: []) super @@ -39,7 +39,7 @@ def find_or_process_things end # Usage result = Things::Something.call(params) # => Things::Something::Result result.successful? # => true result.errors # => [] result.thing # => <ThingRecord... -
matheusazzi revised this gist
May 18, 2023 . 1 changed file with 18 additions and 12 deletions.There are no files selected for viewing
This file contains hidden or 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 charactersOriginal file line number Diff line number Diff line change @@ -1,39 +1,45 @@ # frozen_string_literal: true class ThingService Result = Data.define(:thing, :errors) do def initialize(thing: nil, errors: []) super end def successful? errors.empty? end end def self.call(params, scope = ThingModel) new(params, scope).call end def call something = find_or_process_things if something.successful? Result.new(thing: something) else Result.new(errors: something.errors, thing: something) end end def initialize(params, scope) @params = params @scope = scope end private_class_method :new private def find_or_process_things @scope.where(...) end end # Usage result = ThingService.call(params) # => ThingService::Result result.successful? # => true result.errors # => [] result.thing # => <ThingRecord... -
matheusazzi revised this gist
May 19, 2022 . 1 changed file with 16 additions and 19 deletions.There are no files selected for viewing
This file contains hidden or 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 charactersOriginal file line number Diff line number Diff line change @@ -1,42 +1,39 @@ class ThingService Result = Struct.new(:errors, :thing, defaults: { errors: [] }) do def successful? errors.empty? end end def self.call(params, thing = ThingModel.new) new(params, thing).call end def call something = find_or_process_things if something.successful? Result.new(thing: something...) else Result.new(errors: something.errors, thing: something...) end end def initialize(params, thing) @params = params @thing = thing end private_class_method :new private def find_or_process_things # ... end end # Usage t = ThingService.call(params) # => ThingService::Result t.successful? # => true t.errors # => [] t.thing # => <ThingRecord... -
matheusazzi revised this gist
Feb 9, 2018 . 1 changed file with 7 additions and 1 deletion.There are no files selected for viewing
This file contains hidden or 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 charactersOriginal file line number Diff line number Diff line change @@ -33,4 +33,10 @@ def find_things end end # Usage t = ThingService.call(params) # => ThingService::Result t.success? # => true t.errors # => [] t.things # => [Thing, Thing, ...] -
matheusazzi revised this gist
Sep 25, 2017 . 1 changed file with 18 additions and 18 deletions.There are no files selected for viewing
This file contains hidden or 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 charactersOriginal file line number Diff line number Diff line change @@ -1,36 +1,36 @@ class ThingService attr_reader :thing, :params Result = Struct.new(:errors, :things) do def success? errors.empty? end end def self.call(thing = ThingModel.new, params) new(thing, params).call end def call @something ||= find_things if @something.success? Result.new([...], nil) else Result.new([], '...') end end private def initialize(thing, params) @thing = thing @params = params end def find_things # ... end end ThingService.call(params) -
matheusazzi created this gist
Jun 27, 2017 .There are no files selected for viewing
This file contains hidden or 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 charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,36 @@ class ThingService attr_reader :param1, :param2 Result = Struct.new(:success?, :things, :error) def self.call(param1, param2) new(param1, param2).call end def call do_something end private def initialize(param1, param2) @param1 = param1 @param2 = param2 end def do_something @something ||= find_things if @something.success? Result.new(true, [...], nil) else Result.new(false, [], '...') end end def find_things # ... end end ThingService.call(param1, param2)