Skip to content

Instantly share code, notes, and snippets.

@matheusazzi
Last active May 18, 2023 11:36
Show Gist options
  • Select an option

  • Save matheusazzi/ae04d27e1bdf3b57a30469d9f460c437 to your computer and use it in GitHub Desktop.

Select an option

Save matheusazzi/ae04d27e1bdf3b57a30469d9f460c437 to your computer and use it in GitHub Desktop.

Revisions

  1. matheusazzi revised this gist May 18, 2023. 1 changed file with 2 additions and 2 deletions.
    4 changes: 2 additions & 2 deletions thing_service.rb
    Original file line number Diff line number Diff line change
    @@ -1,6 +1,6 @@
    # frozen_string_literal: true

    class ThingService
    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 = ThingService.call(params) # => ThingService::Result
    result = Things::Something.call(params) # => Things::Something::Result
    result.successful? # => true
    result.errors # => []
    result.thing # => <ThingRecord...
  2. matheusazzi revised this gist May 18, 2023. 1 changed file with 18 additions and 12 deletions.
    30 changes: 18 additions & 12 deletions thing_service.rb
    Original file line number Diff line number Diff line change
    @@ -1,39 +1,45 @@
    # frozen_string_literal: true

    class ThingService
    Result = Struct.new(:errors, :thing, defaults: { errors: [] }) do
    Result = Data.define(:thing, :errors) do
    def initialize(thing: nil, errors: [])
    super
    end

    def successful?
    errors.empty?
    end
    end

    def self.call(params, thing = ThingModel.new)
    new(params, thing).call
    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...)
    Result.new(thing: something)
    else
    Result.new(errors: something.errors, thing: something...)
    Result.new(errors: something.errors, thing: something)
    end
    end

    def initialize(params, thing)
    def initialize(params, scope)
    @params = params
    @thing = thing
    @scope = scope
    end
    private_class_method :new

    private

    def find_or_process_things
    # ...
    @scope.where(...)
    end
    end

    # Usage
    t = ThingService.call(params) # => ThingService::Result
    t.successful? # => true
    t.errors # => []
    t.thing # => <ThingRecord...
    result = ThingService.call(params) # => ThingService::Result
    result.successful? # => true
    result.errors # => []
    result.thing # => <ThingRecord...
  3. matheusazzi revised this gist May 19, 2022. 1 changed file with 16 additions and 19 deletions.
    35 changes: 16 additions & 19 deletions thing_service.rb
    Original file line number Diff line number Diff line change
    @@ -1,42 +1,39 @@
    class ThingService
    attr_reader :thing, :params

    Result = Struct.new(:errors, :things) do
    def success?
    Result = Struct.new(:errors, :thing, defaults: { errors: [] }) do
    def successful?
    errors.empty?
    end
    end

    def self.call(thing = ThingModel.new, params)
    new(thing, params).call
    def self.call(params, thing = ThingModel.new)
    new(params, thing).call
    end

    def call
    @something ||= find_things
    something = find_or_process_things

    if @something.success?
    Result.new([...], nil)
    if something.successful?
    Result.new(thing: something...)
    else
    Result.new([], '...')
    Result.new(errors: something.errors, thing: something...)
    end
    end

    private

    def initialize(thing, params)
    @thing = thing
    def initialize(params, thing)
    @params = params
    @thing = thing
    end
    private_class_method :new

    def find_things
    private

    def find_or_process_things
    # ...
    end
    end

    # Usage
    t = ThingService.call(params) # => ThingService::Result
    t.success? # => true
    t.successful? # => true
    t.errors # => []
    t.things # => [Thing, Thing, ...]


    t.thing # => <ThingRecord...
  4. matheusazzi revised this gist Feb 9, 2018. 1 changed file with 7 additions and 1 deletion.
    8 changes: 7 additions & 1 deletion thing_service.rb
    Original file line number Diff line number Diff line change
    @@ -33,4 +33,10 @@ def find_things
    end
    end

    ThingService.call(params)
    # Usage
    t = ThingService.call(params) # => ThingService::Result
    t.success? # => true
    t.errors # => []
    t.things # => [Thing, Thing, ...]


  5. matheusazzi revised this gist Sep 25, 2017. 1 changed file with 18 additions and 18 deletions.
    36 changes: 18 additions & 18 deletions thing_service.rb
    Original file line number Diff line number Diff line change
    @@ -1,36 +1,36 @@
    class ThingService
    attr_reader :param1, :param2
    attr_reader :thing, :params

    Result = Struct.new(:success?, :things, :error)

    def self.call(param1, param2)
    new(param1, param2).call
    end

    def call
    do_something
    Result = Struct.new(:errors, :things) do
    def success?
    errors.empty?
    end
    end

    private

    def initialize(param1, param2)
    @param1 = param1
    @param2 = param2
    def self.call(thing = ThingModel.new, params)
    new(thing, params).call
    end

    def do_something
    def call
    @something ||= find_things

    if @something.success?
    Result.new(true, [...], nil)
    Result.new([...], nil)
    else
    Result.new(false, [], '...')
    Result.new([], '...')
    end
    end

    private

    def initialize(thing, params)
    @thing = thing
    @params = params
    end

    def find_things
    # ...
    end
    end

    ThingService.call(param1, param2)
    ThingService.call(params)
  6. matheusazzi created this gist Jun 27, 2017.
    36 changes: 36 additions & 0 deletions thing_service.rb
    Original 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)