Sha256: f5ae4f387267f179fae7f6d699d1b0da3f1aa0c18b715512b024599d27c47e56

Contents?: true

Size: 969 Bytes

Versions: 4

Compression:

Stored size: 969 Bytes

Contents

# encoding: utf-8

module Okei

  # Finds an unit by uuid:
  #
  #   use_case = GetUnit.new uuid: "581dfac6-5b58-4227-a242-503f5f8a275d"
  #   use_case.run
  #   # => #<Okei::Unit @uuid: "581dfac6-5b58-4227-a242-503f5f8a275d" ...>
  #
  # Any other parameters are ignored.
  #
  # Allows subscription before running:
  #
  #   use_case.subscribe logger
  #   use_case.subscribe controller
  #   use_case.run
  #
  class GetUnit < Hexx::UseCase

    allow_params :uuid

    # Runs the method:
    #
    # * оn success publishes `found(unit)` and returns an `unit`
    # * on error raises `Hexx::NotFoundError` with `self`
    #
    def run!
      unit_found?
      publish :found, unit
      unit
    end

    private

    def uuid
      @uuid ||= params["uuid"]
    end

    def unit
      @unit ||= Unit.by_uuid(uuid).first
    end

    def unit_found?
      return if unit
      errors.add :uuid, :not_found
      fail Hexx::NotFoundError.new self
    end
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
okei-1.0.2 app/use_cases/okei/get_unit.rb
okei-1.0.1 app/use_cases/okei/get_unit.rb
okei-1.0.0 app/use_cases/okei/get_unit.rb
okei-1.0.0.pre.rc app/use_cases/okei/get_unit.rb